目录

pgsql-27 主从复制与高可用

27 - 主从复制与高可用

1. 📖 概述

主从复制实现数据冗余和读写分离,提高可用性。

2. 🔄 流复制

2.1 主库配置

-- postgresql.conf
wal_level = replica
max_wal_senders = 10

-- pg_hba.conf
host replication replicator 10.0.1.0/24 md5

-- 创建复制用户
CREATE USER replicator WITH REPLICATION PASSWORD 'password';

2.2 从库配置

# 基础备份
pg_basebackup -h primary_host -D /var/lib/postgresql/data -U replicator -P -R

# 启动从库
systemctl start postgresql

3. 🚀 高可用方案

3.1 Patroni + etcd

# patroni.yml
scope: postgres-cluster
name: postgres1
restapi:
  listen: 0.0.0.0:8008
etcd:
  host: etcd:2379
postgresql:
  data_dir: /var/lib/postgresql/data
  parameters:
    max_connections: 200

4. 📚 下一步

学习监控与日志管理