How to migrate database from Heroku using ‘pg:transfer’

I recently changed my local development environment and realized that my app was no longer linking to my local database. I could have just relink the local database, but instead I decided to pull more realistic data from Heroku.

My mentor Ken Mazaika at The Firehose Project suggested using ‘pg:pull’. Others suggested to look into Seed_dump gem. After reading documentation and stumbling upon what to use, I came across this plugin called Heroku pg:transfer CLI plugin.

I like plugins:) I come from Wordpress background….I was immediately sold.

Here are the steps:

  1. Install the plugin by running
$ heroku plugins:install https://github.com/ddollar/heroku-pg-transfer

2. Check if the plugin is installed correctly

$ heroku pg:transfer --help

3. Transfer the data by passing two arguments -f (from) and -t (to)

Go to your app’s Heroku settings and copy your DATABASE_URL. Paste this address after -f option. After -t option, you specify your database name from your database_yml file. In my case, my development database name was xxx_development. Note: Do all this from your app directory:)

$ heroku pg:transfer -f postgres://xxx:xxxxx/xxxx -t postgres://localhost/xxx_development

Run

$ rake db:migrate

That’s it.

I got a lot of errors after this since I have added a new migration file that I have not pushed to the production site. So I had to rollback the migration and delete the migration file:/

Also, I’m not sure what happens if you have existing database on your local app:) Read the documentation or ask your mentor.

Hope this helps.