repmgr

PostgreSQL复制管理组件

概览

扩展包名版本分类许可证语言
repmgr5.5.0ETLGPL-3.0C
ID扩展名BinLibLoadCreateTrustReloc模式
9710repmgrrepmgr
相关扩展pglogical pg_failover_slots pgactive bgw_replstatus postgres_fdw pglogical_origin pglogical_ticker dblink

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG5.5.01817161514repmgr-
RPMPGDG5.5.01817161514repmgr_$v-
DEBPGDG5.5.01817161514postgresql-$v-repmgr-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64

安装

您可以直接安装 repmgr 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:

pig repo add pgdg -u          # 添加 PGDG 仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install repmgr;          # 当前活跃 PG 版本安装
pig ext install -y repmgr -v 18  # PG 18
pig ext install -y repmgr -v 17  # PG 17
pig ext install -y repmgr -v 16  # PG 16
pig ext install -y repmgr -v 15  # PG 15
pig ext install -y repmgr -v 14  # PG 14
dnf install -y repmgr_18       # PG 18
dnf install -y repmgr_17       # PG 17
dnf install -y repmgr_16       # PG 16
dnf install -y repmgr_15       # PG 15
dnf install -y repmgr_14       # PG 14
apt install -y postgresql-18-repmgr   # PG 18
apt install -y postgresql-17-repmgr   # PG 17
apt install -y postgresql-16-repmgr   # PG 16
apt install -y postgresql-15-repmgr   # PG 15
apt install -y postgresql-14-repmgr   # PG 14

预加载配置

shared_preload_libraries = 'repmgr';

创建扩展

CREATE EXTENSION repmgr;

用法

repmgr: PostgreSQL 复制管理器

一套用于管理 PostgreSQL 集群复制和故障转移的工具。支持搭建备库服务器、监控复制状态以及执行故障转移/计划切换。

启用

CREATE EXTENSION repmgr;

配置

在每个节点上创建 repmgr.conf

node_id=1
node_name='node1'
conninfo='host=node1 dbname=repmgr user=repmgr'
data_directory='/var/lib/postgresql/data'

注册主节点

repmgr -f /etc/repmgr.conf primary register

克隆并注册备节点

# 从主节点克隆
repmgr -h primary-host -U repmgr -d repmgr -f /etc/repmgr.conf standby clone

# 启动备节点
pg_ctl -D /var/lib/postgresql/data start

# 注册备节点
repmgr -f /etc/repmgr.conf standby register

监控

# 显示集群状态
repmgr -f /etc/repmgr.conf cluster show

# 显示复制状态
repmgr -f /etc/repmgr.conf node status

手动切换

将备节点提升为主节点(计划性切换):

repmgr -f /etc/repmgr.conf standby switchover

使用 repmgrd 自动故障转移

在每个节点上启动 repmgr 守护进程:

repmgrd -f /etc/repmgr.conf

repmgr.conf 中配置故障转移:

failover='automatic'
promote_command='repmgr standby promote -f /etc/repmgr.conf'
follow_command='repmgr standby follow -f /etc/repmgr.conf --upstream-node-id=%n'

关键命令

  • repmgr primary register - 注册主节点
  • repmgr standby clone - 从主节点克隆备节点
  • repmgr standby register - 注册备节点
  • repmgr standby promote - 将备节点提升为主节点
  • repmgr standby follow - 跟随新的主节点
  • repmgr standby switchover - 计划性切换
  • repmgr cluster show - 显示集群状态
  • repmgr cluster event - 显示集群事件
  • repmgr node check - 节点健康检查

最后修改 2026-03-14: update extension metadata (953cbd0)