Import CSV into Heroku

Become a Subscriber

It took me a while to find a nice and concise way to import a CSV into a Heroku Postgres Database that I use for Rails. At first I thought about doing it through Rails migrations and seeds. Then I realized that was more work and would pollute my repository. This is the approach I would recommend for others:

psql `heroku config:get DATABASE_URL`?ssl=true -c "\copy bans FROM 'export.csv' WITH CSV;"

As you probably know, psql is the command which opens a postgres terminal. The backtick command lets you fetch the database url scheme from heroku config and inject it into your command. The -c switch allows you to execute a command through your local driver, giving you access to files on your system. This means exports.csv in this example can be the relative path from the folder you execute from. You can also provide an absolute file path.

Hope that helps!