Heroku pg:psql to CSV export (locally)

Heroku has a nice UI for that called Dataclips but IMHO command line still beats it. I needed to run an SQL query and export the results as CSV on a local machine and here is a quick little snippet to do just that:


heroku pg:psql -c "\COPY (SELECT id, name, email FROM users WHERE created_at BETWEEN '2016-01-01' AND '2016-12-31' ORDER BY created_at ASC) TO STDOUT CSV DELIMITER ',' HEADER" > users-2016.csv