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?
Asked
Active
Viewed 5.6k times
64

dan
- 43,914
- 47
- 153
- 254
8 Answers
111
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.

Matthew Wood
- 16,017
- 5
- 46
- 35
-
12It really does make this stuff easy, don't it? Imagine the answer from some of the "big database vendors"... First install this 64 Meg package and then run this export tool that has the most arcane syntax imaginable. Be careful not to delete your whole database while using it. :) – Scott Marlowe Jun 29 '11 at 17:18
-
Note: you can't get a header for TSVs this way, you need something like https://stackoverflow.com/a/57862424 – dfrankow Apr 17 '23 at 23:05
41
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

Jason McVetta
- 1,383
- 11
- 11
-
5This will *not* work as you might expect if you are outputting a text field with a comma in it. – Jon Carlson Jun 29 '17 at 19:00
8
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"

Bohemian
- 412,405
- 93
- 575
- 722
-
3This will give problems if your separator character appears in values of the column. – Red15 Oct 23 '12 at 11:54
-
@Red15, how do you go around this problem of ` if your separator character appears in values of the column` – Jin.X Dec 04 '15 at 12:11
-
@Jin You should [ask that as a new question](http://stackoverflow.com/questions/ask) – Bohemian Dec 05 '15 at 17:30
-
@Jin.X I personally think the CSV output (which is the accepted answer) would be the easiest way. – Red15 Mar 10 '16 at 14:03
6
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
-
`'\t'` is not enough in Bash; `--field-separator=$'\t'` works as expected – Francesco Frassinelli May 15 '23 at 15:00
3
You can specify the field separator with the -F command line parameter to psql

Szocske
- 7,466
- 2
- 20
- 24
2
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]$

Victoria Stuart
- 4,610
- 2
- 44
- 37