- Published on
Using psql to export Postgres tables to CSV
- Authors
- Name
- Yair Mark
- @yairmark
From time to time you need to export the data in a table to a text file for analysis. If you are using Postgres then the excellent psql command line tool has you covered. To export a table:
- Login to the DB you need:
psql -U dbUsername -d dbName -h hostname -W
- The
W
option will prompt you for a password - You can leave out the
-h hostname
if you are on the host running the DB - Then to create the csv from the table you want run:
\copy yourTableName to 'yourfileName' csv;
- Then to quit:
\q
The copy command has a bunch more configurations which you can read about here.