溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在PostgreSQL中利用pgdump進行備份恢復(fù)

發(fā)布時間:2020-12-29 14:08:24 來源:億速云 閱讀:213 作者:Leah 欄目:開發(fā)技術(shù)

怎么在PostgreSQL中利用pgdump進行備份恢復(fù)?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

邏輯備份在恢復(fù)時,介于邏輯備份與故障時間點之間的數(shù)據(jù)難以恢復(fù),故一般不采取邏輯備份方式進行數(shù)據(jù)庫備份,但邏輯適用于跨平臺跨版本的數(shù)據(jù)遷移;

邏輯備份恢復(fù)主要以下三種:

pg_dump

pg_dumpall

copy

本小節(jié)主要講解pg_dump

pg_dump備份

只能備份單個數(shù)據(jù)庫,不會導(dǎo)出角色和表空間相關(guān)的信息

-F c 備份為二進制格式,壓縮存儲.并且可被pg_restore用于精細(xì)還原

-F p 備份為文本,大庫不推薦

pg_dump恢復(fù)

psql dbname -U username < bakfile

pg_restore
– pg_restore [option] ... [filename]
– pg_restore -d dbname bakfile

二進制格式的備份只能使用pg_restore來還原,可以指定還原的表,編輯TOC文件,定制還原的順序,表, 索引等。

文本格式的備份還原, 直接使用用戶連接到對應(yīng)的數(shù)據(jù)庫執(zhí)行備份文本即可,例如psql dbname -f bak.sql

pg_dump備份恢復(fù)示例

1)創(chuàng)建數(shù)據(jù)庫

createdb testdb

2)連入數(shù)據(jù)庫testdb

psql testdb

3)創(chuàng)建測試表,插入數(shù)據(jù)

testdb=# create table tt(a int) tablespace tbls_t;
testdb=# insert into tt(a) values(1);
testdb=# insert into tt(a) values(2);

4)查看數(shù)據(jù)

testdb=# select * from tt;

5)備份

pg_dump testdb>/dbbak/testdb.sql #簡單語法,可結(jié)合選項靈活備份

6)刪除數(shù)據(jù)庫testdb

dropdb testdb

7)創(chuàng)建新數(shù)據(jù)庫(恢復(fù)之前需創(chuàng)建數(shù)據(jù)庫)

createdb testdb

8)恢復(fù)數(shù)據(jù)

psql testdb </dbbak/testdb.sql

9)查看數(shù)據(jù)是否回復(fù)

psql testdb

testdb=# select * from tt;

至此,數(shù)據(jù)已成功恢復(fù)!

pg_restore -d postgres /dbbak/pgdumpbak/p.dmp

pg_dump備份恢復(fù)命令擴展練習(xí)

pg_dump -F c -f /dbbak/pgdumpbak/c.dmp -C -E UTF8 -h 127.0.0.1 -U postgres testdb  #二進制格式備份文件 
pg_dump -F p -f /dbbak/pgdumpbak/p.dmp -C -E UTF8 -h 127.0.0.1 -U postgres testdb #文本格式備份文件,”-C” 表示包含創(chuàng)建語句
pg_restore /dbbak/c.dmp|less 可以解析二進制格式的備份文件
pg_restore -l /dbbak/c.dmp 
pg_restore -d testdb /dbbak/pgdumpbak/c.dmp #需要先創(chuàng)建目標(biāo)庫
pg_restore -d postgres /dbbak/pgdumpbak/p.dmp #文件中包含創(chuàng)建數(shù)據(jù)庫的命令,不需要創(chuàng)建目標(biāo)庫

toc文件選擇性備份恢復(fù)

1)根據(jù)二進制備份文件生成toc文件

pg_restore -l -f /dbbak/pgdumpbak/toc /dbbak/pgdumpbak/c.dmp

2)修改 toc文件,以首行加分號“;”的方式注釋掉不用還原的內(nèi)容

3)以toc文件列表做恢復(fù)

pg_restore -F c -L /dbbak/pgdumpbak/toc -d testdb /dbbak/pgdumpbak/c.dmp

補充:Postgresql備份與還原命令pg_dump

postgresql數(shù)據(jù)庫的備份和還原命令pg_dump

常用命令:

備份:

pg_dump -U postgres -d myDBname -f dump.sql

其中

postgres是用戶名

myDBname是數(shù)據(jù)庫名

dump.sql是文件名

還原:

createdb newDBname

psql -d newDBname -U postgres -f dump.sql

其中

postgres是用戶名

newDBname是數(shù)據(jù)庫名

dump.sql是文件名

參考:

pg_dump 把一個數(shù)據(jù)庫轉(zhuǎn)儲為純文本文件或者是其它格式.
用法:
 pg_dump [選項]... [數(shù)據(jù)庫名字]
一般選項:
 -f, --file=FILENAME   輸出文件或目錄名
 -F, --format=c|d|t|p   輸出文件格式 (定制, 目錄, tar)
        明文 (默認(rèn)值))
 -j, --jobs=NUM    執(zhí)行多個并行任務(wù)進行備份轉(zhuǎn)儲工作
 -v, --verbose    詳細(xì)模式
 -V, --version    輸出版本信息,然后退出
 -Z, --compress=0-9   被壓縮格式的壓縮級別
 --lock-wait-timeout=TIMEOUT 在等待表鎖超時后操作失敗
 -?, --help     顯示此幫助, 然后退出
控制輸出內(nèi)容選項:
 -a, --data-only    只轉(zhuǎn)儲數(shù)據(jù),不包括模式
 -b, --blobs     在轉(zhuǎn)儲中包括大對象
 -c, --clean     在重新創(chuàng)建之前,先清除(刪除)數(shù)據(jù)庫對象
 -C, --create     在轉(zhuǎn)儲中包括命令,以便創(chuàng)建數(shù)據(jù)庫
 -E, --encoding=ENCODING  轉(zhuǎn)儲以ENCODING形式編碼的數(shù)據(jù)
 -n, --schema=SCHEMA   只轉(zhuǎn)儲指定名稱的模式
 -N, --exclude-schema=SCHEMA 不轉(zhuǎn)儲已命名的模式
 -o, --oids     在轉(zhuǎn)儲中包括 OID
 -O, --no-owner    在明文格式中, 忽略恢復(fù)對象所屬者
 -s, --schema-only   只轉(zhuǎn)儲模式, 不包括數(shù)據(jù)
 -S, --superuser=NAME   在明文格式中使用指定的超級用戶名
 -t, --table=TABLE   只轉(zhuǎn)儲指定名稱的表
 -T, --exclude-table=TABLE 不轉(zhuǎn)儲指定名稱的表
 -x, --no-privileges   不要轉(zhuǎn)儲權(quán)限 (grant/revoke)
 --binary-upgrade    只能由升級工具使用
 --column-inserts    以帶有列名的INSERT命令形式轉(zhuǎn)儲數(shù)據(jù)
 --disable-dollar-quoting  取消美元 (符號) 引號, 使用 SQL 標(biāo)準(zhǔn)引號
 --disable-triggers   在只恢復(fù)數(shù)據(jù)的過程中禁用觸發(fā)器
 --enable-row-security  啟用行安全性(只轉(zhuǎn)儲用戶能夠訪問的內(nèi)容)
 --exclude-table-data=TABLE 不轉(zhuǎn)儲指定名稱的表中的數(shù)據(jù)
 --if-exists    當(dāng)刪除對象時使用IF EXISTS
 --inserts     以INSERT命令,而不是COPY命令的形式轉(zhuǎn)儲數(shù)據(jù)
 --no-security-labels   不轉(zhuǎn)儲安全標(biāo)簽的分配
 --no-synchronized-snapshots 在并行工作集中不使用同步快照
 --no-tablespaces    不轉(zhuǎn)儲表空間分配信息
 --no-unlogged-table-data  不轉(zhuǎn)儲沒有日志的表數(shù)據(jù)
 --quote-all-identifiers  所有標(biāo)識符加引號,即使不是關(guān)鍵字
 --section=SECTION   備份命名的節(jié) (數(shù)據(jù)前, 數(shù)據(jù), 及 數(shù)據(jù)后)
 --serializable-deferrable 等到備份可以無異常運行
 --snapshot=SNAPSHOT   為轉(zhuǎn)儲使用給定的快照
 --strict-names    要求每個表和/或schema包括模式以匹配至少一個實體
 --use-set-session-authorization
        使用 SESSION AUTHORIZATION 命令代替
    ALTER OWNER 命令來設(shè)置所有權(quán)
聯(lián)接選項:
 -d, --dbname=DBNAME  對數(shù)據(jù)庫 DBNAME備份
 -h, --host=主機名  數(shù)據(jù)庫服務(wù)器的主機名或套接字目錄
 -p, --port=端口號  數(shù)據(jù)庫服務(wù)器的端口號
 -U, --username=名字  以指定的數(shù)據(jù)庫用戶聯(lián)接
 -w, --no-password  永遠(yuǎn)不提示輸入口令
 -W, --password   強制口令提示 (自動)
 --role=ROLENAME   在轉(zhuǎn)儲前運行SET ROLE
如果沒有提供數(shù)據(jù)庫名字, 那么使用 PGDATABASE 環(huán)境變量
的數(shù)值.
報告錯誤至 <pgsql-bugs@postgresql.org>.

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI