目录:
基础操作之___数据导出与恢复
1、导出数据表(pg_dump)
导出表为txt文本
$ pg_dump -t httab -f httab.txt htdb
$ ll
-rw-rw-r--. 1 postgres postgres 429564 Aug 24 00:19 httab.txt
导出表为dmp文件
$ pg_dump -t httab -FC -f httab.dmp htdb
$ ll
-rw-rw-r--. 1 postgres postgres 243696 Aug 24 00:22 httab.dmp
导出表定义
$ pg_dump -t httab -s -f httab.metadata htdb
$ ll
-rw-rw-r--. 1 postgres postgres 1501 Aug 24 00:22 httab.metadata
参数说明:
- -t:指定表名
- -f:指定导出文件名
- -Fc:默认导出文件格式为文本,-Fc使用用户自定义格式
- -s:只导出表定义
注意:若想导出索引只能导出索引创建语句,不导出索引数据
2、恢复导出文件到数据库
根据导出格式不同,恢复命令也不相同
(1)恢复dmp格式文件(pg_restore)
删除当前数据库已有表
$ psql -c 'select count(1) from httab' -Uhtuser htdb
count
-------
10000
(1 row)
$ psql -c 'drop table httab' -Uhtuser htdb
DROP TABLE
将dmp格式文件恢复到数据库

本文详细介绍了PostgreSQL数据库的数据导出与恢复操作,包括使用pg_dump和pg_restore进行dmp和txt格式的文件导出与恢复,以及物理备份与恢复的完整流程。通过示例命令,展示了如何进行全库备份与快速恢复,确保数据安全。
1606

被折叠的 条评论
为什么被折叠?



