[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.confstandby.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
相关推荐
有想法的py工程师2 小时前
PostgreSQL 分区表排序优化:Append Sort 优化为 Merge Append
大数据·数据库·postgresql
水彩橘子10 小时前
PostgreSQL Streaming Replication 主从
数据库·postgresql
亚马逊云开发者10 小时前
Amazon Aurora PostgreSQL 快速配置实战:两次点击秒级创建无服务器数据库,告别 VPC 子网安全组配置噩梦
数据库·postgresql·serverless
羑悻的小杀马特14 小时前
PostgreSQL主从复制实战,告别单点故障,附主从切换与延迟监控命令。
运维·服务器·数据库·人工智能·postgresql
阿里小阿希1 天前
CentOS7 PostgreSQL 9.2 升级到 15 完整教程
数据库·postgresql
WangJunXiang61 天前
第09章:PostgreSQL日常维护
数据库·postgresql
dyyshb1 天前
PostgreSQL 终极兜底方案
数据库·postgresql
Mr.徐大人ゞ1 天前
2-5.全文索引与并行查询
postgresql
360智汇云1 天前
PostgreSQL 全文检索深度指南:内置 FTS、zhparser 与 pg_search 全解
postgresql·django·全文检索
有想法的py工程师1 天前
PostgreSQL 性能优化实战:一条 Order by 的 SQL 从 5 秒优化到 100ms
大数据·数据库·postgresql·性能优化