Importing CSV and commas into string values

I am using postgres 8.4 and am currently trying to import a trivial CSV:

Here is a table:

CREATE TABLE public.sample (
  a VARCHAR, 
  b VARCHAR
) WITHOUT OIDS;

      

and here is an example csv file:

"foo","bar, baz"

      

Request

COPY sample FROM '/tmp/sample.csv' USING DELIMITERS ',';

      

expected throws

ERROR:  extra data after last expected column
CONTEXT:  COPY sample, line 1: ""foo","bar, baz""

      

But, well, CSV parsing is not rocket science and I would wonder if it is impossible to solve it without reformatting the csv source.

Any ideas?

PS: the csv comes from a third party, I cannot change the format.

PPS: yes, I understand that I could preprocess it (by changing the delimiter manually) before importing it

Final solution :

COPY sample FROM '/tmp/sample.csv' WITH DELIMITER ',' CSV;

      

Originally taken from this StackOverflow question and the documentation page is http://www.postgresql.org/docs/8.4/static/sql-copy.html

+3


source to share


1 answer


Obviously you have a CSV file when you try to import it as a text format. Using:

COPY sample FROM '/tmp/sample.csv' FORMAT CSV;

      



The default CSV format separator is comma ( ,

). More details in the manual . The syntax for the current version is 9.1. For PostgreSQL 8.4:

COPY sample FROM '/tmp/sample.csv' CSV;

      

+4


source







All Articles