PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 22P04: extra data after last expected column / missing data for column

The number of fields in a COPY line did not match the number of table columns.

PostgreSQL 17.12
22P04: extra data after last expected column

Jump to a cause

  1. 01The file has more columns than the table
  2. 02The file uses ; but COPY expects a comma
  3. 03One line has fewer fields than the others

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17

COPY splits each input line by the delimiter and expects exactly one field per table column. If a line has more fields, you get extra data after last expected column; if it has fewer, you get missing data for column "...". Both are SQLSTATE 22P04.

The CONTEXT line shows which line failed, for example COPY orders, line 2: "2,Lee". Below are three cases reproduced on PostgreSQL 17, each followed by a COPY that actually loaded the data.

01The file has more columns than the table

The table customers has two columns (id, name), but the CSV lines have three: 1,Kim,kim@example.com. COPY failed on line 1 with extra data after last expected column.

After adding the missing column with ALTER TABLE customers ADD COLUMN email text;, the same COPY returned COPY 2 and both emails were loaded.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, name text);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
COPY customers FROM STDIN WITH (FORMAT csv);
1,Kim,kim@example.com
2,Lee,lee@example.com
\.
✕ ERROR
ERROR:  22P04: extra data after last expected column
CONTEXT:  COPY customers, line 1: "1,Kim,kim@example.com"
Fixrun as lab_a
ALTER TABLE customers ADD COLUMN email text;
OUTPUT
ALTER TABLE

Compare the field count of the first line with the table's column count before loading.

After the fixrun as lab_a
COPY customers FROM STDIN WITH (FORMAT csv);
1,Kim,kim@example.com
2,Lee,lee@example.com
\.
SELECT * FROM customers ORDER BY id;
✓ SUCCESS
COPY 2
 id | name |      email
----+------+-----------------
  1 | Kim  | kim@example.com
  2 | Lee  | lee@example.com
(2 rows)

02The file uses ; but COPY expects a comma

The file is Pen;1200, but FORMAT csv splits on commas by default. The whole line became one field for item, so nothing was left for the second column: missing data for column "price" on line 1.

The table really has two columns (item, price). Telling COPY the actual delimiter with WITH (FORMAT csv, DELIMITER ';') loaded both rows (Pen 1200, Note 3500).

Setup
Setuprun as lab_a
CREATE TABLE prices (item text, price int);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
COPY prices FROM STDIN WITH (FORMAT csv);
Pen;1200
Note;3500
\.
✕ ERROR
ERROR:  22P04: missing data for column "price"
CONTEXT:  COPY prices, line 1: "Pen;1200"
Checkrun as lab_a
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'prices' ORDER BY ordinal_position;
OUTPUT
 column_name | data_type
-------------+-----------
 item        | text
 price       | integer
(2 rows)

If the column named in the message is the second column and line 1 fails, suspect the delimiter first.

After the fixrun as lab_a
COPY prices FROM STDIN WITH (FORMAT csv, DELIMITER ';');
Pen;1200
Note;3500
\.
SELECT * FROM prices;
✓ SUCCESS
COPY 2
 item | price
------+-------
 Pen  |  1200
 Note |  3500
(2 rows)

03One line has fewer fields than the others

Line 1 1,Kim,1000 is fine, but line 2 2,Lee has no third field, so COPY stopped with missing data for column "amount" on line 2. Line 1 was not loaded either: the whole COPY failed.

With a trailing comma, 2,Lee,, the line has three fields. COPY returned COPY 2 and amount for Lee was loaded as empty (NULL).

Setup
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, customer text, amount int);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
COPY orders FROM STDIN WITH (FORMAT csv);
1,Kim,1000
2,Lee
\.
✕ ERROR
ERROR:  22P04: missing data for column "amount"
CONTEXT:  COPY orders, line 2: "2,Lee"
Checkrun as lab_a
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'orders' ORDER BY ordinal_position;
OUTPUT
 column_name | data_type
-------------+-----------
 id          | integer
 customer    | text
 amount      | integer
(3 rows)
After the fixrun as lab_a
COPY orders FROM STDIN WITH (FORMAT csv);
1,Kim,1000
2,Lee,
\.
SELECT * FROM orders ORDER BY id;
✓ SUCCESS
COPY 2
 id | customer | amount
----+----------+--------
  1 | Kim      |   1000
  2 | Lee      |
(2 rows)

FAQ

What does "extra data after last expected column" mean in PostgreSQL COPY?

A line in the file has more fields than the table (or the column list) has columns. On this page the CSV had three fields for a two-column table.

Why do I get "missing data for column" when every row looks complete?

Check the delimiter. A file separated by ; loaded with plain FORMAT csv puts the whole line in the first column. Adding DELIMITER ';' fixed it here.

How do I find which row failed in COPY?

Look at the CONTEXT line under the error. It shows the table, the line number and the line text, e.g. COPY orders, line 2: "2,Lee".

Are the rows before the bad line imported?

No. In the short-row case, line 1 was valid but the table stayed empty; after fixing line 2, COPY loaded both rows (COPY 2).

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12