PostgreSQL同步复制搭建
摘要: PostgreSQL同步复制搭建
一、初始化master节点
1. 安装PostgreSQL
2. 初始化db
initdb -D /data/pg940_data
二、配置master节点
1. 修改postgresql.conf
$ vim /data/pg940_data/postgresql.conf
wal_level = hot_standby
fsync = on
synchronous_commit = on #同步复制必须设置为on
wal_sync_method = fdatasync
max_wal_senders = 4 #配置的是两个slave节点,最好设置大一点,方便以后添加slave节点,pg_basebackup使用stream方式需要占用2个槽
wal_keep_segments = 100
2. 修改白名单
$ vim /data/pg940_data/pg_hba.conf
host all all 172.17.0.0/16 md5
host replication all 172.17.0.0/16 md5
3. 启动master节点
$ pg_ctl -D /data/pg940_data start
三、制作slave节点
1. 获取master节点的基础备份
$ sudo mkdir -p /data/pg940_data
$ sudo chown -R postgres:postgres /data/pg940_data
$ pg_basebackup -X s -h 172.17.5.45 -U postgres -D /data/pg940_data
2. 配置postgresql.conf
hot_standby = on
max_standby_archive_delay = 30s
max_standby_streaming_delay = 30s
3. 配置recovery.conf
standby_mode = 'on'
primary_conninfo = 'host=172.17.5.45 port=5432 user=postgres password=xxxxxx application_name=node2'
restore_command = ''
archive_cleanup_command = ''
4. 启动slave节点
$ pg_ctl -D /data/pg940_data start
$ psql -U postgres
postgres=# select * from pg_stat_replication ;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_location | write_location | fl
ush_location | replay_location | sync_priority | sync_state
-------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+--------------+-----------+---------------+----------------+---
-------------+-----------------+---------------+------------
12262 | 10 | postgres | node3 | 172.17.5.47 | | 48691 | 2015-09-06 14:44:52.674976+08 | 400592 | streaming | 1/74000210 | 1/74000210 | 1/
74000210 | 1/74000210 | 0 | async
12263 | 10 | postgres | node2 | 172.17.5.46 | | 35271 | 2015-09-06 14:44:52.677004+08 | 400592 | streaming | 1/74000210 | 1/74000210 | 1/
74000210 | 1/74000210 | 0 | async
(2 rows)
可以看出两个节点现在都还是异步的
四、修改为同步复制
$ vim /data/pg940_data/postgresql.conf
synchronous_standby_names = 'node2, node3' # reload配置就行,不需要重启
$ psql -U postgres
psql (9.4.4)
Type "help" for help.
postgres=# show synchronous_standby_names;
synchronous_standby_names
---------------------------
(1 row)
postgres=# select pg_reload_conf();
pg_reload_conf
----------------
t
(1 row)
postgres=# show synchronous_standby_names;
synchronous_standby_names
---------------------------
node2, node3
(1 row)
postgres=# select * from pg_stat_replication ;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_location | write_location | fl
ush_location | replay_location | sync_priority | sync_state
-------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+--------------+-----------+---------------+----------------+---
-------------+-----------------+---------------+------------
12262 | 10 | postgres | node3 | 172.17.5.47 | | 48691 | 2015-09-06 14:44:52.674976+08 | 400592 | streaming | 1/740002E8 | 1/740002E8 | 1/
740002E8 | 1/740002E8 | 2 | potential
12263 | 10 | postgres | node2 | 172.17.5.46 | | 35271 | 2015-09-06 14:44:52.677004+08 | 400592 | streaming | 1/740002E8 | 1/740002E8 | 1/
740002E8 | 1/740002E8 | 1 | sync
(2 rows)
可以看出现在node2是同步复制的,node3是备用同步复制节点
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?