因为业务需求,需要将服务器上的postgre多个数据库的数据整个库得迁移到另一个postgre数据库上。

一般表较少时,会使用postgre 的copy to 和 copy from 命令就能完成表的迁移,但这种方式需要target_database 上提前先创建好对应的表,并且每一个表都需要一次copy to 和copy from操作,当表比较多的时候,非常繁琐。
因此我查询了网上的方法,发现了pg_dump这个方法,但是网上的描述比较杂乱,因此我查询了postgresql的官方手册https://www.postgresql.org/docs/9.2/app-pgdump.html
使用pg_dump和pg_restore可以非常快速进行整个database的数据迁移或者备份。
以下是pg_dump的部分选项,pg_restore相似:
 -F format
--format=format
Selects the format of the output. format can be one of the following:
p
plain
Output a plain-text SQL script file (the default).
c
custom
Output a custom-format archive suitable for input into pg_restore. Together with the directory output format, this is the most flexible output format in that it allows manual selection and reordering of archived items during restore. This format is also compressed by default.
d
directory
Output a directory-format archive suitable for input into pg_restore. This will create a directory with one file for each table and blob being dumped, plus a so-called Table of Contents file describing the dumped objects in a machine-readable format that pg_restore can read. A directory format archive can be manipulated with standard Unix tools; for example, files in an uncompressed archive can be compressed with the gzip tool. This format is compressed by default.
t
tar
Output a tar-format archive suitable for input into pg_restore. The tar format is compatible with the directory format: extracting a tar-format archive produces a valid directory-format archive. However, the tar format does not support compression. Also, when using tar format the relative order of table data items cannot be changed during restore. -C
--create
Begin the output with a command to create the database itself and reconnect to the created database. (With a script of this form, it doesn't matter which database in the destination installation you connect to before running the script.) If --clean is also specified, the script drops and recreates the target database before reconnecting to it.
This option is only meaningful for the plain-text format. For the archive formats, you can specify the option when you call pg_restore.
-E encoding
--encoding=encoding
Create the dump in the specified character set encoding. By default, the dump is created in the database encoding. (Another way to get the same result is to set the PGCLIENTENCODING environment variable to the desired dump encoding.) -O
--no-owner
Do not output commands to set ownership of objects to match the original database. By default, pg_dump issues ALTER OWNER or SET SESSION AUTHORIZATION statements to set ownership of created database objects. These statements will fail when the script is run unless it is started by a superuser (or the same user that owns all of the objects in the script). To make a script that can be restored by any user, but will give that user ownership of all the objects, specify -O.
This option is only meaningful for the plain-text format. For the archive formats, you can specify the option when you call pg_restore.
 
举例:
pg_dump  -Fc dm  -O  > dm.dump
需要先su到有操作postgresql权限的用户,否则则需加上指定-h IP -U username
将对名为dm的database 以自定义的的方式并且忽略掉原数据库的owner进行dump。
pg_dump默认dump文件到当前user的的home目录下
pg_dump内部使用的copy命令,速度还比较快,几个G的数据20多分钟就能dump完
 
然后将dm.dump文件用FileZilla Client拷贝到target服务器上
因为在目标服务器上没有有操作postgresql权限的用户,所以需加上指定-h IP -U username
pg_restore -O -h IP -U username -d dm dm.dump
这样就能将数据库迁移到目标服务器上了,这里目标服务器已经有了dm数据库,若没有需要加上-C选项创建数据库。
-O的作用就是能将restore到目标服务上数据库的表的owner更改成目标服务上数据库的owner。
 

postgresql使用pg_dump和pg_restore 实现跨服务器的数据库迁移或备份的更多相关文章

  1. SQL Server跨服务器的数据库迁移

    1. 使用sql server task中back up 任务,保存为*.bak 文件. 2. 在另一个server中restore database,如果已经存在这个database,会覆盖之前的数 ...

  2. SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)

    SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery) 1.启用Ad Hoc Distributed Queries 在使用openrowset ...

  3. SQL Server跨服务器操作数据库

    今天给大家来分享一下跨服务器操作数据库,还是以SQL Server的管理工具(SSMS)为平台进行操作. 什么是跨服务器操作? 跨服务器操作就是可以在本地连接到远程服务器上的数据库,可以在对方的数据库 ...

  4. SQL跨服务器查询数据库

    有时候一个项目需要用到两个数据库或多个数据库而且这些数据库在不同的服务器上时,就需要通过跨服务器查找数据 在A服务器的数据库a查询服务器B的数据库b 的bb表 假如服务器B的IP地址为:10.0.22 ...

  5. postgresql跨服务器复制数据库

    假设名为dbname数据库需要从A服务器拷贝到B服务器 接收服务器B postgres用户 需先重置B服务器postgres系统用户的密码,使之与数据库用户postgres一致: passwd -d ...

  6. sqlserver 添加服务器链接 跨服务器访问数据库

    转载地址1:https://www.cnblogs.com/wanshutao/p/4137994.html //创建服务器链接 转载地址2:https://www.cnblogs.com/xulel ...

  7. [转]SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)

    正 文: 1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因 ...

  8. SQL Server 跨服务器 不同数据库之间复制表的数据

    不同数据库之间复制表的数据的方法: 当表目标表存在时: insert into 目的数据库..表 select * from 源数据库..表 当目标表不存在时: select * into 目的数据库 ...

  9. mysql 跨服务器复制数据库

    比较了下,还是采用ssh的方式最简单.比如传数据库test_db mysqldump --databases test_db| ssh 121.121.121.121 test_db

随机推荐

  1. D3.js 动画 过渡效果 (V3版本)

    一 . 过渡的启动   启动过渡效果,与以下四个方法相关:   d3.transition([selection],[name]) //创建一个过渡对象.但是由于每个选择集中都有transition( ...

  2. windows server 常用功能(一)

    最近做了一个windows server 2016的环境,也遇到了很多问题,作为一个新手,又没有很好的记录下解决方案,因为写这篇文章的时间有点晚,因此只能留下一些思路以供参考. 1.作为一个serve ...

  3. word embedding 精要整理

    word embedding 具体含义:词的实数向量化表示,可以通过向量相似性度量语义相似性,相似性原理是上下文的一致性 Embedding在数学上表示一个maping, f: X -> Y, ...

  4. Winform 奇怪的 英文字体错乱显示问题

    效果如图: 字体是Calibri 后来看了一下,我在vs设计器中为了预览效果定义了这些中文,然后在构造函数中将其改成英文,可能是已经为中文渲染出了位置?在改变就会冲突? 我的设计器 如何修改: 将vs ...

  5. 天猫精灵业务如何使用机器学习PAI进行模型推理优化

    引言 天猫精灵(TmallGenie)是阿里巴巴人工智能实验室(Alibaba A.I.Labs)于2017年7月5日发布的AI智能语音终端设备.天猫精灵目前是全球销量第三.中国销量第一的智能音箱品牌 ...

  6. NX二次开发-UFUN点构造器UF_UI_point_construct

    #include <uf.h> #include <uf_ui.h> UF_initialize(); //点构造器 char sCue[] = "点构造器" ...

  7. NX二次开发-NXOPEN自动切换到工程图模块

    UFUN的API里是没有切换到工程图的函数的,NXOPEN里是有方法可以用的.不过应该是不支持NX9以下的版本. NX9的不能录制出来,在UI类里有方法 NX9+VS2012 #include < ...

  8. Photon Server的Unity3D客户端配置

    Photon Server与Unity3D的交互分为3篇博文实现 (1)Photon Server的服务器端配置 (2)Photon Server的Unity3D客户端配置 (3)Photon Ser ...

  9. LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)

    题目标签:Math 题目让我们判断机器人是否是一直在走一个圈. 当我们把 instructions 走完一遍时候: 1. 如果机器人回到了原点,那么它是在走一个圈. 2. 如果机器人的方向没有改变,那 ...

  10. RTC, Real Time Clock

    配置 写入RTC_PRL, RTC_CNT, RTC_ALR寄存器时,需要先进入配置模式,通过把RTC_CRL寄存器的CNF位置一. 另外,在每次配置一个寄存器时必须等待上一次配置完成,可以通过检测R ...