Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump to PostgreSQL 15.0 #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
FROM postgres:9.6
MAINTAINER Daniel Dent (https://www.danieldent.com)
FROM postgres:15.0

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

## debug
# ENTRYPOINT ["tail", "-f", "/dev/null"]
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Postgres 9.6 Dockerized w/ Replication
# Postgres 15.0 Dockerized w/ Replication

Master/Slave Postgres Replication in 30 seconds.

Expand All @@ -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
47 changes: 24 additions & 23 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@

version: '2'
version: "3.8"

services:
pg-master:
build: '.'
image: 'danieldent/postgres-replication'
restart: 'always'
build: .
restart: always
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: 'danieldent/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
80 changes: 40 additions & 40 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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.'
Expand Down
15 changes: 9 additions & 6 deletions setup-replication.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
if [ "x$REPLICATE_FROM" == "x" ]; then

cat >> ${PGDATA}/postgresql.conf <<EOF
listen_addresses='*'
wal_level = hot_standby
max_wal_senders = $PG_MAX_WAL_SENDERS
wal_keep_segments = $PG_WAL_KEEP_SEGMENTS
wal_keep_size = $PG_WAL_KEEP_SIZE
hot_standby = on
EOF

else

cat > ${PGDATA}/recovery.conf <<EOF
standby_mode = on
cat > ${PGDATA}/postgresql.conf <<EOF
listen_addresses='*'
wal_level = hot_standby
max_wal_senders = $PG_MAX_WAL_SENDERS
wal_keep_size = $PG_WAL_KEEP_SIZE
hot_standby = on
primary_conninfo = 'host=${REPLICATE_FROM} port=5432 user=${POSTGRES_USER} password=${POSTGRES_PASSWORD}'
trigger_file = '/tmp/touch_me_to_promote_to_me_master'
EOF
chown postgres ${PGDATA}/recovery.conf
chmod 600 ${PGDATA}/recovery.conf
cp /tmp/standby.signal ${PGDATA}/standby.signal

fi
Empty file added standby.signal
Empty file.