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
CREATE TABLE customers (id int PRIMARY KEY, name text);CREATE TABLECOPY customers FROM STDIN WITH (FORMAT csv);
1,Kim,kim@example.com
2,Lee,lee@example.com
\.ERROR: 22P04: extra data after last expected column
CONTEXT: COPY customers, line 1: "1,Kim,kim@example.com"ALTER TABLE customers ADD COLUMN email text;ALTER TABLECompare the field count of the first line with the table's column count before loading.
COPY customers FROM STDIN WITH (FORMAT csv);
1,Kim,kim@example.com
2,Lee,lee@example.com
\.
SELECT * FROM customers ORDER BY id;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
CREATE TABLE prices (item text, price int);CREATE TABLECOPY prices FROM STDIN WITH (FORMAT csv);
Pen;1200
Note;3500
\.ERROR: 22P04: missing data for column "price"
CONTEXT: COPY prices, line 1: "Pen;1200"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'prices' ORDER BY ordinal_position; 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.
COPY prices FROM STDIN WITH (FORMAT csv, DELIMITER ';');
Pen;1200
Note;3500
\.
SELECT * FROM prices;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
CREATE TABLE orders (id int PRIMARY KEY, customer text, amount int);CREATE TABLECOPY orders FROM STDIN WITH (FORMAT csv);
1,Kim,1000
2,Lee
\.ERROR: 22P04: missing data for column "amount"
CONTEXT: COPY orders, line 2: "2,Lee"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'orders' ORDER BY ordinal_position; column_name | data_type
-------------+-----------
id | integer
customer | text
amount | integer
(3 rows)COPY orders FROM STDIN WITH (FORMAT csv);
1,Kim,1000
2,Lee,
\.
SELECT * FROM orders ORDER BY id;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