3
votes

I am trying to use the 'copy' command for importing the csv file into my postgres table. Values containing commas have been surrounded by double quotes but I can't seem to find an option to load them into the postgres table without any errors using the 'copy' command.

'COPY' command I am using:

CREATE TABLE candidates (Sno int, name varchar, cases int, case_details varchar, .....);
copy candidates from 'something.csv' with NULL AS ' ' csv ;

Example of an offending csv line:

1, "some name", 2, "(1):IPC Sections -  147, 323, 352, 504, 506 , Other Details - Case no.283A/2000, A.C.J.M-5, Ghumangunj Ellahabad, UP, Dt.12.11.2000", .....

Above the case_details attribute has commas in its value. That is my problem.

3
any chance your CSV was generated by a Microsoft product? in that case, it may have weird quoting - stackoverflow.com/questions/14573623/… - if so, you can use the ruby method provided to convert to a "normal" CSV and load with postgresql's COPYSeamus Abshere
@muistooshort question details updated.kb_14
@SeamusAbshere No. My csv file has values with commas in it. Please see the updated question details for more infokb_14
Postgres as I am writing in 2019 should work with comma inside double quotes without any problem. I encountered the same error message while loading a large table with 64 columns, it turned out to be my own problem as always (programmers trying to figure what else was wrong). I left out one column in the table definition. We programmers think we are smart actually we have difficulties counting to number 64.Kemin Zhou

3 Answers

6
votes

Works for me (PG 9.2, linux):

$ cat something.csv 
1, "some name", 2, "(1):IPC Sections -  147, 323, 352, 504, 506 , Other Details - Case no.283A/2000, A.C.J.M-5, Ghumangunj Ellahabad, UP, Dt.12.11.2000"

$ psql test
test=> CREATE TABLE candidates (Sno int, name varchar, cases int, case_details varchar);
CREATE TABLE
test=> \copy candidates from 'something.csv' with NULL AS ' ' csv ;
test=> select * from candidates ;
 sno |    name    | cases |                                                             case_details                                                             
-----+------------+-------+--------------------------------------------------------------------------------------------------------------------------------------
   1 |  some name |     2 |  (1):IPC Sections -  147, 323, 352, 504, 506 , Other Details - Case no.283A/2000, A.C.J.M-5, Ghumangunj Ellahabad, UP, Dt.12.11.2000
(1 row)
1
votes

Possible solution - replaces comas in quotes with any other character, load the data, replace the comas back.

0
votes

The PhpPgAdmin tool isn't smart enough to handle commas. Postgres is smart enough to handle them. It splits the data up in a given row from an imported file first using the commas, and then it automatically puts quotes around the values afterwards. So there's no way to do it with the client. At least for version 5.1 this is the case. They may have fixed it in later versions.

So if you're importing from the PhpPgAdmin tool, replace the commas with ";" or something that is set apart from your data, and then run a SQL query after import to fix.