Clock Blog

Exporting Postgres Tables as CSV

Posted on Wednesday, 20 April 2011 @ 14:53 GMT in tech-blogs by Paul Serby

If you need to get a quick CVS of a large query output to a client in a CSV and are using PgAdmin3 or some other GUI tool running on your local machine you might want to considering running this command on the database server directly.

 echo "COPY (SELECT * FROM \"User\") TO STDOUT CSV;" | psql -U postgres BigCompanyDatabase | gzip > Users-`date +%F-%H%M`.csv.gz
This will then email you the file. NOT TO BE USED FOR ANYTHING OVER 2MB
 mutt -s 'User Export' -a Users-`date +%F-%H%M`.csv.gz -- paul.serby@clock.co.uk < /dev/null

PgAdmin3, over SSH, on a 2Mbit line, took 30 mins to pull 11,000 rows. With the command line version it was in my inbox in less than a second.

blog comments powered by Disqus