14

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.

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Cool, thanks! I randomly tried quote E'\b', which ended up working.
@grautur What does E'\b' mean? Backspace? what about the 'E'? "escape string constant" - got it.
@IanWarburton Yes, it seems it's a backspace: en.wikipedia.org/wiki/%5Cb
7

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.

2 Comments

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...
I had not thought about that one. Yes: it makes sense.
3

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

Comments

0

To copy from CSV file to PostgreSQL table with headers in CSV file using query:

  • First Add all the files in C:/temp folder

  • Then write the below scripts which accepts both NULL values as well as EMPTY strings

     copy PUBLIC."TABLE_NAME" FROM 
    'C:\tmp\TABLE_NAME.CSV' 
    (format csv, null "NULL", DELIMITER ',', HEADER);
    

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.