[英]PostgreSQL pg_restore error from pgadmin4 showing role doesn't exist
[英]pg_restore error: role XXX does not exist
試圖將數據庫從一個系統復制到另一個系統。 涉及的版本是 9.5.0(源)和 9.5.2(目標)。
源數據庫名稱為foodb
,所有者為pgdba
,目標數據庫名稱為foodb_dev
,所有者為pgdev
。
所有命令都在將托管副本的目標系統上運行。
pg_dump
命令是:
pg_dump -f schema_backup.dump --no-owner -Fc -U pgdba -h $PROD_DB_HOSTNAME -p $PROD_DB_PORT -d foodb -s --clean;
這運行沒有錯誤。
對應的pg_restore
是:
pg_restore --no-owner --if-exists -1 -c -U pgdev -d foodb_dev schema_backup.dump
引發錯誤:
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 3969; 0 0 ACL public pgdba
pg_restore: [archiver (db)] could not execute query: ERROR: role "pgdba" does not exist
Command was: REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM pgdba;
GRANT ALL ON SCHEMA public TO pgdba;
GRANT ...
如果我以純文本格式( -Fp
)生成轉儲文件,我會看到它包含幾個條目,例如:
REVOKE ALL ON TABLE dump_thread FROM PUBLIC;
REVOKE ALL ON TABLE dump_thread FROM pgdba;
GRANT ALL ON TABLE dump_thread TO pgdba;
GRANT SELECT ON TABLE dump_thread TO readonly;
嘗試為用戶pgdba
設置權限,該用戶當然甚至不作為目標系統上的用戶存在,只有用戶pgdev
,因此來自pg_restore
的錯誤。
在源數據庫上,例如dump_thread
表的權限:
# \dp+ dump_thread
Access privileges
-[ RECORD 1 ]-----+--------------------
Schema | public
Name | dump_thread
Type | table
Access privileges | pgdba=arwdDxt/pgdba+
| readonly=r/pgdba
Column privileges |
Policies |
一個快速的解決方案是簡單地在目標集群上添加一個用戶pgdba
並完成它。
但是, --no-owner
不應該首先在轉儲中不包括所有者特定的命令嗎?
我意識到--no-owner
與-x
不同。 我將-x
添加到所有pg_dump
命令中,這意味着:
-x, --no-privileges do not dump privileges (grant/revoke)
這實際上從轉儲中排除了有問題的GRANT
/ REVOKE
命令。 問題已解決。
要恢復數據庫,請運行以下命令:
pg_restore -x --no-owner -d <db name> backup.dump
使用以下命令恢復數據庫備份
pg_restore --no-privileges --no-owner -h localhost -p <DB_Port> -U <DB_User> -d <DB_Name>-1 <DB_Backup_Path>
使用標志--no-privileges來防止恢復訪問權限(授予/撤銷命令)和
--no-owner防止設置對象的所有權以匹配原始數據庫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.