PostgreSQL怎么实现不落地并行导出导入

44次阅读
没有评论

共计 2346 个字符,预计需要花费 6 分钟才能阅读完成。

本篇内容主要讲解“PostgreSQL 怎么实现不落地并行导出导入”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让丸趣 TV 小编来带大家学习“PostgreSQL 怎么实现不落地并行导出导入”吧!

9.4 以下版本,使用 pg_dump 并行导出,pg_restore 并行导入,迁移

(导出使用源版本 pg_dump,导入使用目标版本 pg_restore。如果是 ppas 请使用 enterprisedb 对应版本。)

1、(源库)全局元数据 (用户、表空间) 导出

需要 superuser 权限(如果你没有这个权限,跳过此步,但是务必在执行下一步时,人为在目标实例中创建所有与对象权限相关的用户)。

pg_dumpall -g -h IP 地址  -p  端口  -U  用户  -W -l  数据库名

2、(目标库)全局元数据导入

导入以上元数据,在目标库执行即可(通常包括创建用户,修改用户密码,创建表空间等。)

执行第 2 步的目的是保证导入时,执行 grant, alter set owner 等操作时,目标用户已存在,否则缺失用户会导致 pg_restore 报错。

3、(目标库)建库

postgres=# create database newdb; 
CREATE DATABASE

4、(目标库)插件

安装 postgresql 软件时,打包源库已使用的的插件。 
 
略

5、(源库)导出

mkdir /data01/pg/backup 
pg_dump -j 32 -f /data01/pg/backup -F d -h IP 地址  -p  端口  -U  用户  -W newdb

6、(目标实例)关闭 autovacuum,加速导入(可选)

使用超级用户执行 SQL 
 
alter system set autovacuum=off; 
select pg_reload_conf();

7、(目标库)导入

pg_restore -d newdb -F d -j 32 -h IP 地址  -p  端口  -U  用户  /data01/pg/backup

8、(目标实例)开启 autovacuum(如果执行了 6)

使用超级用户执行 SQL 
 
alter system set autovacuum=on; 
select pg_reload_conf();

9、(目标实例)收集统计信息(如果执行了 6)

使用超级用户执行 SQL,收集统计信息  
\c newdb  超级用户  
analyze;

使用以上方法,60GB 的数据库迁移,耗时约 10 分钟。

多机玩法

Where this gets interesting is with multiple hosts. You can:
$ # dump a remote database to your local machine
$ pg_dump -h remotedb.mydomain.com -f /home/postgres/dump.sql test
 
$ # dump a local database and write to a remote machine
$ pg_dump -h remotedb.mydomain.com test | ssh postgres@remotedb.mydomain.com  cat   dump.sql 
 
$ # dump a remote database and write to the same remote machine
$ pg_dump -h remotedb.mydomain.com test | ssh postgres@remotedb.mydomain.com  cat   dump.sql 
 
$ # or a different remote machine
$ pg_dump -h remotedb1.mydomain.com test | ssh postgres@remotedb2.mydomain.com  cat   dump.sql 
 
You also have similar restore options. I will use psql below but pg_restore works the same:
$ # dump a remote database and restore to your local machine
$ pg_dump -h remotedb.mydomain.com test1 | psql test2
 
$ # dump a local database and restore to a remote machine
$ pg_dump -h remotedb.mydomain.com test | ssh postgres@remotedb.mydomain.com  psql test 
 
$ # dump a remote database and restore to the same remote machine
$ pg_dump -h remotedb.mydomain.com test1 | ssh postgres@remotedb.mydomain.com  psql test2 
 
$ # or a different remote machine
$ pg_dump -h remotedb1.mydomain.com test | ssh postgres@remotedb2.mydomain.com  psql test

到此,相信大家对“PostgreSQL 怎么实现不落地并行导出导入”有了更深的了解,不妨来实际操作一番吧!这里是丸趣 TV 网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-20发表,共计2346字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)