Diagnostic Queries
Symptoms
A COPY input row contained more fields than the target column list. PostgreSQL raises SQLSTATE 22P04 (bad_copy_file_format).
- A data row has too many columns.
- Common with the wrong delimiter or unquoted embedded delimiters.
- The row count exceeds the expected columns.
What the server log shows
ERROR: extra data after last expected column
CONTEXT: COPY orders, line 42: "1,2024-01-01,42.00,extra"
Why PostgreSQL raises this — what the manual says
the COPY reference (File Formats):
“For COPY FROM, each field in the file is inserted, in order, into the specified column.”
COPY expects each row to have exactly as many fields as the target column list. A row with more fields (often from a wrong delimiter or an unescaped delimiter in data) doesn’t align, so PostgreSQL reports 22P04.
Common causes
- Wrong
DELIMITERfor the file. - Unquoted delimiter characters inside a data value.
- Mismatch between the file and the table’s column list.
How to fix it
- Set the correct
DELIMITER(e.g. CSV with commas). - Quote fields containing the delimiter, or use
FORMAT csvwith proper quoting. - Align the COPY column list with the file’s columns.
Related & next steps
Reference: PostgreSQL 18 — COPY.
Thanks — noted. This helps keep the database accurate.