user456584
user456584

Reputation: 88845

Easiest way to dump Heroku database for use in local seed.rb?

I can dump a heroku database with $ heroku pgbackups:capture. Also, this SO post shows that there are tools for taking a development database and dumping it to seed.rb.

I'm wondering if there is an easy way to combine the two processes, effectively dumping the data from a production Heroku database into my local seeds.rb for more realistic development testing.

If this is possible, what's the cleanest way to do this?

Update:

Based on the insightful answer from dB', I may consider using PGSQL locally. I am still interested, however, in the seed.rb aspect of the question if there is a way to do that easily.

Upvotes: 3

Views: 4309

Answers (2)

dB'
dB'

Reputation: 8330

Not sure if it's what you're looking for, but have you tried copying the database to your local machine using pgbackups:capture and pg_restore? This approach doesn't use seeds.rb, but still recreates your production database on your local machine. It looks something like this.

$ heroku pgbackups:capture
$ curl -o latest.dump `heroku pgbackups:url`
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump

(This code is copied liberally from the explanation at the Heroku dev center.)

Upvotes: 2

Ryan Daigle
Ryan Daigle

Reputation: 11667

There are a couple ways to accomplish such a thing. @dB' has outlined one of them - using the PG Backups add-on to export your database. It's a great options but involves a few (trivial) manual commands.

I would recommend using the pg:transfer Heroku CLI plugin to transfer the data in a single step. Under the covers it's still very much the same thing happening as with using PG Backups, but it's packaged a bit nicer and has some useful defaults.

From your app's directory, copy your production database locally (assuming a local PG db), by installing the plugin and execute the pg:transfer command.

$ heroku plugins:install https://github.com/ddollar/heroku-pg-transfer
$ heroku pg:transfer

There are a couple options you can set as well. See my writeup for more details.

Hope that helps! And yes, please do use the same database during development as you do in production.

Upvotes: 5

Related Questions