PostgreSQL 資料庫備份與還原
目錄
- 備份
- 還原
- 栗子
- 拓展
備份
PostgreSQL提供的一個工具pg_dump,邏輯匯出資料,生成sql檔案或其他格式檔案,pg_dump是一個客戶端工具,可以遠程或本地匯出邏輯資料,恢復資料至匯出時間點,
-
Usage:
pg_dump [option]... [dbname]
note:
dbname如果沒有提供資料庫名字, 那么使用PGDATABASE環境變數的數值. -
Options
-
General options:(一般選項)
-f, --file=FILENAME 輸出檔案或目錄名 -F, --format=c|d|t|p 輸出檔案格式 (定制, 目錄, tar) 明文 (默認值)) -j, --jobs=NUM 執行多個并行任務進行備份轉儲作業 -v, --verbose 詳細模式 -V, --version 輸出版本資訊,然后退出 -Z, --compress=0-9 被壓縮格式的壓縮級別 --lock-wait-timeout=TIMEOUT 在等待表鎖超時后操作失敗 -?, --help 顯示此幫助, 然后退出 -
Options controlling the output content:(控制輸出內容選項:)
-a, --data-only 只轉儲資料,不包括模式 -b, --blobs 在轉儲中包括大物件 -c, --clean 在重新創建之前,先清除(洗掉)資料庫物件 -C, --create 在轉儲中包括命令,以便創建資料庫 -E, --encoding=ENCODING 轉儲以ENCODING形式編碼的資料 -n, --schema=SCHEMA 只轉儲指定名稱的模式 -N, --exclude-schema=SCHEMA 不轉儲已命名的模式 -o, --oids 在轉儲中包括 OID -O, --no-owner 在明文格式中, 忽略恢復物件所屬者 -s, --schema-only 只轉儲模式, 不包括資料 -S, --superuser=NAME 在明文格式中使用指定的超級用戶名 -t, --table=TABLE 只轉儲指定名稱的表 -T, --exclude-table=TABLE 不轉儲指定名稱的表 -x, --no-privileges 不要轉儲權限 (grant/revoke) --binary-upgrade 只能由升級工具使用 --column-inserts 以帶有列名的INSERT命令形式轉儲資料 --disable-dollar-quoting 取消美元 (符號) 引號, 使用 SQL 標準引號 --disable-triggers 在只恢復資料的程序中禁用觸發器 --enable-row-security 啟用行安全性(只轉儲用戶能夠訪問的內容) --exclude-table-data=https://www.cnblogs.com/librarookie/p/TABLE 不轉儲指定名稱的表中的資料 --if-exists 當洗掉物件時使用IF EXISTS --inserts 以INSERT命令,而不是COPY命令的形式轉儲資料 --no-security-labels 不轉儲安全標簽的分配 --no-synchronized-snapshots 在并行作業集中不使用同步快照 --no-tablespaces 不轉儲表空間分配資訊 --no-unlogged-table-data 不轉儲沒有日志的表資料 --quote-all-identifiers 所有識別符號加引號,即使不是關鍵字 --section=SECTION 備份命名的節 (資料前, 資料, 及 資料后) --serializable-deferrable 等到備份可以無例外運行 --snapshot=SNAPSHOT 為轉儲使用給定的快照 --strict-names 要求每個表和/或schema包括模式以匹配至少一個物體 --use-set-session-authorization 使用 SESSION AUTHORIZATION 命令代替 ALTER OWNER 命令來設定所有權 -
Connection options:(聯接選項:)
-d, --dbname=DBNAME 對資料庫 DBNAME備份 -h, --host=主機名 資料庫服務器的主機名或套接字目錄 -p, --port=埠號 資料庫服務器的埠號 -U, --username=名字 以指定的資料庫用戶聯接 -w, --no-password 永遠不提示輸入口令 -W, --password 強制口令提示 (自動) --role=ROLENAME 在轉儲前運行SET ROLE -
PG_DUMP 檔案
-
PG_DUMP 中文檔案
-
還原
-
Usage:
psql [option]... [dbname [username]]
pg_restore [option]... [file]
note: 還原前需要創建 table 所需的 role;
-
Options
psql
- PSQL 檔案
- PSQL 中文檔案
pg_restore
- PG_RESTORE 檔案
- PG_RESTORE 中文檔案
栗子
-
備份資料庫
-
還原資料庫
-
基礎引數
Option Value -h--host=HOSTNAME資料庫服務器IP或主機名 -p--port=PORT資料庫埠號 -U--username=NAME用戶名 -d--dbname=DBNAME資料庫名 -f--file=FILENAME指定匯出檔案名(也可以使用 <>)-v--verbose詳細模式(列印操作程序) -
常用引數
Option Value -F--format=c|d|t|p輸出檔案格式 (定制, 目錄, tar, 明文 (默認值)) -c--clean在重新創建之前,先清除(洗掉)資料庫物件 -C--create在轉儲中包括創庫命令,以便創建資料庫 -E--encoding=ENCODING轉儲以ENCODING形式編碼的資料 -n--schema=SCHEMA只轉儲指定名稱的模式 -N--exclude-schema=SCHEMA不轉儲指定名稱的模式 -t--table=TABLE只轉儲指定名稱的表 -T--exclude-table=TABLE不轉儲指定名稱的表 -s--schema-only只轉儲模式, 不包括資料(不匯出資料) -Z--compress=0-9指定壓縮格式的壓縮級別(0-9)
note:
- 操作遠程資料庫時必須加上
-h-p引數; - 還原時,不指定資料庫默認還原到
postgres庫(環境變數中的PGDATABASE); --if-exists當洗掉物件時使用IF EXISTS(配合-c引數使用)-c洗掉原資料庫物件(備份檔案中有洗掉原庫的SQL)-C創建資料庫(備份檔案中有創建原庫名的新資料庫的SQL)-c引數: 還原程序中會先洗掉原資料庫;- 使用
-C引數時,創建資料庫并備份所有模式,還原時無需指定資料庫,自動創建原庫的同名庫,并把所有模式還原其中; - 不使用
-C引數時,只備份所有模式,還原時需要指定資料庫;* -C 在轉儲中創建資料庫的命令,還原時無需在匯入之前先建資料庫;
備份資料庫
-
常用備份
pg_dump [-h host -p 5432] -U username [-c -C] -d db_name -f db_backup.sql [-v]
pg_dump [-h host -p 5432] -U username [-c -C] -d db_name > db_backup.sql [-v]
-
備份歸檔格式
-F c|d|t|p(默認為p)pg_dump [-h host -p 5432] -U username [-c -C] -F c -d db_name -f db_backup.sql [-v]
-
備份使用指定壓縮級別
-Z 0-9pg_dump [-h host -p 5432] -U username -F c -d db_name -f db_backup.sql [-v]
note:
0不壓縮,1-9壓縮級別;-Fc對于自定義歸檔格式,這會指定個體表資料段的壓縮,并且默認是進行中等級別的壓縮;-Fp對于純文本輸出,設定一個非零壓縮級別會導致整個輸出檔案被壓縮,就好像它被gzip處理過一樣,但是默認是不壓縮;-Fttar 歸檔格式當前完全不支持壓縮;
-
備份指定模式(schema)
-n-Npg_dump [-h host -p 5432] -U username -d db_name -n schema_name [-n schema_name2]... -f db_schema.sql [-v]
pg_dump [-h host -p 5432] -U username [-c -C] -d db_name -N schema_name [-N schema_name2]... -f db_exclude-schema.sql [-v]
-
備份指定物件(table, view, etc)
-t-Tpg_dump [-h host -p 5432] -U username -d db_name [-n schema_name] -t tb_name [-t tb_name2]... -f db_table.sql [-v]
pg_dump [-h host -p 5432] -U username -d db_name [-n schema_name] -T tb_name [-T tb_name2]... -f db_exclude-table.sql [-v]
note: 匯出表
username為 表的所有者 -
只備份模式(只備份所有模式,不備份資料)
-spg_dump [-h host -p 5432] -U username [-c -C] -d db_name -s -f db_only_schema_backup.sql [-v]
還原資料庫
-
還原
psql [-h host -p 5432] -U username -d db_name -f db_backup.sql
psql [-h host -p 5432] -U username -d db_name < db_backup.sql
-
恢復
pg_restore [-h host -p 5432] -U username -d db_name -f db_backup.tar
note:
- 錯誤日志: pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
- pg_resotre僅支持Fc/Ft格式的匯出檔案,Fp格式的檔案是sql腳本,需要使用psql工具匯入腳本資料
-
修改所有表
OWNER(postgres用戶 除外)REASSIGN OWNED BY old_role [, ...] TO new_role
- REASSIGN OWNED 檔案
- REASSIGN OWNED 中文檔案
拓展
- PostgreSQL 資料庫備份腳本
Reference
- PostgreSQL 官網手冊
- PostgreSQL 中文手冊
- https://blog.csdn.net/qq_31156277/article/details/90374872
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/447141.html
標籤:PostgreSQL
上一篇:MySQL8.0其他新特性
