Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip over lines that cause errors in PostgreSQL Copy Command

I get a UTF encoding error when trying to copy data from a csv file

Error invalid by sequence for encoding...
SQL State: 22021 
Context Copy mydatabase line 165

I am wondering if it is possible to skip over the line(s) that throws the error. I am aware that is possible to skip the first line using the header command but the line I get isn't in the first row. I'd like to find a solution inside of Postgres rather than one that has me going into another program.

like image 579
dharv Avatar asked Oct 31 '22 08:10

dharv


1 Answers

Unfortunately, that functionality hasn't been integrated into PostgreSQL. The quickest workaround I know of is to use sed to filter out bad input lines. If you really must keep the solution PostgreSQL-based, I know of no alternative to using PL/pgsql or another procedural language interface to read in your data.

like image 96
Politank-Z Avatar answered Nov 15 '22 07:11

Politank-Z