以最短的停机时间将 Postgres 12 数据库复制到另一台服务器

问题描述 投票:0回答:1

我要将生产 PostgreSQL 12 数据库(~200GB)迁移到另一个更强大的服务器。分区表一堆,有的没有主键(理论上如果需要的话临时添加也没有问题)

是否有任何解决方案可以在不停机或至少停机时间最短的情况下进行迁移?

postgresql replication database-replication master-slave master-master
1个回答
0
投票

正如@FrankHeikens 在问题评论中所写,我们需要配置主备复制。

以下是 master 上的步骤(Postgres 12):

注意: 所有命令均代表具有 sudo 权限的用户运行。

  1. 创建用于复制的 Postgres 用户
psql -U postgres
CREATE ROLE replication WITH REPLICATION PASSWORD 'REPLICATION_USER_PASSWORD' LOGIN;
  1. 将复制用户添加到pg_hba.conf
sudo nano /etc/postgresql/12/main/pg_hba.conf
# Add the following line
> To Existing Server
host    replication     replication     STANDBY_SERVER_IP/32       md5
  1. 启用复制。
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
  1. 在系统防火墙中打开 Postgres 网络端口(默认为 5432)。

  2. 重新启动Postgtres

sudo service postgresql restart

以下是待机步骤(Postgres 12):

注意: 所有命令均代表具有 sudo 权限的用户运行。

  1. 。停止发帖
sudo service postgresql stop
  1. 清理数据和日志目录。
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/
  1. 启用复制。
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'
  1. 启用 Postgres 待机模式。
cd /etc/postgresql/12/main/
sudo touch standby.signal
sudo chown postgres:postgres standby.signal
  1. 创建基础备份。
# 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"
  1. 基础备份完成后启动备用服务器。
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();
© www.soinside.com 2019 - 2024. All rights reserved.