Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore quotation marks when importing a CSV file into PostgreSQL?

Tags:

postgresql

I'm trying to import a tab-delimited file into my PostgreSQL database. One of the fields in my file is a "title" field, which occasionally contains actual quotation marks. For example, my tsv might look like:

id    title
5     Hello/Bleah" Foo

(Yeah, there's just that one quotation mark in the title.)

When I try importing the file into my database:

copy articles from 'articles.tsv' with delimiter E'\t' csv header;

I get this error, referencing that line:

ERROR:  unterminated CSV quoted field

How do I fix this? Quotation marks are never used to surround entire fields in the file. I tried copy articles from 'articles.tsv' with delimiter E'\t' escape E'\\' csv header; but I get the same error on the same line.

like image 545
grautur Avatar asked Sep 11 '11 04:09

grautur


1 Answers

Assuming the file never actually tries to quote its fields:

The option you want is "with quote", see http://www.postgresql.org/docs/8.2/static/sql-copy.html

Unfortunately, I'm not sure how to turn off quote processing altogether, one kludge would be to specify a character that does not appear in your file at all.

like image 98
daxelrod Avatar answered Sep 28 '22 07:09

daxelrod