obda.net

Export Postgres Table to CSV

Add a comment

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

PostgreSQL’s psql command line client features a \copy command that allows dumping a table to a CSV file:

\copy table_name to 'filename.csv' delimiter ',' csv header

The header argument at the end will add a header line with the column names to the CSV file. Use ; as delimiter if the CSV file shall be compatible with Microsoft’s Excel.