3

In PostgreSQL psql, how to make \copy command ignore empty lines in input file?

Here is the code to reproduce it,

create table t1(
  n1 int
);

echo "1
2

" > m.csv

psql> \copy t1(n1) FROM 'm.csv' (delimiter E'\t', NULL 'NULL', FORMAT CSV, HEADER false);

ERROR:  invalid input syntax for integer: ""
CONTEXT:  COPY t1, line 3, column n1: ""

There is an empty line in file m.csv

cat m.csv
1
2
  << empty line

1 Answer 1

5

PostgreSQL COPY is very strict, so there is not possibility to start COPY in tolerant mode. If it is possible, you can use COPY FROM PROGRAM

[pavel@nemesis ~]$ cat ~/data.csv 
10,20,30
40,50,60

70,80,90

psql -c "\copy f from program ' sed ''/^\s*$/d'' ~/data.csv ' csv" postgres
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.