postgresql 如何将数据库附加到 Heroku 中的应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11700602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:08:14  来源:igfitidea点击:

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

databasepostgresqlheroku

提问by Emam

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 my App.

我正在使用 Heroku 的 Postgres 插件,并从 Heroku Postgres 插件页面创建了一个新的生产数据库。我没有使用我的应用程序的资源页面将它直接添加到我的应用程序中。

Now I want to attach this database to my App so it'll be recognized by the heroku pgcommand.

现在我想将此数据库附加到我的应用程序,以便heroku pg命令可以识别它。

I'm able to use the database btw after setting the DATABASE_URLconfig var of my app to point to it, but heroku pgcommand doesn't recognize it yet.

顺便说一下,在将我的应用程序的DATABASE_URL配置变量设置为指向它之后,我可以使用数据库,但是heroku pg命令还不能识别它。

Additional info: The previous database was Shared, and the new one is a Production.

附加信息:以前的数据库是共享的,新的数据库是生产。

回答by rdegges

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?

您是否使用独立于应用程序的https://postgres.heroku.com/站点添加了数据库?或者你只是在你的 Heroku 控制面板中创建了一个 postgresql 数据库?

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

如果您在https://postgres.heroku.com/上创建了您的数据库,您将不会通过您的heroku pg:info命令看到该数据库。但是,要将数据库添加到应用程序中,您可以执行以下操作:

  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:[email protected]:5432/omg".
  6. In your application, on the command line, run heroku config:set DATABASE_URL=postgres://blah:[email protected]:5432/omg
  1. 登录https://postgres.heroku.com/
  2. 单击要附加到应用程序的数据库。
  3. 在“连接设置”下,单击右上角的配置按钮。
  4. 然后单击“URL”选项。
  5. 复制您的数据库 URL,这应该类似于“postgres://blah:[email protected]:5432/omg”。
  6. 在您的应用程序中,在命令行上运行 heroku config:set DATABASE_URL=postgres://blah:[email protected]:5432/omg

What we did there was assign your database to the DATABASE_URLenvironment 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.

我们所做的是将您的数据库分配给DATABASE_URL应用程序中的环境变量。这是您在本地为应用程序配置数据库时默认使用的变量,因此理论上,分配此值对您来说应该没问题。

回答by Stéphane Bruckert

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

Heroku 附加组件现在可以跨应用程序附加,并且可以在单个应用程序上多次附加。

heroku addons:attach ADDON_NAME -a APP_NAME

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

来源: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

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

回答by Blake Erickson

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 commandsand as far as I can tell there is no supported Heroku way of attaching a database to a heroku app.

要将您在https://postgres.heroku.com/上创建的数据库附加到您正在使用的实际 heroku 应用程序,您不能使用任何pg 备份命令,据我所知没有支持将数据库附加到 heroku 应用程序的 Heroku 方式。

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 创建数据库的备份,然后使用 pg_restore 填充附加到您的应用程序的新数据库:

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.

即使您从“基本计划”升级到“起重机计划”,您仍然需要进行备份和恢复,但由于数据库已经附加到您的应用程序,您可以使用 heroku 备份命令。