Skip to content

Instantly share code, notes, and snippets.

@brunnogomes
Forked from kosyfrances/heroku_database_copy.md
Created August 30, 2022 12:42
Show Gist options
  • Save brunnogomes/06db4501bb8f87a417a708202d2c3989 to your computer and use it in GitHub Desktop.
Save brunnogomes/06db4501bb8f87a417a708202d2c3989 to your computer and use it in GitHub Desktop.
To copy heroku database from one app to another and from local to heroku

To copy database from one heroku app to another -

heroku pg:backups capture [database_name]
heroku pg:backups restore $(heroku pg:backups public-url --app source_app) DATABASE_URL --app target_app

You can refer to https://devcenter.heroku.com/articles/heroku-postgres-backups for more information.

To copy database from local to heroku - Dump your local database in compressed format using the open source pg_dump tool: PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump where myuser is your database username and mydb is the database name. In order for PG Backups to access and import your dump file you will need to upload it somewhere with an HTTP-accessible URL. I used cloudinary, but you can use aws or anyone you prefer. heroku pg:backups restore 'the dump url on cloudinary or aws' DATABASE_URL e.g: heroku pg:backups restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' DATABASE_URL

To access heroku database from command line - heroku pg:psql --app app_name DATABASE_URL

@brunnogomes
Copy link
Author

Also:

heroku pg:copy source_project::source_database_name target_database_name --app target_app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment