Forking Your Heroku Postgres Database
Last updated July 26, 2024
Table of Contents
Forking creates a new database containing a snapshot of an existing database at the current point in time. Unlike follower databases, forks don’t stay up to date with the parent database and you can write to them. Forks don’t affect the performance of the parent database.
Forking is only supported on Standard-tier or higher database plans. Follow these steps to upgrade from an Essential-tier database to a plan in a higher tier.
Use Cases
Forks don’t affect the performance of the parent database.
Forked databases provide a risk-free way of working with your production data and schema. For example, you can use them to test new schema migrations or to load test your application on a different database plan. They’re also valuable as snapshots of your data at a particular point in time for later analysis or forensics.
Create a Fork
You can create forks on any Standard, Premium, Private, or Shield database that isn’t a follower.
You can’t create forks from another newly forked database for a short period of time. This limitation applies both to forks created by the methods described in this article and to forks created through unfollow. Use the pg:info
command to check if Fork/Follow
is Available
.
Forks don’t have to be on the same database plan as the parent database. Forks can be on any plan but must have enough disk space to contain the data from the parent database.
Preparing a fork can take anywhere from several minutes to several hours, depending on the size of your dataset.
After forking a database, the app the fork is attached to updates with a new HEROKU_POSTGRESQL_COLOR_URL
config var. This config var update restarts the app.
Create Using the CLI
You can create forks using the Heroku CLI with the heroku addons:create
command. Provision a new database add-on with the --fork
flag. Supply the flag with one of the following:
- The config var name of the database on the same app
- The name of the database add-on
- The full URL of any Heroku Postgres database
- An argument in the form of
appname::HEROKU_POSTGRESQL_COLOR_URL
. This allows you to create a fork of a database that is attached to another app.
The addons:create
example follows the syntax for Heroku CLI v9.0.0 or later. If you’re on v8.11.5 or earlier, use the command:
$ heroku addons:create heroku-postgresql:standard-0 --fork HEROKU_POSTGRESQL_CHARCOAL_URL --app example-app
$ heroku addons:create heroku-postgresql:standard-0 --app example-app -- --fork HEROKU_POSTGRESQL_CHARCOAL_URL
Adding heroku-postgresql:standard-0 on example-app... done, v71 ($50/mo)
Attached as HEROKU_POSTGRESQL_SILVER_URL
Database will become available after it completes forking
Use `heroku pg:wait` to track status
The heroku pg:wait
command outputs the provisioning status of any new databases. Use it to determine when the fork is up to date.
$ heroku pg:wait --app example-app
Waiting for database HEROKU_POSTGRESQL_SILVER_URL... available
Create Using the Heroku Data Dashboard
You can also create forks through the web dashboard:
- Go to data.heroku.com.
- Use the search and select the database you want to create a fork from.
- Click the
Settings
tab. - Click
Fork Database...
. - Choose the plan for the fork. Review the Create a Fork section for notes on selecting a plan size.
- Click
Fork Database
.
The dashboard shows the status of the forked database and updates when the provisioning is complete.
Fork Fast Option
Fast forks can be up to 30 hours out-of-date.
You can create forks faster via the CLI with the --fast
flag. This option is useful if no major changes, such as schema migrations or large data imports, occurred in your database in the last 30 hours.
The addons:create
example follows the syntax for Heroku CLI v9.0.0 or later. If you’re on v8.11.5 or earlier, use the command:
$ heroku addons:create heroku-postgresql:standard-4 --fork HEROKU_POSTGRESQL_CHARCOAL --fast --app example-app
$ heroku addons:create heroku-postgresql:standard-4 --app example-app -- --fork HEROKU_POSTGRESQL_CHARCOAL --fast
Adding heroku-postgresql:standard-4 on dashboard... done, v1022 ($1200/mo)
Attached as HEROKU_POSTGRESQL_BLUE_URL
Fork will contain data from 5/26/2021 at 13:56 UTC (about 16 hours old)
To create a fork with up-to-date data, exclude the `--fast` flag.
Database will become available after it completes forking
Use `heroku pg:wait` to track status.
Use `heroku addons:docs heroku-postgresql` to view documentation
Delete a Fork
Delete Using the CLI
Deprovision a fork using heroku addons:destroy
:
Be sure to remove the _URL
suffix from the database name in this command.
$ heroku addons:destroy HEROKU_POSTGRESQL_SILVER --app example-app
! WARNING: Destructive Action
! This command will affect the app: example-app
! To proceed, type "example-app" or re-run this command with --confirm example-app
Delete Using the Heroku Data Dashboard
- Go to data.heroku.com.
- Use the search and select the fork database you want to delete.
- Click the
Settings
tab. - Click
Delete Database...
. - Type in the name of your app to confirm.
- Click
Delete Database
.