How can I attach a database to an app in Heroku?

前端 未结 3 1300
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 11:01

I\'m using Heroku\'s Postgres addon, and I created a new production database from the Heroku Postgres addon page. I Didn\'t add it directly to my App using the Resources page of

相关标签:
3条回答
  • 2021-02-05 11:27

    Did you add the database using the app-independent https://postgres.heroku.com/ site? Or did you just create a postgresql database in your Heroku control panel?

    If you created your database on https://postgres.heroku.com/, you will not see the database via your heroku pg:info command. What you can do to add your database to your application, however, would be to:

    1. Log into https://postgres.heroku.com/.
    2. Click on the database you want to attach to your application.
    3. Under 'Connection Settings', click the configuration button at the top right.
    4. Then click the 'URL' option.
    5. Copy your database URL, this should be something like "postgres://blah:blah@ec2-23-23-122-88.compute-1.amazonaws.com:5432/omg".
    6. In your application, on the command line, run heroku config:set DATABASE_URL=postgres://blah:blah@ec2-23-23-122-88.compute-1.amazonaws.com:5432/omg

    What we did there was assign your database to the DATABASE_URL environment variable in your application. This is the variable that's used by default when you provision databases locally to your application, so theoretically, assigning this value should work just fine for you.

    0 讨论(0)
  • 2021-02-05 11:28

    Heroku add-ons may now be attached across applications and multiple times on a single app.

    heroku addons:attach ADDON_NAME -a APP_NAME
    

    Source: https://devcenter.heroku.com/changelog-items/646


    To know the name of your addon, do:

    heroku addons
    

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

    0 讨论(0)
  • 2021-02-05 11:28

    To get your database that you created at https://postgres.heroku.com/ attached to your actual heroku app that you are working on you can't use any of the pg backup commands and as far as I can tell there is no supported Heroku way of attaching a database to a heroku app.

    You can however create a backup of your database using pg_dump and then use pg_restore to populate your new database that is attached to your app:

    pg_dump -i -h hostname -p 5432 -U username -F c -b -v -f "backup-filename" database_name
    

    Once that is complete you can populate your new database with:

    pg_restore -i -h new_hostname -p 5432 -U new_username -d new_database_name -v "same_backup_filename"
    

    Even if you are upgrading from the "basic plan" to a the "crane plan" you still have to do a backup and restore, but since the db's are already attached to your app you have the advantage of using the heroku backup commands.

    0 讨论(0)
提交回复
热议问题