+2 votes
in Databases by (15.2k points)
I want to create a CSV file using the output of SELECT query. How can I export the output?

1 Answer

+1 vote
by (25.6k points)

Use 'copy' command to copy the output to a CSV file. Here is an example to create tab delimited CSV file.

\copy (SELECT * FROM table_name) to 'path_to_file/filename.csv' WITH DELIMITER E'\t' CSV HEADER QUOTE E'\b';

...