How to export table from heroku production database locally to excel from console using Ruby?

calendar_today Asked Mar 22, 2013
thumb_up 18 upvotes
history Updated April 16, 2026

Question posted 2013 · +6 upvotes

I know how to export table in Rails to formated Excel file: http://railscasts.com/episodes/362-exporting-csv-and-excel

But how to do that from console.

Accepted answer +18 upvotes

Connect to your Heroku database by using

heroku pg:psql

Then run the sql command to get the csv file e.g.

copy (SELECT * FROM users) TO dump.csv CSV DELIMITER ','

Use q to exit.

After executing the commands dump.csv would have been created in your local environment

2 code variants in this answer

  • Variant 1 — 1 lines, starts with heroku pg:psql
  • Variant 2 — 1 lines, starts with copy (SELECT * FROM users) TO dump.csv CSV DELIMITER ','

Top excel Q&A (6)

+18 upvotes ranks this answer #33 out of 167 excel solutions on this site — top 20%.