Diagnostic Queries
Symptoms
A COPY … FROM STDIN failed while reading input. PostgreSQL raises SQLSTATE 22P04 (bad_copy_file_format) with details about what went wrong.
- The streamed COPY input was rejected.
- Often follows a malformed line or a client-side abort.
- Accompanied by a more specific reason.
What the server log shows
ERROR: COPY from stdin failed: error in COPY data
Why PostgreSQL raises this — what the manual says
As the COPY reference (Description) explains:
A COPY … FROM STDIN aborted while reading the client-supplied data stream — typically because the client sent malformed input, closed the stream early, or the server-side row processing raised an error that ended the copy.
COPY FROM STDIN parses streamed text/CSV/binary input. If the stream is malformed or the client aborts mid-transfer, COPY cannot continue and reports 22P04 with the underlying reason.
Common causes
- Malformed rows in the input stream (wrong delimiters/columns).
- The client aborted the data transfer.
- Format/encoding mismatch between client and COPY options.
How to fix it
- Read the appended reason (extra/missing column, bad encoding) and fix the input.
- Match COPY options (
FORMAT,DELIMITER,ENCODING) to the data. - Validate the source file before streaming it.
Related & next steps
Reference: PostgreSQL 18 — COPY.
Thanks — noted. This helps keep the database accurate.