12

I'm trying to insert some data into my table using the copy command :

copy otype_cstore from '/tmp/otype_fdw.csv' delimiter ';' quote '"' csv;

And I have this answer :

ERROR: unterminated CSV quoted field

There is the line in my CSV file where I have the problem :

533696;PoG;-251658240;from id GSW C";

This is the only line a double quote and I can't remove it, so do you have some advice for me ?

Thank you in advance

3
  • two choices: either you escape the quote or you simply remove it from that line. Commented May 22, 2017 at 8:46
  • I can't remove from my line. What do you mean by esacpe the quot ? Commented May 22, 2017 at 8:48
  • 1
    escape the quote would be to change the value to "from id GSW C""". Alternatively, you could use a different quoting character (if that is an option). The CSV file is syntactically incorrect, you'll have to fix it in some fashion. Commented May 22, 2017 at 8:59

3 Answers 3

21

If you have lines like this in your csv:

533696;PoG;-251658240;from id GSW C";

this actually means/shows the fields are not quoted, which is still perfectly valid csv as long as there are no separators inside the fields.

In this case the parser should be told the fields are not quoted.

So, instead of using quote '"' (which is actually telling the parser the fields are quoted and why you get the error), you should use something like quote 'none', or leave the quote parameter out (I don't know Postgres, so I can't give you the exact option to do this).

Ok, I did a quick lookup of the parameters. It looks like there is not really an option to turn quoting off. The only option left would be to provide a quote character that is never used in the data.

quote E'\b' (backspace) seems to work ok.

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

Comments

2

Bit late to reply, but I was facing same issue and found that for double quote we need to add one more double quote to escape with along with prefix and suffix double quote to that special characater. for your case it would be

input data is : from id GSW C"

change data to : from id GSW C""""

note there are 4 consecutive double quotes. first and last double quote is prefix and postfix as per documentation. middle 2 double quotes is data with one escape double quote. Hope this helps for readers with similar issue going forward.

So for every double quote in data it needs to be escaped with one escape character (default double quote). This is as per documentation.

Comments

0

Import the csv file using the Import/Export data option, Select the file you want to Import Go to Options, scroll down to "Escape" select double quotes instead of single quotes "

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.