亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

pg_dump 和 pg_dumpall

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

檢查pg_dump 和 pg_dumpall命令是否可用

su - postgres
pg_dump --version
pg_dumpall --version

在這里插入圖片描述

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

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

參數(shù):

  • -U:指定數(shù)據(jù)庫(kù)用戶名。
  • -h:指定數(shù)據(jù)庫(kù)主機(jī)地址(默認(rèn) localhost)。
  • -p:指定數(shù)據(jù)庫(kù)端口(默認(rèn) 5432)。
  • -F:指定備份格式:
    • plain(默認(rèn)):生成 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ù)庫(kù))。

示例

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

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

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

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 備份整個(gè)數(shù)據(jù)庫(kù)集群

基本用法

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

命令格式:

pg_dumpall -U <用戶名> -h <主機(jī)名> -p <端口號(hào)> -f <輸出文件路徑> [選項(xiàng)]

參數(shù):

  • -g:僅備份全局對(duì)象(角色、表空間等)。
  • -c:在備份中包含刪除數(shù)據(jù)庫(kù)的命令(用于恢復(fù)時(shí)清理舊數(shù)據(jù))。
  • -v:?jiǎn)⒂迷敿?xì)模式(顯示備份過(guò)程)。

示例

備份整個(gè)集群:

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

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

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

備份整個(gè)集群并包含清理命令:

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

恢復(fù)備份

恢復(fù) pg_dump 備份

SQL 文件恢復(fù):

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

示例:

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

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

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

示例:

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

恢復(fù) pg_dumpall 備份

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

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

需以 postgres 用戶連接到默認(rèn)數(shù)據(jù)庫(kù)(如 postgres),因?yàn)榛謴?fù)過(guò)程中會(huì)創(chuàng)建其他數(shù)據(jù)庫(kù)。

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

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

Tips

權(quán)限要求:

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

備份格式選擇:

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

并行備份:

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

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

相關(guān)文章

最新評(píng)論