PG COPY error: invalid input syntax for integer PG COPY error: invalid input syntax for integer postgresql postgresql

PG COPY error: invalid input syntax for integer


ERROR: invalid input syntax for integer: ""

"" isn't a valid integer. PostgreSQL accepts unquoted blank fields as null by default in CSV, but "" would be like writing:

SELECT ''::integer;

and fail for the same reason.

If you want to deal with CSV that has things like quoted empty strings for null integers, you'll need to feed it to PostgreSQL via a pre-processor that can neaten it up a bit. PostgreSQL's CSV input doesn't understand all the weird and wonderful possible abuses of CSV.

Options include:

  • Loading it in a spreadsheet and exporting sane CSV;
  • Using the Python csv module, Perl Text::CSV, etc to pre-process it;
  • Using Perl/Python/whatever to load the CSV and insert it directly into the DB
  • Using an ETL tool like CloverETL, Talend Studio, or Pentaho Kettle


I think it's better to change your csv file like:

"age","first_name","last_name"23,Ivan,Poupkine,Eugene,Pirogov

It's also possible to define your table like

CREATE TABLE people (  age        varchar(20),  first_name varchar(20),  last_name  varchar(20));

and after copy, you can convert empty strings:

select nullif(age, '')::int as age, first_name, last_namefrom people


Just came across this while looking for a solution and wanted to add I was able to solve the issue by adding the "null" parameter to the copy_from call:

cur.copy_from(f, tablename, sep=',', null='')