在Heroku上如何在生产环境和预览应用程序之间共享一个Heroku Postgres数据库
如果按照这篇文章的内容做,就能完成↓
https://zenn.dev/karaageeeee/articles/41eff65f1c73c0
发生什么事情
可以在Heroku Pipeline上的Production和Preview共享同一个数据库。
可以确保在与Production相同数据量的情况下,速度和展示是否存在问题。
费用只需支付一个Heroku PostgreSQL的费用(大概)。
当上述的文章被删除时作为备份,可以复制粘贴。
首先,我们需要在一个应用程序中添加PostgreSQL的附加功能。无论是通过浏览器还是命令行都可以。
我们将在第一个应用程序(test-app-1)中检查已添加的附加功能。
$ heroku addons -a test-app-1
Add-on Plan Price State
───────────────────────────────────────────── ───────── ───── ───────
heroku-postgresql (postgresql-xxxx-yyyy) hobby-dev free created
└─ as DATABASE
请确认上述显示的附加组件ID。在此情况下,该部分应为postgresql-xxxx-yyyy。
以确认的附加组件ID为基础,将第二个应用程序添加为test-app-2。
$ heroku addons:attach -a test-app-2 postgresql-xxxx-yyyy
Attaching postgresql-xxxx-yyyy to ⬢ test-app-2... done
Setting DATABASE config vars and restarting ⬢ test-app-2... done, v13
完成后,我们要确认两个插件已经被添加到第二个应用程序中。
$ heroku addons -a test-app-2
Add-on Plan Price State
───────────────────────────────────────────── ───────── ──────────────────────────────── ───────
heroku-postgresql (postgresql-xxxx-yyyy) hobby-dev (billed to test-app-1 app) created
├─ as DATABASE
└─ as DATABASE on test-app-1 app
从上述情况可以了解到,这个附加组件也被用于test-app-1。
最后,检查两个应用程序的环境变量是否设置了相同的内容。
$ heroku config -a test-app-1
=== test-app-1 Config Vars
DATABASE_URL: postgres://xxxx:yyyy@ec2-xxxxx.compute-1.amazonaws.com:5432/zzzz
$ heroku config -a test-app-2
=== test-app-2 Config Vars
DATABASE_URL: postgres://xxxx:yyyy@ec2-xxxxx.compute-1.amazonaws.com:5432/zzzz
只要两个 DATABASE_URL 匹配,就完成了。
可以在Heroku界面上确认它已与多个应用程序相关联。
