postgresql跨服务器复制数据库

时间:2023-03-09 13:24:53
postgresql跨服务器复制数据库

假设名为dbname数据库需要从A服务器拷贝到B服务器

接收服务器B

postgres用户

需先重置B服务器postgres系统用户的密码,使之与数据库用户postgres一致:

passwd -d postgres
sudo -u postgres passwd

如果不知道数据库用户postgres的密码, 在psql客户端使用命令ALTER USER postgres WITH PASSWORD '123456';重置.

创建数据库

B服务器需要先存在名为dbname的数据库才能接收.

CREATE DATABASE dbname

ssh相关

以postgres身份登录B后, 可在~/.ssh/authorized_keys 里面添加A服务器的公钥. 以免除输入密码的麻烦.

发送服务器A

切换postgres身份

su - postgres
pg_dump -C dbname | bzip2 | ssh postgres@B_host "bunzip2 | psql dbname"

参考

http://*.com/questions/1237725/copying-postgresql-database-to-another-server