Postgres Table to CSV

August 4, 2015

I just found a simple way to create CSV files from database tables in postgres:

mybandsite=# copy (select * from news_newsitem where site_id = 2) to '/tmp/news.csv' DELIMITER ',' CSV HEADER

This will create a file news.csv which will contain all the rows returned by the specified query. DELIMITER sets the delimiter to use, in this case a comma. CSV specifies the format and HEADER outputs a heading row.


Tags: csv postgres copy