簡體   English   中英

如何恢復 postgres 備份 - 錯誤:無法刪除當前打開的數據庫

[英]How to restore a postgres backup - ERROR: cannot drop the currently open database

我正在嘗試從不再配置的系統中恢復 postgres 備份,但我遇到了一系列錯誤,具體取決於我的嘗試。

  • 我只有備份,沒有原始服務器可供參考
  • 我過去已經成功地從這些生產備份中恢復了,但是自從上次成功后我的本地環境就搞砸了(例如,通過 Homebrew 重新安裝了 postgres)
  • 我相信一位前同事(不再可用)已成功恢復此特定備份
  • 我在以下情況下遇到了同樣的錯誤:
    • 嘗試使用 postgresql@9.4 進行備份
    • 嘗試以前已知良好的數據庫版本(使用 postgres 11)

(我已閱讀相關/建議的問題,但它們似乎不相關)

使用以下命令創建備份(在 bash 腳本中):

pg_dump --schema=public -Fc

用於恢復腳本的命令(由備份腳本作為幫助文本回顯)是:

dropdb ${PGDATABASE}
createdb ${PGDATABASE}
time pg_restore \
    -j4 \
    -d ${PGDATABASE} \
    --create \
    --no-privileges \
    --no-owner \
    --clean \
    --if-exists \
    --exit-on-error \
    "${dirname}/${filename}"

當我在本地運行恢復命令時,我得到以下信息:

time pg_restore -j4 -d ${PGDATABASE} --create --no-privileges --no-owner --clean --if-exists --exit-on-error ../backups/backup__2019-09-03_16-00-19.pg_dump
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 4954; 1262 962277 DATABASE mydb mydb
pg_restore: [archiver (db)] could not execute query: ERROR:  cannot drop the currently open database
    Command was: DROP DATABASE IF EXISTS mydb;

real    0m0.049s
user    0m0.019s
sys    0m0.012s

以前,我只在運行幾分鍾后出現“磁盤空間不足錯誤”,但從 output time可以看出,此錯誤幾乎立即發生!

備份是使用壓縮-Fc創建的,我可以使用pg_restore backup.pg_dump > backup.sql將備份解壓縮為純文本,但我無法在其中找到與DROP DATABASE相關的任何命令:/

-- Dumped from database version 9.6.11
-- Dumped by pg_dump version 11.3 (Ubuntu 11.3-1.pgdg14.04+1)

我看到數據庫最初是從9.6.11轉儲的,但我有理由確定我之前使用 PG 10 成功恢復了...

我嘗試了各種方法,例如:

time pg_restore -d ${PGDATABASE} --no-privileges --no-owner --exit-on-error ../backups/backup__2019-09-03_16-00-19.pg_dump
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 7; 2615 1033373 SCHEMA public mydb
pg_restore: [archiver (db)] could not execute query: ERROR:  schema "public" already exists
    Command was: CREATE SCHEMA public;

ptim:dropdb mydb; createdb mydb
ptim:gizmag ptim$ dropdb mydb

ptim:gizmag ptim$ time pg_restore -d ${PGDATABASE} --create --no-privileges --no-owner --exit-on-error ../backups/backup__2019-09-03_16-00-19.pg_dump
pg_restore: [archiver (db)] connection to database "mydb" failed: FATAL:  database "mydb" does not exist

# guess I misunderstood the create flag!

ptim:gizmag ptim$ dropdb mydb; createdb mydb
dropdb: database removal failed: ERROR:  database "mydb" does not exist

ptim:gizmag ptim$ time pg_restore -d ${PGDATABASE} --create --no-privileges --no-owner --exit-on-error ../backups/backup__2019-09-03_16-00-19.pg_dump
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 4954; 1262 962277 DATABASE mydb mydb
pg_restore: [archiver (db)] could not execute query: ERROR:  database "mydb" already exists
    Command was: CREATE DATABASE mydb WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8';

我也嘗試從明文轉儲中恢復但未成功:

psql -U ptim -d mydb -1 -f ../backups/backup__2019-09-03_16-00-19.sql

SET
SET
SET
SET
SET
 set_config
------------

(1 row)

SET
SET
SET
SET
psql:../backups/backup__2019-09-03_16-00-19.sql:23: ERROR:  schema "public" already exists
psql:../backups/backup__2019-09-03_16-00-19.sql:26: ERROR:  current transaction is aborted, commands ignored until end of transaction block

# snip... repeated, and varied errors follow

有什么建議么?!

感謝@a_horse_with_no_name 的提示,我了解到:

-C , --create在恢復之前創建數據庫。 如果還指定了--clean ,則在連接到它之前刪除並重新創建目標數據庫。

使用此選項時,以-d命名的數據庫僅用於發出初始DROP DATABASECREATE DATABASE命令。 所有數據都恢復到存檔中出現的數據庫名稱中。

(在dba.stackexchange 進一步解釋:為什么 pg_restore 忽略 --create 。請注意, --create在較新版本的pg_restore中具有進一步的不相關影響)

我的解決方案是刪除--create參數:

dropdb ${PGDATABASE}
createdb ${PGDATABASE}
time pg_restore \
    -j4 \
    -d ${PGDATABASE} \
    --no-privileges \
    --no-owner \
    --clean \
    --if-exists \
    --exit-on-error \
    backup.pg_dump

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM