Heroku - how to pull data from one database and put it to another one?

We have 2 Heroku apps - the first one is production and the second one is staging. I would like to pull data from one table from the production app (it's table users with all user's data) and push it to the staging database.

After a little research I found the addon called pgbackups - I have just a few concerns: Does this addon also allow to get data only from one table, not from the whole database?

The second thing is this - let's say that on production are users with IDs from 1 to 300. On the staging version users with IDs from 1 to 10. How to put those 300 users from the production to the staging version the way, that these 300 users would be counted from the ID 11 (we would like to keep our staging users in the staging database as well).

Thank you


There are ways to do this in straight SQL. If you're comfortable with that, go for it. This way is for devs comfortable in Rails -- so we pull data out using JSON, and create users with a new ID in the new database from that JSON.

Since you're pulling only 1 table, AND you want to reset the IDs to the new database, I recommend:

  1. bring a copy of the database locally with pbackups
  2. File.open('yourfile.json', 'wb') {|file| file << User.all.to_json }
  3. Connect to your new database, and move yourfile.json up there


users_json = JSON.parse(File.read('yourfile.json'))
users_json.each do |json|

This script pulls data from your Heroku database into a local postgres database. You need the pgbackups addon installed. Execute it from the root directory of your Heroku app.

#!/bin/bash -ex
# This script asks Heroku to backup the current production data, then downloads that dump and imports it into the local db.
# It is at least 3 times quicker than heroku db:pull, and we can only do it because we are dumping from postgres to postgres.
heroku pgbackups:capture --expire
curl -o tmp/latest.dump `heroku pgbackups:url`
pg_restore --verbose --clean --no-acl --no-owner -d your_db_name tmp/latest.dump

You can check my answer in this thread. You can use a library called forceps to do exactly what you are asking for.

Need Your Help