Skip to main content

Exporting a CSV from Postgres

There are many ways to run a SQL statement in psql and export the result as a CSV. These are the two I usually reach for.

From a postgres repl:

\copy (SELECT * FROM users) 
TO 'users.csv' 
WITH (FORMAT csv, HEADER true);

Shell one-liner:

psql --csv -c "SELECT * FROM users;" --host 127.0.0.1 --port 5432 --user fred --dbname my_db > users.csv