Menu Close

Save psql query output to csv file

by stackoverflow.com

In terminal (while connected to the db) set output to the csv file

1) set field seperator to “;” by:

\f ';'

2) set output format unaligned:

\a

3) show only tuples

\t

4) set output

\o '/tmp/yourOutputFile.csv'

5) execute your query

select * from YOUR_TABLE

6) output

\o

you will be able to find your cvs file in this location

cd //tmp
copy it using scp command 

or edit using nano:

nano //tmp/yourOutputFile.csv

You can also print to console with \o again.

Posted in Linux, News

Leave a Reply

Your email address will not be published. Required fields are marked *