1

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
22
  • Try: \copy accounts from 'D:\accounts.csv' DELIMITER ';' CSV; Commented Feb 16, 2020 at 15:47
  • It gave the same error Commented Feb 16, 2020 at 15:54
  • Do you have a header? If so, try adding "HEADER" ...FROM 'D:/accounts/csv' DELIMITER ';' CSV HEADER; Commented Feb 16, 2020 at 16:06
  • 2
    just tested, it does (on Windows), indeed a feature, so do powershell "format-hex accounts.csv" to chheck for the BOM, If it starts with a BOM you will see EF BB BF as first 3 bytes. Commented Feb 16, 2020 at 16:38
  • 1
    Never trust Excel spreadsheets, always load to a staging table where they can be validated and corrected before loading to production tables. You think they're safe. Think again. Commented Feb 16, 2020 at 17:17

1 Answer 1

3

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")
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.