在 CentOS 上配置 PostgreSQL 主从(流复制,Streaming Replication) 是常见做法。下面以 PostgreSQL 12+ 为例,说明基本步骤(CentOS 7/8 通用)。
假设:
/var/lib/pgsql/12/datapostgresyum install -y postgresql12-server postgresql12
/usr/pgsql-12/bin/postgresql-12-setup initdb
systemctl enable postgresql-12
systemctl start postgresql-12
postgresql.conflisten_addresses = '*'
wal_level = replica
max_wal_senders = 10
wal_keep_size = 1GB
hot_standby = on
pg_hba.conf允许从库连接:
host replication repluser 192.168.1.11/32 md5
su - postgres
psql
CREATE ROLE repluser WITH REPLICATION PASSWORD 'yourpassword' LOGIN;
systemctl restart postgresql-12
yum install -y postgresql12-server postgresql12
su - postgres
pg_basebackup -h 192.168.1.10 -U repluser -D /var/lib/pgsql/12/data -P --wal-method=stream
standby.signaltouch /var/lib/pgsql/12/data/standby.signal
postgresql.conf(从库)primary_conninfo = 'host=192.168.1.10 port=5432 user=repluser password=yourpassword'
systemctl enable postgresql-12
systemctl start postgresql-12
SELECT * FROM pg_stat_replication;
看到从库连接即成功。
SELECT pg_is_in_recovery();
返回 t 表示从库。
synchronous_commit = on
synchronous_standby_names = 'standby1'
repmgr 或 Patroni如果你需要:
可以继续告诉我。