Skip to content
Advertisement

Why am I getting “Invalid input syntax for type integer” in postgresql when importing a CSV?

I’m trying to import a .csv file to my postgresql DB. I created a table as follows:

CREATE TABLE accounts
(
    acc_id integer,
    acc_name text,
    website text,
    lat numeric,
    longe numeric,
    primary_poc text,
    sales_rep_id integer
)

Then I used the following command to import the .csv file

COPY accounts(acc_id,acc_name,website,lat,longe,primary_poc,sales_rep_id) 
FROM 'D:accounts.csv' DELIMITER ';' CSV ;

And my .csv file contains the following:

1;Walmart;www.walmart.com;40.23849561;-75.10329704;Tamara Tuma;321500
2;Exxon Mobil;www.exxonmobil.com;41.16915630;-73.84937379;Sung Shields;321510
3;Apple;www.apple.com;42.29049481;-76.08400942;Jodee Lupo;321520

However, this doesn’t work and the following message appear:

ERROR:  invalid input syntax for type integer: "1"
CONTEXT:  COPY accounts, line 1, column acc_id: "1"
SQL state: 22P02

Advertisement

Answer

Maybe there is a BOM in the CSV?

  • hexdump the file, and inspect the first three characters
  • (and) use an editor to remove the BOM
  • (or) export again, without the BOM (there should be a checkmark, even in the Microsoft “software”)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement