PostgreSQL提供的一个工具pg_dump,逻辑导出数据,生成sql文件或其他格式文件,pg_dump是一个客户端工具,可以远程或本地导出逻辑数据,恢复数据至导出时间点。
pg_dump 一次只转储一个数据库, 并且不会转储有关角色或表空间的信息 (因为那些是群集范围而不是每个数据库)。为支持方便地转储数据库群集的全部内容, 提供了 pg_dumpall 程序。pg_dumpall 在给定的群集中备份每个数据库, 并保留群集范围内的数据, 如角色和表空间定义。
PostgreSQL提供的一个工具pg_restore用来导入数据

1 具体用法参考help

$ pg_dump --help
pg_dump dumps a database as a text file or to other formats.
Usage:pg_dump [OPTION]... [DBNAME]
General options:-f, --file=FILENAME          output file or directory name-F, --format=c|d|t|p         output file format (custom, directory, tar,plain text (default))
......
$ pg_restore --help
pg_restore restores a PostgreSQL database from an archive created by pg_dump.
Usage:pg_restore [OPTION]... [FILE]
General options:-d, --dbname=NAME        connect to database name-f, --file=FILENAME      output file name-F, --format=c|d|t       backup file format (should be automatic)-l, --list               print summarized TOC of the archive-v, --verbose            verbose mode-V, --version            output version information, then exit-?, --help               show this help, then exit
......

2 导出

操作系统平面文件格式
$ pg_dump -h 192.168.6.16 -U highgo -p 5433 -Fp -t test_dump > /tmp/test_dump1.dmp highgo
自定义格式
$ pg_dump -h 192.168.6.16 -U highgo -p 5433 -Fc -t test_dump > /tmp/test_dump2.dmp highgo
tar包格式
$ pg_dump -h 192.168.6.16 -U highgo -p 5433 -Ft -t test_dump > /tmp/test_dump3.dmp highgo
自定义、0级压缩格式
$ pg_dump -h 192.168.6.16 -U highgo -p 5433 -Fc -Z 0 -t test_dump > /tmp/test_dump4.dmp highgo
自定义、9级压缩格式
$ pg_dump -h 192.168.6.16 -U highgo -p 5433 -Fc -Z 9 -t test_dump > /tmp/test_dump5.dmp highgo
显示信息内容
$ pg_dump -h 192.168.6.16 -U highgo -p 5433 -Fc -v -t test_dump > /tmp/test_dump6.dmp highgo

验证不同格式的文件

# ll
-rw-rw-r-- 1 pg   pg   379738 Nov  1 15:05 test_dump1.dmp
-rw-rw-r-- 1 pg   pg   217142 Nov  1 15:05 test_dump2.dmp
-rw-rw-r-- 1 pg   pg   384512 Nov  1 15:05 test_dump3.dmp
-rw-rw-r-- 1 pg   pg   429852 Nov  1 15:08 test_dump4.dmp
-rw-rw-r-- 1 pg   pg   217142 Nov  1 15:07 test_dump5.dmp
压缩格式的文件明显较小不同格式的文件
# file test_dump1.dmp
test_dump1.dmp: ASCII text
# file test_dump2.dmp
test_dump2.dmp: PostgreSQL custom database dump - v1.13-0
# file test_dump3.dmp
test_dump3.dmp: POSIX tar archive
# file test_dump4.dmp
test_dump4.dmp: PostgreSQL custom database dump - v1.13-0
注意1:默认是-Fp格式(操作系统平面文件)
注意2:并行导出只支持-Fd 目录格式$ pg_dump -h 192.168.6.16 -U highgo -p 5433 -Fc -v -Z 1 -t test_dump > /tmp/test_dump3.dmp highgo
pg_dump: parallel backup only supported by the directory format

3 导入

导入前创建table所需的role
CREATE ROLE highgo;
ALTER ROLE highgo WITH NOSUPERUSER INHERIT LOGIN NOCREATEROLE NOCREATEDB NOREPLICATION PASSWORD 'highgo@123';

pg_restore -h 127.0.0.1 -d postgres -p 5432 -v /tmp/highgo_table.dmp

case:

create user highgo with password 'Highgo@123';
create schema highgo AUTHORIZATION highgo;
create table test_dump (id int,name text,tim timestamp);
insert into test_dump select generate_series(1,100),md5(random()::text),clock_timestamp();postgres=>  select schemaname,tablename,tableowner from pg_tables where tablename='test_dump';schemaname | tablename | tableowner
------------+-----------+------------test       | test_dump | testhighgo     | test_dump | highgo
(2 rows)1
pg_dump -h 127.0.0.1 -U highgo -p 5432 -Fc -v -t test_dump > /tmp/highgo_table.dmp postgres
drop table highgo_table;2
CREATE ROLE highgo;
ALTER ROLE highgo WITH NOSUPERUSER INHERIT LOGIN NOCREATEROLE NOCREATEDB NOREPLICATION PASSWORD 'highgo@123';
\c highgo highgo[pg@pg tmp]$ pg_restore -h 127.0.0.1  -d postgres -p 5432  -v /tmp/highgo_table.dmp
pg_restore: connecting to database for restore
pg_restore: creating TABLE "highgo.test_dump"
pg_restore: processing data for table "highgo.test_dump"

