基于docker容器的Postgresql 11主从复制搭建 及切换
以下是一个简化版的Docker容器化PostgreSQL 11主从复制的示例:
- 准备
docker-compose.yml
文件:
version: '3'
services:
master:
image: postgres:11
environment:
POSTGRES_DB: primary
POSTGRES_USER: primaryuser
POSTGRES_PASSWORD: primarypassword
volumes:
- pgdata_master:/var/lib/postgresql/data
ports:
- "5432:5432"
slave:
image: postgres:11
environment:
POSTGRES_DB: replica
POSTGRES_USER: replicauser
POSTGRES_PASSWORD: replicapassword
POSTGRES_REPLICA_USER: standbyuser
POSTGRES_REPLICA_PASSWORD: standbypassword
volumes:
- pgdata_slave:/var/lib/postgresql/data
ports:
- "5433:5432"
command: >
/bin/bash -c "
echo 'primary_conninfo = 'host=master user=primaryuser password=primarypassword dbname=primary connect_timeout=20' >> /var/lib/postgresql/data/recovery.conf &&
echo 'primary_slot_name = 'replica_slot' >> /var/lib/postgresql/data/recovery.conf &&
echo 'trigger_file = /trigger_failover' >> /var/lib/postgresql/data/recovery.conf &&
echo 'recovery_min_apply_delay = 0' >> /var/lib/postgresql/data/recovery.conf &&
chmod 600 /var/lib/postgresql/data/recovery.conf &&
exec docker-entrypoint.sh postgres -c hot_standby=on -c max_standby_streaming_delay=30s
"
volumes:
pgdata_master:
pgdata_slave:
- 运行\`docker-compose
评论已关闭