From 663dfbc542fdf896e001510b517f9d0f890920b8 Mon Sep 17 00:00:00 2001 From: Kenneth Tu Date: Tue, 9 Aug 2022 14:37:17 +0800 Subject: [PATCH 1/6] bump to PostgreSQL 13.7 --- Dockerfile | 16 ++++++--- docker-compose.yml | 4 +-- docker-entrypoint.sh | 80 ++++++++++++++++++++++---------------------- setup-replication.sh | 15 +++++---- standby.signal | 0 5 files changed, 62 insertions(+), 53 deletions(-) create mode 100644 standby.signal diff --git a/Dockerfile b/Dockerfile index e17409a..31ffc33 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,13 @@ -FROM postgres:9.6 -MAINTAINER Daniel Dent (https://www.danieldent.com) +FROM postgres:13.7 + +RUN apt-get update -y +RUN apt-get install -y iputils-ping + ENV PG_MAX_WAL_SENDERS 8 -ENV PG_WAL_KEEP_SEGMENTS 8 +ENV PG_WAL_KEEP_SIZE 128 + COPY setup-replication.sh /docker-entrypoint-initdb.d/ -COPY docker-entrypoint.sh /docker-entrypoint.sh -RUN chmod +x /docker-entrypoint-initdb.d/setup-replication.sh /docker-entrypoint.sh +COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh +COPY standby.signal /tmp/standby.signal + +RUN chmod +x /docker-entrypoint-initdb.d/setup-replication.sh /usr/local/bin/docker-entrypoint.sh diff --git a/docker-compose.yml b/docker-compose.yml index 81a66da..721c04e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ version: '2' services: pg-master: build: '.' - image: 'danieldent/postgres-replication' + image: 'donlinglok/postgres-replication' restart: 'always' environment: POSTGRES_USER: 'postgres' @@ -17,7 +17,7 @@ services: pg-slave: build: '.' - image: 'danieldent/postgres-replication' + image: 'donlinglok/postgres-replication' restart: 'always' environment: POSTGRES_USER: 'postgres' diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index f290efb..b8cd750 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -34,18 +34,18 @@ if [ "$1" = 'postgres' ]; then # look specifically for PG_VERSION, as it is expected in the DB dir if [ ! -s "$PGDATA/PG_VERSION" ]; then if [ "x$REPLICATE_FROM" == "x" ]; then - eval "gosu postgres initdb $POSTGRES_INITDB_ARGS" + eval "gosu postgres initdb $POSTGRES_INITDB_ARGS" else - until ping -c 1 -W 1 ${REPLICATE_FROM} - do - echo "Waiting for master to ping..." - sleep 1s - done - until gosu postgres pg_basebackup -h ${REPLICATE_FROM} -D ${PGDATA} -U ${POSTGRES_USER} -vP -w - do - echo "Waiting for master to connect..." - sleep 1s - done + until ping -c 1 -W 1 ${REPLICATE_FROM} + do + echo "Waiting for master to ping..." + sleep 1s + done + until gosu postgres pg_basebackup -h ${REPLICATE_FROM} -D ${PGDATA} -U ${POSTGRES_USER} -vP -w + do + echo "Waiting for master to connect..." + sleep 1s + done fi # check password first so we can output the warning before postgres @@ -73,39 +73,39 @@ if [ "$1" = 'postgres' ]; then authMethod=trust fi - if [ "x$REPLICATE_FROM" == "x" ]; then - { echo; echo "host replication all 0.0.0.0/0 $authMethod"; } | gosu postgres tee -a "$PGDATA/pg_hba.conf" > /dev/null { echo; echo "host all all 0.0.0.0/0 $authMethod"; } | gosu postgres tee -a "$PGDATA/pg_hba.conf" > /dev/null - # internal start of server in order to allow set-up using psql-client - # does not listen on external TCP/IP and waits until start finishes - gosu postgres pg_ctl -D "$PGDATA" \ - -o "-c listen_addresses='localhost'" \ - -w start - - : ${POSTGRES_USER:=postgres} - : ${POSTGRES_DB:=$POSTGRES_USER} - export POSTGRES_USER POSTGRES_DB - - psql=( psql -v ON_ERROR_STOP=1 ) + if [ "x$REPLICATE_FROM" == "x" ]; then - if [ "$POSTGRES_DB" != 'postgres' ]; then + # internal start of server in order to allow set-up using psql-client + # does not listen on external TCP/IP and waits until start finishes + gosu postgres pg_ctl -D "$PGDATA" \ + -o "-c listen_addresses='localhost'" \ + -w start + + : ${POSTGRES_USER:=postgres} + : ${POSTGRES_DB:=$POSTGRES_USER} + export POSTGRES_USER POSTGRES_DB + + psql=( psql -v ON_ERROR_STOP=1 ) + + if [ "$POSTGRES_DB" != 'postgres' ]; then + "${psql[@]}" --username postgres <<-EOSQL + CREATE DATABASE "$POSTGRES_DB" ; + EOSQL + echo + fi + + if [ "$POSTGRES_USER" = 'postgres' ]; then + op='ALTER' + else + op='CREATE' + fi "${psql[@]}" --username postgres <<-EOSQL - CREATE DATABASE "$POSTGRES_DB" ; + $op USER "$POSTGRES_USER" WITH SUPERUSER $pass ; EOSQL echo - fi - - if [ "$POSTGRES_USER" = 'postgres' ]; then - op='ALTER' - else - op='CREATE' - fi - "${psql[@]}" --username postgres <<-EOSQL - $op USER "$POSTGRES_USER" WITH SUPERUSER $pass ; - EOSQL - echo fi @@ -122,9 +122,9 @@ if [ "$1" = 'postgres' ]; then echo done - if [ "x$REPLICATE_FROM" == "x" ]; then - gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop - fi + if [ "x$REPLICATE_FROM" == "x" ]; then + gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop + fi echo echo 'PostgreSQL init process complete; ready for start up.' diff --git a/setup-replication.sh b/setup-replication.sh index 460c548..a81ac33 100755 --- a/setup-replication.sh +++ b/setup-replication.sh @@ -3,20 +3,23 @@ if [ "x$REPLICATE_FROM" == "x" ]; then cat >> ${PGDATA}/postgresql.conf < ${PGDATA}/recovery.conf < ${PGDATA}/postgresql.conf < Date: Tue, 9 Aug 2022 14:38:20 +0800 Subject: [PATCH 2/6] bump to PostgreSQL 13.7 --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 09a348e..7a0a03c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Postgres 9.6 Dockerized w/ Replication +# Postgres 13.7 Dockerized w/ Replication Master/Slave Postgres Replication in 30 seconds. @@ -14,6 +14,3 @@ Master/Slave Postgres Replication in 30 seconds. * No additional replication user is setup - the postgres admin user is used. This means the superuser credentials must be identical on the master and all slaves. * setup-replication.sh is only executed when a container's data volume is first initialized. * REPLICATE_FROM environment variable is only used during container initialization - if the master changes after the database has been initialized, you'll need to manually adjust the recovery.conf file in the slave containers' data volume. - * Configuration: - * PG_MAX_WAL_SENDERS 8 - Maximum number of slaves - * PG_WAL_KEEP_SEGMENTS 32 - See http://www.postgresql.org/docs/9.6/static/runtime-config-replication.html From e718e060a52c9a6b5dfaa87d93f24ca9d32cf10a Mon Sep 17 00:00:00 2001 From: Kenneth Tu Date: Mon, 17 Oct 2022 11:00:03 +0800 Subject: [PATCH 3/6] bump to version 15.0 --- Dockerfile | 5 ++++- docker-compose.yml | 48 ++++++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Dockerfile b/Dockerfile index 31ffc33..b6fe61d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM postgres:13.7 +FROM postgres:15.0 RUN apt-get update -y RUN apt-get install -y iputils-ping @@ -11,3 +11,6 @@ COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh COPY standby.signal /tmp/standby.signal RUN chmod +x /docker-entrypoint-initdb.d/setup-replication.sh /usr/local/bin/docker-entrypoint.sh + +## debug +# ENTRYPOINT ["tail", "-f", "/dev/null"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 721c04e..e4f8b89 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,32 +1,34 @@ - -version: '2' +version: "3.8" services: pg-master: - build: '.' - image: 'donlinglok/postgres-replication' - restart: 'always' + build: . + restart: always + # image: 'donlinglok/postgres-replication' + ports: + - "5432:5432" environment: - POSTGRES_USER: 'postgres' - POSTGRES_PASSWORD: 'postgres' - PGDATA: '/var/lib/postgresql/data/pgdata' + - POSTGRES_DB=test + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - PGDATA=/var/lib/postgresql/data/pgdata + #- REPLICATE_FROM=pg-slave #only after pg-slave get promoted volumes: - - '/var/lib/postgresql/data' - expose: - - '5432' + - /var/lib/postgresql/data + # #Promote to master `select pg_promote(true,60);` pg-slave: - build: '.' - image: 'donlinglok/postgres-replication' - restart: 'always' + build: . + restart: always + ports: + - "2432:5432" environment: - POSTGRES_USER: 'postgres' - POSTGRES_PASSWORD: 'postgres' - PGDATA: '/var/lib/postgresql/data/pgdata' - REPLICATE_FROM: 'pg-master' + - POSTGRES_DB=test + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - PGDATA=/var/lib/postgresql/data/pgdata + - REPLICATE_FROM=pg-master + depends_on: + - "pg-master" volumes: - - '/var/lib/postgresql/data' - expose: - - '5432' - links: - - 'pg-master' + - /var/lib/postgresql/data From 6c8961d6825822f7c1e00b74670987a291fa000f Mon Sep 17 00:00:00 2001 From: Kenneth Tu Date: Mon, 17 Oct 2022 11:00:54 +0800 Subject: [PATCH 4/6] update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7a0a03c..3e728f5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Postgres 13.7 Dockerized w/ Replication +# Postgres 15.0 Dockerized w/ Replication Master/Slave Postgres Replication in 30 seconds. From addcb43937ba27f4251af0b80108d49e63df7645 Mon Sep 17 00:00:00 2001 From: Kenneth Tu Date: Mon, 17 Oct 2022 11:53:54 +0800 Subject: [PATCH 5/6] Update docker-compose.yml --- docker-compose.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index e4f8b89..373a1a4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,8 @@ services: pg-master: build: . restart: always - # image: 'donlinglok/postgres-replication' + # the image name + image: 'donlinglok/postgres-replication' ports: - "5432:5432" environment: From 28561d9523151a0e8b8437235c587f89d477896b Mon Sep 17 00:00:00 2001 From: Kenneth Tu Date: Mon, 17 Oct 2022 12:01:32 +0800 Subject: [PATCH 6/6] Update docker-compose.yml --- docker-compose.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 373a1a4..858a719 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,8 +4,6 @@ services: pg-master: build: . restart: always - # the image name - image: 'donlinglok/postgres-replication' ports: - "5432:5432" environment: