Postgres export to csv

July 31, 2008 – 11:58

To export PostgreSQL data to csv file using psql interactive terminal.

open terminal
su postgres
psql <db_name>
\f ‘,’
\a
\t
\o outputfile.csv
select ….. (your sql statement)
\o
\q

Additional notes: If you want a TAB delimited file instead of comma, use \f ‘Ctrl-V TAB’. \f sets the field separator. \a echo all. \t tuples(rows) only. \q quit.

Sorry, comments for this entry are closed at this time.