欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PostgreSQL數(shù)據(jù)庫備份的幾種實現(xiàn)方法

 更新時間:2025年06月15日 11:13:35   作者:雪花凌落的盛夏  
本文主要介紹了PostgreSQL數(shù)據(jù)庫備份的幾種實現(xiàn)方法,包括pg_dump和pg_dumpall是PostgreSQL備份工具,前者備份單數(shù)據(jù)庫,后者備份整個集群,感興趣的可以了解一下

pg_dump 和 pg_dumpall

在 PostgreSQL 中,pg_dump 和 pg_dumpall 是兩個常用的備份工具,分別用于邏輯備份單個數(shù)據(jù)庫和整個數(shù)據(jù)庫集群。

檢查pg_dump 和 pg_dumpall命令是否可用

su - postgres
pg_dump --version
pg_dumpall --version

在這里插入圖片描述

使用 pg_dump 備份單個數(shù)據(jù)庫

pg_dump -U <用戶名> -h <主機名> -p <端口號> -F <格式> -f <輸出文件路徑> <數(shù)據(jù)庫名>

參數(shù):

  • -U:指定數(shù)據(jù)庫用戶名。
  • -h:指定數(shù)據(jù)庫主機地址(默認 localhost)。
  • -p:指定數(shù)據(jù)庫端口(默認 5432)。
  • -F:指定備份格式:
    • plain(默認):生成 SQL 腳本文件。
    • c:自定義格式(支持壓縮,需用 pg_restore 恢復(fù))。
    • d:目錄格式(支持并行備份)。
    • t:tar 格式。
  • -f:指定輸出文件路徑。
  • --schema-only:僅備份表結(jié)構(gòu)(不包含數(shù)據(jù))。
  • --data-only:僅備份數(shù)據(jù)(不包含表結(jié)構(gòu))。
  • -t <表名>:備份特定表。
  • -j <并行任務(wù)數(shù)>:并行備份(適用于大數(shù)據(jù)庫)。

示例

備份整個數(shù)據(jù)庫為 SQL 文件:

pg_dump -U postgres -h localhost -p 5432 -F p -f /path/to/backup.sql mydb
  • -F p 表示輸出為普通 SQL 文件。
  • mydb 是要備份的數(shù)據(jù)庫名。

備份整個數(shù)據(jù)庫為自定義格式(支持壓縮):

pg_dump -U postgres -h localhost -p 5432 -F c -f /path/to/backup.custom mydb
  • -F c 表示輸出為自定義格式(需用 pg_restore 恢復(fù))。

備份特定表:

pg_dump -U postgres -h localhost -p 5432 -F p -t users -f /path/to/users_backup.sql mydb
  • -t users 表示僅備份 users 表。

僅備份表結(jié)構(gòu):

pg_dump -U postgres -h localhost -p 5432 --schema-only -f /path/to/schema.sql mydb

僅備份數(shù)據(jù)(不包含表結(jié)構(gòu)):

pg_dump -U postgres -h localhost -p 5432 --data-only -f /path/to/data.sql mydb

使用 pg_dumpall 備份整個數(shù)據(jù)庫集群

基本用法

pg_dumpall 用于備份整個 PostgreSQL 集群,包括所有數(shù)據(jù)庫、角色(用戶)、表空間等全局對象。

命令格式:

pg_dumpall -U <用戶名> -h <主機名> -p <端口號> -f <輸出文件路徑> [選項]

參數(shù):

  • -g:僅備份全局對象(角色、表空間等)。
  • -c:在備份中包含刪除數(shù)據(jù)庫的命令(用于恢復(fù)時清理舊數(shù)據(jù))。
  • -v:啟用詳細模式(顯示備份過程)。

示例

備份整個集群:

pg_dumpall -U postgres -h localhost -p 5432 -f /path/to/cluster_backup.sql
  • 生成的 SQL 文件包含所有數(shù)據(jù)庫、角色和表空間。

僅備份全局對象(角色、表空間):

pg_dumpall -U postgres -h localhost -p 5432 -g -f /path/to/globals.sql

備份整個集群并包含清理命令:

pg_dumpall -U postgres -h localhost -p 5432 -c -f /path/to/cluster_backup.sql

恢復(fù)備份

恢復(fù) pg_dump 備份

SQL 文件恢復(fù):

psql -U <用戶名> -h <主機名> -d <目標數(shù)據(jù)庫> -f <備份文件路徑>

示例:

psql -U postgres -h localhost -d mydb -f /path/to/backup.sql

自定義格式恢復(fù):

pg_restore -U <用戶名> -h <主機名> -d <目標數(shù)據(jù)庫> <備份文件路徑>

示例:

pg_restore -U postgres -h localhost -d mydb /path/to/backup.custom

恢復(fù) pg_dumpall 備份

恢復(fù)整個集群備份:

psql -U postgres -h localhost -d postgres -f /path/to/cluster_backup.sql

需以 postgres 用戶連接到默認數(shù)據(jù)庫(如 postgres),因為恢復(fù)過程中會創(chuàng)建其他數(shù)據(jù)庫。

恢復(fù)全局對象備份:

psql -U postgres -h localhost -d postgres -f /path/to/globals.sql

Tips

權(quán)限要求:

  • pg_dump 需要對目標數(shù)據(jù)庫有讀取權(quán)限。
  • pg_dumpall 需要超級用戶權(quán)限(以備份角色和表空間)。

備份格式選擇:

  • 如果需要靈活的恢復(fù)選項(如選擇性恢復(fù)表),建議使用自定義格式(-F c)。
  • 如果需要快速恢復(fù),SQL 文件可能更直接。

并行備份:

  • 對大型數(shù)據(jù)庫,使用 -j <并行任務(wù)數(shù)> 可加速備份(需 PostgreSQL 12+)

到此這篇關(guān)于PostgreSQL數(shù)據(jù)庫備份的幾種實現(xiàn)方法的文章就介紹到這了,更多相關(guān)PostgreSQL數(shù)據(jù)庫備份內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評論