Will Heroku database exist after uninstalling the application - and can I transfer it to another application?

I have a Heroku application that runs on the Cedar-10 stack, which will soon be deprecated. I follow the Migration to the Celadon Cedar-14 Stack manual and created an instance of my application in a new stack. However, it also automatically created another (empty) PostgreSQL .

  • Is it possible to upgrade to a new stack, but continue to use the existing database?
  • When I remove the application from the old stack, will it automatically delete the database associated with it?

I see that the database URL is set in the environment variable $DATABASE_URL - does this mean that I can somehow update this and "link" the old database to the new application?

When searching for information about this, I came across the heroku directives pg:copy and pg:transfer , but it seems strange to duplicate the database when it works fine, and has paid β€œupdates” and backups already associated with it.

+6
source share
3 answers

Problem with approach to configuration variable

So far, you can share the database between two applications in Heroku by copying the configuration variable DATABASE_URL first application to the second, only the first application will have the main connection (addition) to the database.

If you uninstall the first application, your database will disappear from the postgres control panel in Heroku. Although it seems that the second application will still be able to use the database. (Tested in 2015-10-25)

It is also worth noting that:

Heroku reserves the right to change the database URLs as necessary. If this happens, your additional connections will fail, and you will need to update the URLs accordingly.

This means that if Heroku decides to change the URL of your database, your new application will fail, and since the database will now disappear from your dashboard, you will not be able to get the new URL without approaching Heroku. (If at all.)

This means that there is a more internal relationship between the application and its database than just DATABASE_URL , and it might not be a better idea to use this as a mechanism for transferring.


Alternative Approach Using PGBackups

Heroku official documentation recommends using the free PGBackups add-on (which, frankly, you should always work anyway) to transfer data between Postgres instances. The tool uses its own tools pg_dump and pg_restore .

In particular, you can use the PG copy function described below:

PG copy uses PostgreSQL's built-in backup and recovery tools. Instead of writing the backup to disk, it transfers it by cable directly to the recovery process in the new database.

They conveniently provide you with the means to copy the database between applications with a single command and without any intermediate storage:

Alternatively, you can transfer data between databases that are connected to various applications.

To copy the source database to the target database, you will need to call pg: copy from the target application, referencing the source database.

Example:

 heroku pg:copy source-application::OLIVE HEROKU_POSTGRESQL_PINK -a target-application 

Where source-application is your existing stack, and target-application your new one. It does not apply to you, but it is worth mentioning that any existing data in the target-application database will be destroyed.

After that, you may need to promote the database in a new application like this:

 heroku pg:promote HEROKU_POSTGRESQL_PINK 
+2
source

You can change configuration variables to point from a new application database to an existing database. I did this before without any problems (just make sure you have a new backup).

So, in your new application, run heroku config . You will see a link to your database in DATABASE_URL . Then you can change the value with:

 heroku config:set DATABASE_URL=<existing-database-url> 

At this point, you can use your old application and a new application pointing to the same database without aging. As you said, you pay for updating the database in your existing database, so uninstalling the old application should not delete the database; if so (do you have backups again?), then there is time to contact Heroku!

If you are not comfortable using the command line, you can also change the values ​​in your Settings tab on the Heroku application toolbar.


With all that said, you do not need to create a new application to update your stack. Again, I did a stack update before, and you should be able to do it locally; which confirms Heroku documents .

+2
source

Although the @Drenmi approach is probably the safest option, it allows you to transfer the ownership database between applications. Make a backup first.

Heroku databases are considered Add-Ons. Therefore, when you, for example, press the rails, the heroku application will add the postgres db addon to the application.

First use heroku addons to get a list of connected add-ons:

 $ heroku addons --app old_app Add-on Plan Price ─────────────────────────────────────── ──── ───── heroku-postgresql (resting-fairly-4672) dev free └─ as HEROKU_POSTGRESQL_CYAN 

Then we can bind the database to the new application.

 heroku addons:attach resting-fairly-4672 --app new_app 

And disconnect it from the old application.

 heroku addons:detach resting-fairly-4672 -app old_app 

See https://devcenter.heroku.com/articles/managing-add-ons

+2
source

All Articles