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

Alexandre S.
  • 522
  • 2
  • 5
  • 24
  • two choices: either you escape the quote or you simply remove it from that line. – Arkhena May 22 '17 at 08:46
  • I can't remove from my line. What do you mean by esacpe the quot ? – Alexandre S. May 22 '17 at 08: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. – Laurenz Albe May 22 '17 at 08:59

2 Answers2

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.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
1

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.

Rakesh
  • 21
  • 5