Welcome to the Heroku Postgres guide! This guide will help you get started with using Heroku Postgres, Heroku's managed database service.
快速开始
连接到 Heroku Postgres
To connect your application to Heroku Postgres, you'll need to use the Heroku CLI. First, make sure you have the CLI installed and logged in.
heroku login
Next, create a new database using the heroku addons:create
command:
heroku addons:create heroku-postgresql:hobby-dev
This will create a new database for your application and add it to your Heroku app.
使用环境变量连接
Heroku Postgres 会自动将数据库的连接信息作为环境变量添加到你的应用中。在你的应用中,你可以使用以下环境变量来连接到数据库:
DATABASE_URL
: 包含数据库连接信息的完整 URL。
在你的应用中,你可以使用以下代码来连接到数据库:
import os
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL)
管理你的数据库
Heroku Postgres 提供了多种方式来管理你的数据库。
- Heroku Dashboard: 你可以在 Heroku Dashboard 中查看数据库的详细信息,包括连接字符串、性能指标和备份等。
- Heroku CLI: 使用 Heroku CLI 可以执行各种数据库操作,如备份、还原和迁移等。
备份
要备份你的数据库,可以使用以下命令:
heroku pg:backups:capture
这将创建一个数据库备份,并将其存储在 Heroku 的备份存储中。
其他资源
希望这个指南能帮助你快速开始使用 Heroku Postgres!🚀