Common SQL Errors

PostgreSQL bad_copy_file_format (22P04) Error Explained

August 4, 2025

The bad_copy_file_format error (22P04) appears when COPY cannot parse the input file because its structure does not match the declared format options.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is PostgreSQL error bad_copy_file_format (22P04)?

PostgreSQL bad_copy_file_format (22P04) occurs when COPY cannot parse the data file. Check delimiters, null markers, headers, and encoding, then rerun COPY with corrected format.

Error Highlights

Typical Error Message

bad_copy_file_format

Error Type

Data Import Error

Language

PostgreSQL

Symbol

bad_copy_file_format

Error Code

22P04

SQL State

Explanation

Table of Contents

What is PostgreSQL error bad_copy_file_format (22P04)?

PostgreSQL raises error 22P04 – bad_copy_file_format – when the COPY command cannot parse the source data according to the supplied format options. The server detects unexpected field counts, unmatched quotes, or incompatible encodings and aborts the import.

The error stops data ingestion, leaving the target table unchanged.

Resolving it quickly matters because partial or failed loads block analytics, ETL pipelines, and downstream applications.

What Causes This Error?

Incorrect delimiters, quote marks, or escape characters make COPY misinterpret column boundaries, producing bad_copy_file_format instantly.

Mismatched column counts between the file and table definition trigger the same error as soon as PostgreSQL meets a shorter or longer row.

Encoding differences such as UTF-8 data loaded with SQL_ASCII or mismatched null strings (e.g., "NULL" vs "\\N") also break parsing.

How to Fix bad_copy_file_format

First, inspect the file with a text editor or csvkit to find malformed lines.

Count delimiters per row and confirm quoting rules.

Next, rerun COPY with explicit FORMAT, DELIMITER, NULL, QUOTE, and ENCODING parameters that match the file. Trim header rows or use HEADER TRUE if needed.

Large files benefit from testing a sample via COPY ... FROM PROGRAM 'head -n 100 file.csv' to validate format before a full load.

Common Scenarios and Solutions

CSV exported from Excel often uses Windows-1252 encoding.

Add ENCODING 'WIN1252' or convert the file to UTF-8 with iconv to avoid 22P04.

Files created by pg_dump may include a header line. Use COPY ...

CSV HEADER to ignore it.

If a JSON column contains commas, wrap the JSON with double quotes and specify QUOTE '"' and ESCAPE '"' to keep COPY aligned.

Best Practices to Avoid This Error

Validate data files in CI pipelines with csvlint or similar linters before copying into production.

Always specify explicit format options in COPY instead of relying on defaults so future script changes remain safe.

Log rejected rows to a staging table using COPY ...

LOG ERRORS (PostgreSQL 14+) or load into a temporary table for review.

Related Errors and Solutions

Error 22004 null_value_not_allowed appears when a NOT NULL column receives NULL during COPY. Define default values or allow NULL.

Error 22P05 untranslatable_character is thrown when encoding conversion fails. Set correct ENCODING or sanitize the file.

Error 23502 not_null_violation happens when a row lacks a required column. Align file columns to the table schema.

.

Common Causes

Related Errors

FAQs

Can I ignore malformed rows?

PostgreSQL 14+ supports COPY ... LOG ERRORS to record bad rows and continue loading.

How do I detect the exact bad line?

Use COPY ... FROM PROGRAM 'tail -n +start file.csv' in a loop or load into a staging table with row numbers.

Does bad_copy_file_format affect existing data?

No. COPY is transactional. On error, the entire statement rolls back and existing table data stays intact.

How can Galaxy help?

Galaxy highlights COPY errors inline, lets you preview CSV files, and suggests correct COPY syntax with its AI copilot.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo