因为业务需求,需要将服务器上的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. 浅析vue响应式原理

    图很清晰 当我们把一个普通的 JavaScript 对象传给 Vue 实例的 data 选项,Vue 将遍历此对象所有的属性,并使用 Object.defineProperty 把这些属性全部转为 g ...

  2. C#十六进制值0x12,是一个无效字符 - 程序园

    原文:C#十六进制值0x12,是一个无效字符 - 程序园 我正在加载很多xml文档,其中一些返回错误,如“十六进制值0x12,是无效字符”,并且有不同的字符.如何删除它们?   我在这里做了一个小的研 ...

  3. C#操作Word的+ CKEditor 輸出成Word文件(包含圖案上傳)

    C#操作Word 参考博文: C#操作word类文件 https://www.cnblogs.com/walking/p/3571068.html C#中的Office操作专栏(21) http:// ...

  4. 一般处理程序(ashx)获取不到POST请求的参数问题

    写了一个一般处理程序来做接口,由于字段Content是文本,长度可能很长,鉴于这个原因,所以不能GET请求 所以问题来了,当我改成POST请求,自己使用HttpHelper类来写了一个Demo cod ...

  5. Linux 进程间通信 消息队列 实现两个进程间通信

    例子: 通过消息队列实现两个进程间通信,一个进程从终端输入数据,通过消息队列发送,另一个进程通过消息队列接收数据 文件1 创建进程1 终端输入通过消息队列发送数据 #include <stdio ...

  6. Spring入门(四)Spring-test模块

    自动化转配bean的测试案例分析 package soundsystem; import static org.junit.Assert.*; import org.junit.Rule; impor ...

  7. Java—Map浅入

    写支付签名的时候遇到了Map一家,就简单的比较了一下,于是乎先打印看看结果 Map<String,String> hashMap1 = new HashMap<>();hash ...

  8. 面对对象(JS)

    面对对象的三大特征:封闭.继承.多态 七大基本原则:    1.单一职责    2.开闭原则    3.里氏替换    4.依赖倒置    5.接口隔离    6.迪米特法则    7.01组合/聚合 ...

  9. css的3d注意事项

    1,如果要写出立体效果,所有的父容器都要添加属性transform-style: preserve-3d;不然子元素不能进行3d变换:背景色要写到最外层容器,且不能有transform-style: ...

  10. 泛型(Generic)方法(函数,算法)

    例子: static void Main(string[] args) { int[] a1 = { 1, 2, 3, 4, 5 }; int[] a2 = { 1, 2, 3, 4, 5 }; do ...