0

I'm trying to import a CSV file into PostgreSQL but I am having an issue with special characters.

I'm using the following command

./psql -d data -U postgres -c "copy users from 'users.csv' delimiter E'\t' quote '~' csv"

It works fine until it encounters a field with the '~' which I'm using as a quote value to not break the existing quotes and inverted commas etc.

How do I escape this character in the csv file 'Person~name' so that it will import as 'Person~name'

1
  • 1
    show simple example how your CSV format looks like and which PostgreSQK version you use. I can't recreate error on my machine. Commented Oct 28, 2015 at 12:34

1 Answer 1

2

CSV rules are listed in https://www.ietf.org/rfc/rfc4180.txt

To embed the quote character inside a string:

  1. If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. For example:

    "aaa","b""bb","ccc"

In your case, replace double-quote by tilde, since you've choosen that delimiter.

Example:

test=> create table copytest(t text);
CREATE TABLE

test=> \copy copytest from stdin delimiter E'\t' quote '~' csv
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> ~foo~~bar~
>> \.

test=> select * from copytest;
   t    
---------
 foo~bar
Sign up to request clarification or add additional context in comments.

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.