主备服务器信息
主:192.168.9.113 centos7.9
备:192.168.9.114 centos7.9
一、主节点配置
1.安装postgresql-14服务
# Install the repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Install PostgreSQL:
sudo yum install -y postgresql14-server
# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14
2.创建用户复制的专用用户和修改postgres用户密码
su - postgres
psql
ALTER USER postgres with encrypted password 'postgres'; #修改postgres密码
create user repl login replication encrypted password 'repl'; #创建主从复制的专用用户repl
3.修改主配置文件postgresql.conf
vim $PGDATA/postgresql.conf
listen_addresses = '*'
wal_level = replica
max_wal_senders = 10 # 最大发送进程,默认 10,读写分离一写多服务读,请设置为读数据库的数量
synchronous_commit = on # 将事务提交方式改为本地提交,默认为 on,在 on 模式下事务需要等备份数据库一起提交,这里改为 local 让备份数据库不影响主库,如若是先配置读写分离,请设置为默认 on
修改主配置文件pg_hba.conf
vim $PGDATA/pg_hba.conf
host all all 0.0.0.0/0 scram-sha-256 #允许其他主机连接该数据库,记住防火墙也要开放5432端口才能连接,或者直接关闭防火墙
host all all 192.168.9.0/24 trust
host replication repl 192.168.9.0/24 md5 #其中repl就是创建的用户
4.重启主节点
systemctl restart postgresql-14
主节点就配置完成了
二、从节点配置
1.安装postgresql-14服务
# Install the repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Install PostgreSQL:
sudo yum install -y postgresql14-server
# Optionally initialize the database and enab