PostgreSQL COPY csv including Quotes

11,316

According to the documentation, the default quote symbol is ", so you need to provide a QUOTE argument with a different symbol. The quote symbol has to be a single one-byte character.

COPY tbname FROM '/tmp/file.csv'
delimiter '|' QUOTE '}' csv; -- use a symbol you know does not appear in your file.
Share:
11,316
TheLovelySausage
Author by

TheLovelySausage

Primarily Informix-4GL Programmer, Linux Bash Programmer and Linux Administrator. Dabbler in Java, Python, HTML, PHP and JavaScript. Expert at breaking everything.

Updated on June 14, 2022

Comments

  • TheLovelySausage
    TheLovelySausage almost 2 years

    This is a very simple problem, I am using the psql terminal command COPY as shown bellow

    COPY tbname FROM '/tmp/file.csv'
    delimiter '|' csv;
    

    However this file.csv contains data such as

    random|stuff|32"
    

    as well as

    random|other "stuff"|15
    

    I tried to use the double quote to escape the quotes as the Postgres site suggested

    random|stuff|32""
    random|other ""stuff""|15
    

    This seems to remove the quotes completely which I don't want. Is there a way to get the import to just treat these quotes as regular characters so that they appear in the database as they do in the csv file?