DEV Community

Kaleem for This is Learning

Posted on

3 1

How to easily migrate Heroku Postgres database between apps.

There are two ways to copy data between databases: restore backup or copy command.

We will be performing a database migration from the myapp-prod app to the myapp-test app that is copying the schema (structure) and data from the source to target.

Restore backup

Copies specified backup version of the database.

heroku pg:backups:capture --app myapp-prod

Creates a backup in the app myapp-prod. The Heroku console will tell you backup ID version b001 in this case.

heroku pg:backups:restore myapp-prod::b001 DATABASE_URL --app myapp-test

Second command: Copy from myapp-prod app backup id b001 (latest backup) to default DATABASE_URL of the app myapp-test.

Direct copy

Copies the latest state of the database.

heroku pg:copy myapp-prod::DATABASE_URL DATABASE_URL --app myapp-test

Copy from myapp-prod app latest default database DATABASE_URL copy to app myapp-test default database DATABASE_URL.

Here DATABASE_URL is the name of the default database of your Heroku apps.

Thanks :)

Reference
https://devcenter.heroku.com/articles/heroku-postgres-backups#restore-a-backup

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay