PostgreSQL 數據庫備份與恢復 pd_dump pg_restore


備份 PG 數據庫生成的文件可以有兩種,一種是 SQL 文件,一種是二進制文件,二進制文件只能使用 pg_restore 進行恢復。

 

PostgreSQL 數據庫操作簡要說明

PostgreSQL數據庫版本

psql --version
psql (PostgreSQL) 9.1.3

 

下面是在linux下的操作,在windows下面將su -postgres 換為

運行輸入cmd→d:→

cd D:\Program Files\PostgreSQL\9.2\bin,下面創建表、刪除表時,需要在postgres下操作,因此需要在創建刪除數據庫前面輸入psql -u postgres

一、數據庫備份

1、備份數據庫結構

su - postgres
pg_dump -Fc -s -f testdbschema.sql testdb

2、備份數據庫數據

su - postgres
pg_dump -Fc -a -f testdbdata.sql testdb

3、備份數據庫結構和數據

su - postgres
pg_dump -Fc -f testdbschemadata.sql testdb

4、備份數據庫中指定表結構

 pg_dump -Fc -s -t citycode -f citycode_schema.sql testdb

5、備份數據庫中指定表數據

 pg_dump -Fc -a -t citycode -f citycode_data.sql testdb

.6、備份數據庫中指定表(結構和數據)

 pg_dump -Fc -t citycode -f citycode_schemadata.sql testdb

二、刪除數據庫

su - postgres

dropdb testdb

三、恢復數據庫

1、創建新數據庫testdb

su - postgres

createdb testdb;


2、 恢復數據結構(only schema)

su - postgres

 pg_restore -s -d testdb testdbschema.sql 
 

3、恢復數據庫數據(only data)

su - postgres

pg_restore -a -d testdb testdbdata.sql

4、恢復數據庫結構和數據(schema and data)

su - postgres

pg_restore -d testdb testdbschemadata.sql

5、指定表數據恢復

1)刪除表

psql testdb

DROP TABLE citycode;

2)恢復表結構

pg_restore -s -t citycode -d testdb citycode_schema.sql

3)恢復表數據

pg_restore -a -t citycode -d testdb citycode_data.sql

4)恢復表(結構和數據)

pg_restore -t citycode -d testdb citycode_schemadata.sql

以上備份恢復相關操作可用於靜態(無數據增長)數據庫。

重要提示:pg_restore 可用來恢復pg_dump命令以 (Fc\Ft)格式備份的數據文件。執行pg_dump備份命令時若無此格式參數聲明,pg_restore 恢復時可能出現錯誤提示“pg_restore: [archiver] input file does not appear to be a valid archive”。

 

 

pg_dump 官方文檔

https://www.postgresql.org/docs/10/app-pgdump.html

pg_dumpall

https://www.postgresql.org/docs/10/app-pg-dumpall.html

pg_restore

https://www.postgresql.org/docs/10/app-pgrestore.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM