Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL COPY command with multiple FORCE_QUOTE columns

Tags:

postgresql

I'm trying to export a table to CSV and force quoting of two columns.

I've tried the following command:

COPY hotel_position_daily TO '/home/ubuntu/export/hotel_position_daily.csv' CSV HEADER FORCE_QUOTE hotelname, city;

which resulted in

ERROR:  syntax error at or near "FORCE_QUOTE"
LINE 1: ...buntu/export/hotel_position_daily.csv' CSV HEADER FORCE_QUOT...

and I've tried

COPY hotel_position_daily TO '/home/ubuntu/export/hotel_position_daily.csv' WITH (FORMAT CSV, HEADER, FORCE_QUOTE hotelname, city);

which resulted in

ERROR:  argument to option "force_quote" must be a list of column names

what am I doing wrong?

like image 733
Zoltán Avatar asked Jul 03 '15 10:07

Zoltán


People also ask

What does the COPY command in PostgreSQL do?

Description. COPY moves data between PostgreSQL tables and standard file-system files. COPY TO copies the contents of a table to a file, while COPY FROM copies data from a file to a table (appending the data to whatever is in the table already). COPY TO can also copy the results of a SELECT query.

Does Postgres COPY overwrite?

If you COPY data into a table already containing data, the new data will be appended. If you COPY TO a file already containing data, the existing data will be overwritten.

How do I COPY output from PostgreSQL?

The easiest but the most efficient way to export data from a Postgres table to a CSV file is by using the COPY command. COPY command generates a CSV file on the Database Server. You can export the entire table or the results of a query to a CSV file with the COPY TO command.


1 Answers

I found the solution to be

COPY hotel_position_daily TO '/home/ubuntu/export/hotel_position_daily.csv' WITH (FORMAT CSV, HEADER, FORCE_QUOTE(hotelname, city));
like image 135
Zoltán Avatar answered Nov 15 '22 05:11

Zoltán