13

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.

grautur
  • 29,955
  • 34
  • 93
  • 128

3 Answers3

11

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.

daxelrod
  • 2,499
  • 1
  • 22
  • 30
6

Tab separated is the default format for copy statements. Treating them as CSV is just silly. (do you take this path just to skip the header ?)

copy articles from 'articles.tsv';

does exactly what you want.

wildplasser
  • 43,142
  • 8
  • 66
  • 109
  • 2
    Sounds simple enough, but what if the CSV data contains backslashes as valid field content? (yes, I've encountered this) The default format for COPY treats a backslash as an escape char, so then you HAVE to use COPY with CSV format... – Tom De Leu Dec 05 '13 at 14:00
  • I had not thought about that one. Yes: it makes sense. – wildplasser Dec 06 '13 at 00:12
2

I struggled with the same error and a few more. Finally gathering knowledge from few SO questions I came up with the following setup for making COPY TO/FROM successful even for quite sophisticated JSON columns:

COPY "your_schema_name.yor_table_name" (your, column_names, here) 
FROM STDIN WITH CSV DELIMITER E'\t' QUOTE '\b' ESCAPE '\';
--here rows data
\.

the most important parts:

  • QUOTE '\b' - quote with backspace (thanks a lot @grautur!)
  • DELIMITER E'\t' - delimiter with tabs
  • ESCAPE '\' - and escape with a backslash
andilabs
  • 22,159
  • 14
  • 114
  • 151