原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處、作者信息和本聲明。否則將追究法律責任。http://hehehe.blog.51cto.com/205455/892080
PostgreSQL 數據庫操作簡要說明
PostgreSQL數據庫版本
psql --version
psql (PostgreSQL) 9.1.3
一、數據庫備份
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”。