postgresql使用pg_dump和pg_restore 实现跨服务器的数据库迁移或备份

postgresql使用pg_dump和pg_restore 实现跨服务器的数据库迁移或备份,第1张

概述因为业务需求,需要将服务器上的postgre多个数据库的数据整个库得迁移到另一个postgre数据库上。 一般表较少时,会使用postgre 的copy to 和 copy from 命令就能完成表的迁移,但这种方式需要target_database 上提前先创建好对应的表,并且每一个表都需要一次copy to 和copy from *** 作,当表比较多的时候,非常繁琐。 因此我查询了网上的方法,发现了

因为业务需求,需要将服务器上的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相似:
 1 -F format 2 --format=format 3 Selects the format of the output. format can be one of the following: 4 p 5 plain 6 Output a plain-text sql script file (the default). 7 c 8 custom 9 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.10 d11 directory12 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.13 t14 tar15 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.16  17 -C18 --create19 Begin the output with a command to create the database itself and reconnect to the created database. (With a script of this form,it doesnt 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.20 This option is only meaningful for the plain-text format. For the archive formats,you can specify the option when you call pg_restore.21 -E en@R_404_5563@22 --en@R_404_5563@=en@R_404_5563@23 Create the dump in the specifIEd character set en@R_404_5563@. By default,the dump is created in the database en@R_404_5563@. (Another way to get the same result is to set the PGCLIENTEN@R_404_5563@ environment variable to the desired dump en@R_404_5563@.)24  25 -O26 --no-owner27 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.28 This option is only meaningful for the plain-text format. For the archive formats,you can specify the option when you call pg_restore.29  

 

  举例: 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 实现跨服务器的数据库迁移或备份全部内容,希望文章能够帮你解决postgresql使用pg_dump和pg_restore 实现跨服务器的数据库迁移或备份所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1155670.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存