備份
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://postgres.tw/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 的備份檔案。