Search code examples
postgresqlherokuheroku-postgresheroku-cli

How to use a new database on Heroku


Originally when I provisioned a database on Heroku (free one), then I my app got a new environment variable called DATABASE_URL that points to a newly created database and my app uses this one.

Today when I provisioned a new database on Heroku (paid) via the UI, I got a new environment variable called HEROKU_POSTGRESQL_MAUVE_URL that points to new database.

How do I point my Heroku app to this newly provisioned database HEROKU_POSTGRESQL_MAUVE_URL?

I expected my database URL to be swapped with HEROKU_POSTGRESQL_MAUVE_URL but that didn't happen.


Solution

  • This happens when your app has multiple databases:

    As part of the provisioning process, a DATABASE_URL config var is added to your app’s configuration. DATABASE_URL contains the URL your app uses to access the database. If your app already has a Heroku Postgres database and you’ve provisioned another one, this config var’s name instead has the format HEROKU_POSTGRESQL_<COLOR>_URL (for example, HEROKU_POSTGRESQL_YELLOW_URL

    You can promote your new database to make it the primary database:

    pg:promote updates the value of the DATABASE_URL config var with the newly promoted database’s connection string. It also creates an alternate attachment for the old primary database, assigned with a new HEROKU_POSTGRESQL_<color>_URL config var. The promotion process triggers a release and restarts the app.

    For example:

    heroku pg:promote HEROKU_POSTGRESQL_MAUVE_URL -a your-app
    

    You should continue to use the DATABASE_URL environment variable to connect to your database.