Docs/数据库/postgresql/导入导出数据.txt
2022-10-18 16:59:37 +08:00

45 lines
1.2 KiB
Text
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

1、导入整个数据库
# psql -U postgres(用户名) 数据库名(缺省时同用户名) < /data/dum.sql
2、导出整个数据库
# pg_dump -h localhost -U postgres(用户名) 数据库名(缺省时同用户名) >/data/dum.sql
3、导出某个表
# pg_dump -h localhost -U postgres(用户名) 数据库名(缺省时同用户名) -t table(表名) >/data/dum.sql
4、压缩方法
一般用dump导出数据会比较大推荐使用xz压缩
压缩方法 xz dum.sql 会生成 dum.sql.xz 的文件
5、xz压缩数据倒数数据库方法
xzcat /data/dum.sql.xz | psql -h localhost -U postgres(用户名) 数据库名(缺省时同用户名)
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” 表示包含创建语句
n
pg_restore /dbbak/c.dmp|less    可以解析二进制格式的备份文件
pg_restore -l /dbbak/c.dmp   
pg_restore -d testdb /dbbak/pgdumpbak/c.dmp   #需要先创建目标库
pg_restore -d postgres /dbbak/pgdumpbak/p.dmp  #文件中包含创建数据库的命令,不需要创建目标库