Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL COPY csv including Quotes

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?

like image 978
TheLovelySausage Avatar asked Feb 20 '15 12:02

TheLovelySausage


1 Answers

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.
like image 120
mlinth Avatar answered Nov 07 '22 20:11

mlinth