[postgres]配置主从异步流复制

前言

环境信息

IP 角色 操作系统 PostgreSQL版本
192.168.1.112 主库 Debian 12 15.3
192.168.1.113 从库 Debian 12 15.3

配置主从

  1. 修改主库的postgresql.conf文件。修改此配置文件需重启数据库服务。归档脚本内容见"附录-clean_archivelog"

    listen_addresses = '*'
    archive_mode = on
    archive_command = 'bash /home/postgres/scripts/clean_archivelog.sh %f %p'

  2. 主库创建用户

sql 复制代码
create user replicator replication password '1234qwer';
  1. 修改主库pg_hba.conf

    host replication replicator 192.168.1.113/24 scram-sha-256

  2. 重载pg_hba.conf的配置

bash 复制代码
pg_ctl reload -D /home/postgres/apps/pgsql/data/
  1. 删除从库的数据目录,备份主库的数据
bash 复制代码
rm -rf /home/postgres/apps/pgsql/data
mkdir /home/postgres/apps/pgsql/data
chmod -R 700 /home/postgres/apps/pgsql/data

pg_basebackup -h 192.168.1.112 -p 5432 -U replicator -D /home/postgres/apps/pgsql/data/ -P -v -R -X stream -C -S slot1
  1. 备份完成后,从库的data目录下自动生成postgresql.auto.conf和standby.signal文件。
    1. postgresql.auto.conf包含主库节点的相关连接信息
    2. standby.signal用于标识当前节点为从库节点
  2. 启动从库。PS:因为从库的配置皆从主库复制而来,因此归档脚本也要在从库服务器存在。
bash 复制代码
pg_ctl start -D /home/postgres/apps/pgsql/data -l ~/logfile

测试

  1. 主库建库建表添数,之后查看从库是否同步数据
bash 复制代码
create database reptest;
\c reptest
create table t1(id int, time timestamp);
insert into t1 values (1, now());
  1. 主库psql查看从库的相关信息
sql 复制代码
-- 启用扩展输出
\x
-- 检查复制状态
select * from pg_stat_replication;

-[ RECORD 1 ]----+------------------------------
pid              | 3794
usesysid         | 16388
usename          | replicator
application_name | walreceiver
client_addr      | 192.168.1.113
client_hostname  | 
client_port      | 41708
backend_start    | 2024-01-27 18:44:08.326476+08
backend_xmin     | 
state            | streaming
sent_lsn         | 0/33FF750
write_lsn        | 0/33FF750
flush_lsn        | 0/33FF750
replay_lsn       | 0/33FF750
write_lag        | 
flush_lag        | 
replay_lag       | 
sync_priority    | 0
sync_state       | async
reply_time       | 2024-01-27 18:54:58.764699+08
  1. 查看主库进程,会有walsender进程
bash 复制代码
$ ps -ef | grep postgres:

postgres    3794    3739  0 18:44 ?        00:00:00 postgres: walsender replicator 192.168.1.113(41708) streaming 0/33FF750
  1. 查看从库进程,会有walreceiver进程
bash 复制代码
$ ps -ef | grep postgres:

postgres    1309    1305  0 18:44 ?        00:00:01 postgres: walreceiver streaming 0/33FF838

附录

clean_archive

bash 复制代码
#!/bin/bash

set -u

log_dt=$(date +%F)
archivelog_dir="/home/postgres/archivelog/${log_dt}"

if [ ! -d "${archivelog_dir}/$1" ]; then
    mkdir -p "${archivelog_dir}/$1"
fi

cp --preserve=timestamps $2 ${archivelog_dir}/$1

find ${archivelog_dir}/* -type f -mtime +7 | xargs rm -f
相关推荐
wno70410 小时前
postgresSQL压缩版安装与基本命令
数据库·postgresql
瀚高PG实验室12 小时前
瀚高数据库常用系统表查询入门指南(附常用SQL)
数据库·sql·postgresql·瀚高数据库
像风一样自由202015 小时前
56.实战PostgreSQL-pgvector-FastAPI构建轻量级RAG
数据库·人工智能·postgresql·大模型·fastapi·智能体
ss27320 小时前
AI时代首先数据库-PostgreSQL实战笔记 | 11 PostgreSQL安装部署常见问题排查与解决
数据库·笔记·postgresql
ss27321 小时前
AI时代首先数据库-PostgreSQL实战笔记 | 12 PostgreSQL数据目录与重要文件结构
数据库·笔记·postgresql
一木 之林1 天前
LangChain 第8章 记忆系统:短期/长期记忆与 PostgreSQL 持久化
数据库·postgresql·langchain
IvorySQL1 天前
IvorySQL 校园行走进山东大学:从 PostgreSQL 到 AI,连接技术实践与职业成长
数据库·人工智能·postgresql
IvorySQL1 天前
PostgreSQL 日报| PostgreSQL 19 RC1 正式发布(10 月 16 日)
数据库·人工智能·postgresql·开源
IvorySQL2 天前
PostgreSQL 日报| PgBouncer 修复三个 CVE 漏洞(10 月 15 日)
数据库·postgresql·区块链
饺子大魔王的男人2 天前
后台表格、查询、流程总在重复写?我用ToolJet把PostgreSQL直接做成可远程访问的业务系统
数据库·postgresql