我寫了一個 bat 腳本來執行 Postgres 備份和恢復工具。
我對恢復流程有一個小問題:只要我的資料庫存在,它就可以正常作業。但如果不這樣做,它將失敗。 我的恢復命令:
"pg_restore.exe" -d postgres://postgres:[email protected]:9195/mydb -w -c -v -F c --if-exists "DatabaseBackup_mydb.tar" 2>> "DatabaseRestore_mydb.log"
所以我需要以某種方式修改該命令,以處理資料庫“mydb”不存在的用例,并在這種情況下創建它。
在這種情況下,僅添加 -C 標志將不起作用。
有什么建議嗎?
uj5u.com熱心網友回復:
應該通過使用postgres://postgres:[email protected]:9195/postgres和添加-C. 顯然測驗扔掉的實體。這將連接到 postgres 資料庫DROP DATABASE IF EXISTS mydb;,然后CREATE DATABASE mydb連接到mydb然后恢復資料庫物件。
展示:
\l test_db
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------ ------- ---------- --------- ------- -------------------
(0 rows)
pg_restore -d postgres -c -C -U postgres test_db.out
pg_restore: while PROCESSING TOC:
pg_restore: from TOC entry 4734; 1262 1170111 DATABASE test_db postgres
pg_restore: error: could not execute query: ERROR: database "test_db" does not exist
Command was: DROP DATABASE test_db;
pg_restore: warning: errors ignored on restore: 1
\l test_db
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------- ---------- ---------- ------------- ------------- -------------------
test_db | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(1 row)
pg_restore -d postgres -c -C -U postgres test_db.out
\l test_db
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------- ---------- ---------- ------------- ------------- -------------------
test_db | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(1 row)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/326935.html
標籤:PostgreSQL 查询语句 恢复 转储 pg-restore
