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 备份的数据库名称 备份文件路径
相关推荐
IvorySQL22 分钟前
PostgreSQL 日报|建库策略致备库静默丢数据(9 月 12 日)
数据库·人工智能·postgresql
瀚高PG实验室6 小时前
瀚高数据库如何克隆表
数据库·sql·postgresql·瀚高数据库
承渊政道1 天前
PostgreSQL 鸿蒙 PC 适配全记录:从原生交叉编译到 HNP 数据库服务闭环
数据库·postgresql·harmonyos·鸿蒙系统·pc端
梦想平凡3 天前
百游棋牌源代码开发搭建教程(二):PostgreSQL安装、业务表创建与版本化迁移
数据库·游戏·postgresql
瀚高PG实验室3 天前
瀚高数据库V9.0.5数据脱敏完整配置步骤
jvm·数据库·postgresql·瀚高数据库
瀚高PG实验室4 天前
使用pg_stat_statements抓取数据库TOP SQL
数据库·sql·postgresql·瀚高数据库
IvorySQL4 天前
打造下一代 AI Agent 的统一多模智能数据底座——PostgreSQL 与 AI 的融合演进
数据库·人工智能·postgresql
半兽先生4 天前
MySQL + Milvus vs PostgreSQL + pgvector:AI 应用向量检索架构选型终极指南
mysql·postgresql·milvus
l1t4 天前
DeepSeek 4.1总结的Tom Lane 谈塑造 Postgres 三十年历程的架构决策
开发语言·数据库·postgresql·架构
PrudentWoo5 天前
PostgreSQL 12 登录失败:role “postgres“ is not permitted to log in 的排查与修复
数据库·postgresql