导入失败情况
$ pg_restore -h 127.0.0.1 -d postgres -p 5432 -U pg  -v /tmp/test_dump1.dmp
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
注:pg_resotre仅支持Fc/Ft格式的导出文件,Fp格式的文件是sql脚本,需要使用psql工具导入脚本数据
如:
postgres=# i /tmp/test_dump1.dmp

Postgresql pg_dumppg_restore用法相关推荐

  1. PostgreSQL REGEXP_REPLACE用法及代码示例

    PostgreSQL REGEXP_REPLACE用法及代码示例: 原文链接:https://vimsky.com/examples/usage/postgresql-regexp_replace-f ...

  2. PostgreSQL hint用法(兼容oracle)

    Hint是Oracle数据库中很有特色的一个功能,pg和oracle的优化器一样也是CBO(基于成本的).但是在pg中是没有hint的,但是我们可以去通过设置成本因子,设置优化器开关等方式来" ...

  3. PostgreSQL DISTINCT用法

    2019独角兽企业重金招聘Python工程师标准>>> PostgreSQL的DISTINCT关键字用于与SELECT语句消除所有重复的记录,并获取唯一记录.有可能的情况下,当你有多 ...

  4. postgresql PREPARE 用法

    Name PREPARE -- 创建一个准备好的查询 Synopsis PREPARE plan_name [ (datatype [, ...] ) ] AS statement 描述 PREPAR ...

  5. PostgreSql pg_dump用法

    语法 pg_dump [connection-option...] [option...] [dbname] pg_dump 连接选项参数如下: -h host 或 --host=host:指定运行服 ...

  6. PostgreSql pg_restore 用法

    一.概述 pg_restore 是一个用来从 pg_dump 创建的非文本格式文件中恢复 PostgreSQL 数据库的工具. 二.语法 pg_restore [connection-option] ...

  7. postgreSQl pathman 用法语句总结

    2019独角兽企业重金招聘Python工程师标准>>> --新建主表 create table part_test(id int, info text, crt_time times ...

  8. postgresql分页用法_postgresql分页数据重复问题的深入理解

    问题背景 许多开发和测试人员都可能遇到过列表的数据翻下一页的时候显示了上一页的数据,也就是翻页会有重复的数据. 如何处理? 这个问题出现的原因是因为选择的排序字段有重复,常见的处理办法就是排序的时候加 ...

  9. postgresql数据库的数据导出

    一.pg_dump的用法: 数据库的导入导出是最常用的功能之一,每种数据库都提供有这方面的工具,例如Oracle的exp/imp,Informix的dbexp/dbimp,MySQL的mysqldum ...

  10. postgresql导入mysql的备份_PostgreSQL数据备份、导入转

    一.pg_dump的用法: 数据库的导入导出是最常用的功能之一,每种数据库都提供有这方面的工具,例如Oracle的exp/imp,Informix的dbexp/dbimp,MySQL的mysqldum ...

最新文章

  1. 暂无支持此机型的手机版本_华为AR地图发布重要更新版本 新增对8款机型的支持...
  2. hibernate中一对多关系的映射
  3. 微型计算机基础知识答案,计算机基础知识(答案已填)
  4. Nginx与Serssion一致性问题
  5. BZOJ2299 [HAOI2011]向量 【裴蜀定理】
  6. html 元素的属性
  7. 当我们群嘲假博士时,不要忘了真博士们的艰辛
  8. 图片测量尺寸软件_3D扫描之工件测量检测
  9. 为什么需要 AtomicInteger 原子操作类?
  10. 可蠕虫 DarkRadiation 勒索软件瞄准 Linux 和 Docker 实例
  11. JXSE 2.5 : What's Cool #6 -- PeerGroup Executor and ScheduledExcutor
  12. robocopy 备份_windows下使用RoboCopy命令进行文件夹增量备份
  13. ORA-12547: TNS:lost contact 问题处理
  14. pdfjs 使用viewer 实现自动翻页(js控制)
  15. 嵌入式系统词汇表(收藏)
  16. Google被封事件真相
  17. [BZOJ1502] [NOI2005]月下柠檬树
  18. 最近的心路历程非常之多
  19. python一次性封装多条sql语句(begin end)
  20. 童鞋们有福了,U880 GPS使用指南终于找到了,不看后悔

热门文章

  1. Atitit 信息系统安全法 目录 1. 常见的安全保护目标 1 2. WEB安全风险行为 2 2.1. Injection 2 2.2. Broker Authentication损坏的身份验证
  2. 常见方案 目录 1. 发现目前 WEB 上主流的视频直播方案有 HLS 和 RTMP, 1 2. 实现直播的方法有很多,但是常用的,就这几个。 3个直播协议:rtmp、rtsp、hls。 和三个端:
  3. Atitit zip解压文件 java use apache ant.jar C:\0wkspc\hislog\src\main\java\com\attilax\compress\ZipUt
  4. atitit.错误:找不到或无法加载主类 的解决 v4 qa15.doc 艾提拉总结 attilax总结 1.1. 修改此java文件,让他启动编译,还是不能生成了新的class, 1 1.2. 查
  5. 2020年度SaaS企业 TOP100
  6. WSL:ssh 本地与阿里云数据交互
  7. 港交所上新衍生工具:“界内证”——交易规则与投资价值全解析
  8. Julia: 关于1.0的Array的变化
  9. 堪比黑石 花旗、摩根大通的股东 揭开贝莱德的神秘面纱
  10. Rust: codewars 的Count of positives / sum of negatives