postgresql16配置主从

参考:

Centos7.6部署postgresql15主从_centos postgresql_sqlboy-yuzhenc的博客-CSDN博客

主服务器

复制代码
vim /home/opt/postgresql-16.0/data/postgresql.conf
#设置以下
listen_addresses = '*' # 允许远程连接
hot_standby = on # 打开热备
wal_level = replica     # 设置 WAL 日志级别为 replica
max_wal_senders = 3     # 允许的 WAL 发送者数量,根据需要进行调整
archive_mode = on
archive_command = 'cp "%p" /home/opt/postgresql-16.0/data/archive/%f'

mkdir /home/opt/postgresql-16.0/data/archive
chown postgres:postgres -R /home/opt/postgresql-16.0/data/archive
su - postgres
pg_ctl -D /home/opt/postgresql-16.0/data restart

#创建slave用户
psql
create role replicator with login replication encrypted password '密码';

vim /home/opt/postgresql-16.0/data/pg_hba.conf
#添加以下
host replication replicator ip地址/32 md5

从服务器

首先,你需要创建一个新的PostgreSQL实例。然后,停止这个实例,并且同步用的数据目录需要是空的

复制代码
#使用pg_basebackup命令从主服务器复制数据
pg_basebackup -Fp --progress -D /home/opt/postgresql-16.0/data/ -R -h 主库ip地址 -p 主库端口 -U replicator --password
chown postgres:postgres -R /home/opt/postgresql-16.0/data/

#创建slave配置文件
vim /home/opt/postgresql-16.0/data/standby.signal
#添加以下
standby_mode = on #on为从库
primary_conninfo = 'host=主库ip地址 port=主库端口 user=replicator password=密码' #主库信息
recovery_target_timeline = 'latest' #流复制同步最新数据

su - postgres
pg_ctl -D /home/opt/postgresql-16.0/data start

验证主从关系

复制代码
#主库上验证
psql
select * from pg_stat_replication;

#从库上验证
pg_controldata -D /home/opt/postgresql-16.0/data/ | grep 'Database cluster state'
#正常显示
Database cluster state:               in archive recovery

其他命令

复制代码
#备份数据
pg_dump -U postgres -F c -b -f 备份文件路径 备份的数据库名称

#恢复数据
pg_restore -U postgres -d 备份的数据库名称 备份文件路径
相关推荐
mpHH1 天前
postgresql-分区表
数据库·postgresql
l1t1 天前
在WSL的ubuntu 26.04容器中用deb安装包安装使用redrock-4.1-1
linux·运维·ubuntu·postgresql
Hehuyi_In1 天前
postgres-howto 学习笔记
笔记·学习·postgresql·脚本·how to
Mike117.1 天前
GBase 8c 里 search_path 没管住,SQL 可能跑到另一个对象上
数据库·sql·postgresql
倒流时光三十年1 天前
PostgreSQL 中的 NULL 陷阱:从一次排除过滤说起
java·数据库·postgresql
IvorySQL2 天前
从 repack.c 深入理解 PostgreSQL REPACK 的底层实现
数据库·postgresql·开源
是垚不是土2 天前
PostgreSQL 运维工程师 “一本通“ :安装、配置、备份与监控
linux·运维·数据库·postgresql·运维开发
不会摸鱼的小鱼2 天前
WSL 安装 Ubuntu 22.04 到指定磁盘
数据库·postgresql·php
chushiyunen2 天前
postgresql安装timescaledb替代influxdb功能、查看已安装的插件
数据库·postgresql