SQLSTATE 22P04 ERROR Class 22: Data Exception

bad_copy_file_format Bad Copy File Format — SQLSTATE 22P04

COPY input does not match the expected format.

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

Symptoms

COPY input does not match the expected format.

  • The error is written to the server log and returned to the client carrying SQLSTATE 22P04.
  • Any driver (libpq, JDBC, psycopg, npgsql, pgx) surfaces this code in its error object so you can branch on it programmatically.
  • PL/pgSQL can trap it by name: EXCEPTION WHEN bad_copy_file_format THEN.

Environment

Severity: ERROR  |  PostgreSQL versions: 12, 13, 14, 15, 16, 17

Reproduce with the exact statement and read the full message in the server log (raise log_min_messages / set log_min_error_statement for more context).

Root Cause

The data stream did not conform to the COPY format, delimiter, quoting, or column count.

Common causes:

  • Wrong delimiter, quote, or escape character.
  • A column-count mismatch between data and target list.
  • Missing header handling, or wrong encoding/line endings.
  • CSV versus text mode mismatch.

Diagnostic Queries

Recovery

Steps to resolve 22P04:

  1. Match the COPY options exactly (FORMAT csv, HEADER true, and the correct DELIMITER, QUOTE, and ESCAPE characters).
  2. Ensure every row has the same number of columns as the target list.
  3. Set the correct ENCODING and line endings.
  4. Validate a few sample rows with a strict parser before the bulk load.

Reference: PostgreSQL error codes — Class 22 (Data Exception).

Was this helpful?