I am using psql
with a PostgreSQL database and the following copy
command:
\COPY isa (np1, np2, sentence) FROM 'c:\Downloads\isa.txt' WITH DELIMITER '|'
I get:
ERROR: extra data after last expected column
How can I skip the lines with errors?
You cannot skip the errors without skipping the whole command up to and including Postgres 9.5. There is currently no more sophisticated error handling.
\copy
is just a wrapper around SQLCOPY
that channels results through psql. The manual forCOPY
:Bold emphasis mine. And:
There was an attempt to add error logging to
COPY
in Postgres 9.0, spearheaded by Aster Data, but it was never committed. The company was later acquired by Teradata, so I doubt they are still pursuing the project.Solution
Fix your input file instead.
If you have one or more additional column in your input file and the file is otherwise consistent, you might add dummy columns to your table
isa
and drop those afterwards. Or (cleaner with production tables) import to a temporary staging table andINSERT
selected columns (or expressions) to your target tableisa
from there.Related answers with detailed instructions: