psql> COPY (SELECT * FROM tablename) TO '/tmp/filename.csv' WITH CSV HEADER;