备份
Prisma Postgres 每 24 小时对您的数据库进行完整快照,以备份您的数据。如果您需要访问数据库备份,请使用与您的 Prisma Data Platform 帐户关联的电子邮件地址联系我们的支持团队。这样做有助于我们快速验证您的帐户并加快恢复过程。
请注意,在最近快照之后发生的任何数据库更改或事件可能无法恢复。
未来,Prisma Postgres 将根据用户特定配置提供更细粒度的备份机制,并具有时间点还原功能。
通过 @prisma/ppg-tunnel
创建备份文件
如果您想创建数据库的备份文件,可以使用 @prisma/ppg-tunnel
CLI。这对于在数据库之间迁移数据或创建数据库的本地副本非常有用。
前提条件
在开始之前,请确保您拥有
- 已安装 **Node.js**(版本 16 或更高版本)。
- 用于创建备份的 **PostgreSQL CLI 工具** (
pg_dump
)。使用 Postgres 版本 17,因为 Prisma Postgres 基于此版本。 - 您的 Prisma Postgres 数据库的 **数据库连接字符串**。
要创建备份,请确保您已安装 PostgreSQL 命令行工具。根据您的操作系统运行以下命令
- macOS
- Windows
- Linux
brew install postgresql@17
which pg_dump
which pg_restore
# Download from the official PostgreSQL website:
# https://postgresql.ac.cn/download/windows/
# During installation, select "Command Line Tools".
# Then verify with:
where pg_dump
where pg_restore
sudo apt-get update
sudo apt-get install postgresql-client-17
which pg_dump
which pg_restore
如果您已安装 PostgreSQL 但仍然看到 pg_dump
或 pg_restore
的“command not found”错误,请确保您的安装目录在系统的 PATH 环境变量中。
请确保您安装的是 Postgresql 版本 17。其他版本可能会在备份过程中导致错误。
1. 使用 @prisma/ppg-tunnel
直接连接到数据库
在您的终端中,运行 npx @prisma/ppg-tunnel
以建立到数据库的安全隧道。
如果您在当前目录中已经有一个设置了 DATABASE_URL
的 .env
文件,隧道 CLI 将自动拾取它——无需手动导出它。但是,如果您尚未设置 .env
文件,则需要显式设置 DATABASE_URL
环境变量。
要设置环境变量(使用您的实际数据库 URL)
export DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=eyJhbGciOiJIUzI..."
如果您在终端中显式设置 DATABASE_URL
,则该值将优先于 .env
文件中的值。
运行隧道
npx @prisma/ppg-tunnel --host 127.0.0.1 --port 5432
如果您愿意,可以提供任何其他端口。如果未提供端口值,则会分配一个随机端口。您应该看到类似于以下的输出
Prisma Postgres auth proxy listening on 127.0.0.1:5432 🚀
Your connection is authenticated using your Prisma Postgres API key.
...
==============================
hostname: 127.0.0.1
port: 5432
username: <anything>
password: <none>
==============================
请注意,您在输出中看到的端口将是一个随机分配的端口,可能与此处提到的端口不同。另外,保持此终端窗口打开,以便隧道保持活动状态!如果您关闭它,隧道将断开连接。
从终端输出中复制端口号,您将在下一步的 pg_dump
命令中需要它。
2. 使用 pg_dump
创建备份
在隧道运行的情况下,您现在可以通过运行以下命令转储数据库
PGSSLMODE=disable \
pg_dump \
-h 127.0.0.1 \
-p 5432 \
-Fc \
-v \
-d postgres \
-f ./mydatabase.bak \
&& echo "-complete-"
PGSSLMODE=disable
表示本地不需要 SSL,因为隧道已经加密了连接。
`-h` is the host (127.0.0.1)
`-p` is the port, which should match the one from the tunnel output.
`-Fc` uses the custom format for backups, recommended for pg_restore.
`-d` postgres is the default database name used in Prisma Postgres.
`-f` ./mydatabase.bak specifies the backup file name and location.
`-v` runs pg_dump in verbose mode.
这应该在当前目录中创建名为 mydatabase.bak
的备份文件。