Docker 启动 PostgreSQL 主从架构:实现数据同步

1、准备环境

IP地址 主机名 角色
10.16.12.115 postgresql01 主库
10.16.12.116 postgresql02 从库

3、拉取postgresql镜像

bash 复制代码
docker pull registry.cn-hangzhou.aliyuncs.com/qiluo-images/postgres-with-gis:16.4

4、创建db网络

bash 复制代码
docker network create --driver bridge dbnet

5、创建数据目录

bash 复制代码
mkdir /data/postgresql/data -p
chmod -R 777 /data/postgresql/data

docker-compose.yml文件编写

主库文件:

bash 复制代码
sudo tee /data/postgresql/docker-compose.yml <<-'EOF'
version: '3.0'
services:
  postgresql:
    image: registry.cn-hangzhou.aliyuncs.com/qiluo-images/postgres-with-gis:16.4
    restart: always
    container_name: postgresql01
    environment:
      POSTGRES_PASSWORD: admin@123
    working_dir: /postgresql
    networks:
      - dbnet
    ports:
      - "5432:5432"
    volumes:
      - /data/postgresql/data:/var/lib/postgresql/data
 
networks:
  dbnet:
    external: true
EOF

从库文件:

bash 复制代码
sudo tee /data/postgresql/docker-compose.yml <<-'EOF'
version: '3.0'
services:
  postgresql:
    image: registry.cn-hangzhou.aliyuncs.com/qiluo-images/postgres-with-gis:16.4
    restart: always
    container_name: postgresql02
    environment:
      POSTGRES_PASSWORD: admin@123
    working_dir: /postgresql
    networks:
      - dbnet
    ports:
      - "5432:5432"
    volumes:
      - /data/postgresql/data:/var/lib/postgresql/data
 
networks:
  dbnet:
    external: true
EOF

启动postgresql服务

bash 复制代码
cd /data/postgresql
docker-compose up -d
docker-compose ps

部署主库

bash 复制代码
docker exec -it postgresql01 /bin/bash
su - postgres
psql
CREATE ROLE replica login replication encrypted password 'replica@123';
bash 复制代码
修改postgresql.conf
vi /data/postgresql/data/postgresql.conf

```bash
listen_addresses = '*'
archive_mode = on
archive_command = '/bin/date'
max_connections = 1024
max_wal_senders = 32
wal_sender_timeout = 60s
wal_keep_size = 16
wal_level = replica

修改pg_hba.conf

vi /data/postgresql/data/pg_hba.conf

bash 复制代码
host    replication     replica        0.0.0.0/32        trust

部署从库

bash 复制代码
docker exec -it postgresql02 /bin/bash
在容器中运行相关命令:
rm -rf /var/lib/postgresql/data/*

如果退出了,则再次进入

bash 复制代码
docker exec -it postgresql02 /bin/bash
su - postgres
pg_basebackup -R -D /var/lib/postgresql/data -P -h 192.168.52.15 -p 5432 -U replica
bash 复制代码
docker restart postgresql02 
# 查看/data/postgresql/data下的 postgresql.auto.conf

、查看进程

主机查看主数据库

bash 复制代码
ps -ef|grep postgres

从机查看从数据库

bash 复制代码
docker ps |grep postgres

测试同步

在主机容器中

bash 复制代码
docker exec -it postgresql01 /bin/bash
su - postgres
psql
create database test12;
\list

在从机查看已经同步

bash 复制代码
docker exec -it postgresql02 /bin/bash
su - postgres
psql
\list

关闭selinux

bash 复制代码
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
setenforce 0
相关推荐
weixin_446260851 天前
城市智能化的底层基石:基于腾讯地图服务生态的移动定位与导航架构指引
大数据·人工智能·架构
SCBAiotAigc1 天前
2026.5.1:`DockerDesktop must be owned by an elevated account`错误的解决办法
人工智能·docker·具身智能
of Watermelon League1 天前
5、使用 pgAdmin4 图形化创建和管理 PostgreSQL 数据库
数据库·postgresql
@#¥&~是乱码鱼啦1 天前
Spring分层架构:Controller、Service、Mapper数据链路,IOC的真实工作意义
java·spring·架构
vortex51 天前
SafeLine 雷池WAF 真实体验,谈谈架构与原理
架构
Dontla1 天前
Python asyncpg库介绍(基于Python asyncio的PostgreSQL数据库驱动)连接池、SQLAlchemy
数据库·python·postgresql
该昵称用户已存在1 天前
MyEMS 开源能源管理系统:模块化架构赋能精细化能源管控
架构·开源·能源
张~颜1 天前
PostgreSQL数据压缩技术
数据库·postgresql
Ulyanov1 天前
《现代 Python 桌面应用架构实战:PySide6 + QML 从入门到工程化》 开发环境搭建与工具链极简主义 —— 拒绝臃肿,构建工业级基座
开发语言·python·qt·ui·架构·系统仿真
郭龙_Jack1 天前
Kubernetes 架构一张图讲透
架构