COPY (query)
TO 'file';
COPY (select * from users)
TO '/tmp/users.csv';

https://www.postgresql.org/docs/9.2/static/sql-copy.html