Heroku transfer db from one app to another

后端 未结 8 1846
攒了一身酷
攒了一身酷 2021-01-31 02:25

I need to transfer db from app_1 to app_2

I created backup on app_1

Then ran:

heroku pg:backups restore HEROKU_POSTGRESQL_COLOR --app app_2 heroku

相关标签:
8条回答
  • 2021-01-31 03:12

    You may find useful pgAdmin III (http://pgadmin.org/), a free db management tool specifically designed to do these types of tasks. You can edit/view/import/export from/to your Heroku db directly. Let me know if you need help in setup. (It's like MySQL Workbench, but for PostgreSQL).

    0 讨论(0)
  • 2021-01-31 03:13

    There are simple ways to do this, and there is a fast way to do it

    The simple ways generally involve using a backup/restore methodology (including pg:copy, which is a backup that streams the data directly to a pg_restore process), but these are slow in creating the new database because you are restoring a logical definition of tables, loading the data, and creating indexes on the data.

    That's a lot of work, and for my 30GB standard-2 databases it can literally take hours.

    The fast way to do it is to provision a follower of the database to be copied (e.g. production) on the application you want the data on (e.g. test). On the same 30GB databases that take hours to do a restore, this last took about 15 minutes.

    The methodology I use is:

    # Get the name of the source database addon (e.g. postgresql-clean-12345)
    heroku pg:info -a production-app
    
    # Create a follower on the destination app (choose your own plan)
    # You create the follower on the new app because otherwise it is 
    # perpetually associated with the source as it's "billing app"
    heroku addons:create heroku-postgresql:standard-2 --follow postgresql-clean-12345 -a test-app
    heroku pg:wait -a test-app
    
    # Quiesce the destination app
    heroku scale web=0 worker=0 -a test-app
    heroku maintenance:on -a test-app
    
    # Get the colour of the new database (e.g. HEROKU_POSTGRESQL_GRAY_URL)
    heroku pg:info -a test-app
    
    # Unfollow the source database.
    # If you want to upgrade the database, do that now instead of the
    # unfollow.
    heroku pg:unfollow HEROKU_POSTGRESQL_GRAY_URL -a test-app
    
    # Promote the new database on the destination app
    heroku pg:promote HEROKU_POSTGRESQL_GRAY_URL -a test-app
    
    # Get the colour of the old database, if any(e.g. HEROKU_POSTGRESQL_MAROON_URL)
    heroku pg:info -a test-app
    
    # Destroy the old database (if any)
    heroku addons:destroy HEROKU_POSTGRESQL_MAROON_URL -a test-app
    
    # Bring the test app back up
    heroku scale web=1 worker=1 -a test-app
    heroku maintenance:off -a test-app
    

    Why is this faster?

    I believe that when creating a follower, Heroku creates the new database by either copying data files or restoring from a physical (file) backup, then replaying the logs to bring it up to date.

    0 讨论(0)
提交回复
热议问题