SQLSTATE 22P04 ERROR Class 22: Data Exception

bad_copy_file_format COPY from stdin failed: … — 22P04

PostgreSQL error "COPY from stdin failed: …" (SQLSTATE 22P04): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

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

  1. Read the appended reason (extra/missing column, bad encoding) and fix the input.
  2. Match COPY options (FORMAT, DELIMITER, ENCODING) to the data.
  3. Validate the source file before streaming it.

Related & next steps

Reference: PostgreSQL 18 — COPY.

Was this helpful?