How do you print the result of a PostgreSQL query in CSV or TSV format from the command line?

Postgresql

Postgresql Problem Overview


I'd like to execute a query from the shell (not in the interactive psql client) and have it print the CSV or TSV representation of the output to STDOUT. How do you do that with psql or one of the PostgreSQL command-line tools?

Postgresql Solutions


Solution 1 - Postgresql

If you are using PostgreSQL 8.2 or newer, use this for CSV:

psql -c "COPY (<select query>) TO STDOUT WITH CSV"

and this of TSV, with proper NULLs:

psql -c "COPY (<select query>) TO STDOUT WITH NULL AS ''"

The CSV form will properly quote any fields that contain the double-quote character. See the PostgreSQL documentation of your specific version for more details and options for COPY.

Solution 2 - Postgresql

Starting from Bohemian's answer, I found these flags useful:

psql my_database -U myuser -A -F , -X -t -f /path/to/query.sql -o /path/to/output.csv
  • Unaligned output mode: -A
  • Use comma as field delimiter: -F ,
  • Do not read psqlrc: -X
  • Tuples only (no header/footer): -t
  • File containing SQL query: -f
  • Output file: -o

Solution 3 - Postgresql

EDITED: Using -F

Use commas via -F and use "unaligned table output mode" -A:

psql my_database -U myuser -A -F , -c "select * from mytable"

Solution 4 - Postgresql

To specify a tsv use the delimiter '\t'

psql my_database -U myuser -F'\t' --no-align -f mysqlfile.sql -o outputfile.tsv

To specify a csv use the delimiter ','

psql my_database -U myuser -F',' --no-align -f mysqlfile.sql -o outputfile.csv

Solution 5 - Postgresql

Also possible is the copy command which allows you to specify header, delimiters and quoting options

psql my_database -U my_user -c "copy (select a.id,b.id from my_table_a as a inner join my_table_b as b on b.id = a.id) to STDOUT"

Solution 6 - Postgresql

You can specify the field separator with the -F command line parameter to psql

Solution 7 - Postgresql

Export AS TSV WITH HEADER

You can include the HEADER as follows:

\COPY (SELECT * FROM tca) TO '/.../metab/tca.tsv' WITH DELIMITER E'\t' CSV HEADER;

\COPY (SELECT * FROM tca) TO '/...a/metab/tca.tsv' WITH NULL AS '' DELIMITER E'\t' CSV HEADER;

E.g. (PSQL):

[metabolism]# \COPY (SELECT * FROM tca) TO '/mnt/Vancouver/programming/data/metabolism/tca.tsv' WITH NULL AS '' DELIMITER E'\t' CSV HEADER;
COPY 22

BASH:

[victoria@victoria tsv]$ pwd
/mnt/Vancouver/programming/data/metabolism/tsv

[victoria@victoria tsv]$ head -n3 tca.tsv
uuid	src	tgt	rel	rel_type
878b87de-0ca8-49a8-9f77-a24353e251d2	oxalosuccinic acid	oxoglutaric acid	1.1.1.42	2
7fd9cf88-495b-491b-956e-294f19097923	isocitric acid	oxoglutaric acid	1.1.1.41	2
[victoria@victoria csv]$ 

Solution 8 - Postgresql

The simplest way (using psql) seems to be by using --csv flag:

psql --csv -c "SELECT * FROM sometable"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondanView Question on Stackoverflow
Solution 1 - PostgresqlMatthew WoodView Answer on Stackoverflow
Solution 2 - PostgresqlJason McVettaView Answer on Stackoverflow
Solution 3 - PostgresqlBohemianView Answer on Stackoverflow
Solution 4 - PostgresqlConKatView Answer on Stackoverflow
Solution 5 - PostgresqlGavinView Answer on Stackoverflow
Solution 6 - PostgresqlSzocskeView Answer on Stackoverflow
Solution 7 - PostgresqlVictoria StuartView Answer on Stackoverflow
Solution 8 - PostgresqlYan FotoView Answer on Stackoverflow