我要将生产 PostgreSQL 12 数据库(~200GB)迁移到另一个更强大的服务器。分区表一堆,有的没有主键(理论上如果需要的话临时添加也没有问题)
是否有任何解决方案可以在不停机或至少停机时间最短的情况下进行迁移?
正如@FrankHeikens 在问题评论中所写,我们需要配置主备复制。
以下是 master 上的步骤(Postgres 12):
注意: 所有命令均代表具有 sudo 权限的用户运行。
psql -U postgres
CREATE ROLE replication WITH REPLICATION PASSWORD 'REPLICATION_USER_PASSWORD' LOGIN;
sudo nano /etc/postgresql/12/main/pg_hba.conf
# Add the following line
> To Existing Server
host replication replication STANDBY_SERVER_IP/32 md5
sudo nano /etc/postgresql/12/main/postgres.conf
# Uncomment and edit values
listen_addresses = '*'
wal_level = replica
wal_keep_segments = 256 # (each segment is 16 MB by default, so ~5GB of free space is need)
max_wal_senders = 2 # (for case of single standby)
max_replication_slots = 2 # (for case of single standby)
hot_standby = on
hot_standby_feedback = on
在系统防火墙中打开 Postgres 网络端口(默认为 5432)。
重新启动Postgtres
sudo service postgresql restart
以下是待机步骤(Postgres 12):
注意: 所有命令均代表具有 sudo 权限的用户运行。
sudo service postgresql stop
sudo rm /var/log/postgresql/*
sudo rm -rf /var/lib/postgresql/12/main/*
# Ensure that cleaning up is none
sudo du -sh /var/lib/postgresql/12/main/
sudo nano /etc/postgresql/12/main/postgres.conf
# Uncomment and edit values
hot_standby = on
primary_conninfo = 'user=replication password=<REPLICATION_USER_PASSWORD> host=MASTER_SERVER_IP port=5432'
cd /etc/postgresql/12/main/
sudo touch standby.signal
sudo chown postgres:postgres standby.signal
# Enter 2 passwords: for local 'postgres' user and then for remote 'replication' user
su - postgres -c "pg_basebackup --host=MASTER_SERVER_IP --username=replication --pgdata=/var/lib/postgresql/12/main --wal-method=stream --write-recovery-conf"
sudo service postgresql start
检查复制进度:
psql -U postgres
# On master
select * from pg_stat_replication;
# On standby
select * from pg_stat_wal_receiver;
将备用服务器升级为主模式:
psql -U postgres
select pg_promote();