Skip to content

Instantly share code, notes, and snippets.

@lfittl
Last active February 4, 2024 07:53
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfittl/9689e3b7b3e1293f1f18 to your computer and use it in GitHub Desktop.
Save lfittl/9689e3b7b3e1293f1f18 to your computer and use it in GitHub Desktop.
Setting up a docker-ized Postgres Slave

(Note: This assumes you've already configured your master server for streaming replication)

export DATADIR=/data/postgres-9.3
export MASTER=192.168.0.1
export REPL_USER=replication
export REPL_PASSWORD=mypassword

Create a base backup:

$ docker run -it --rm -v $DATADIR:/var/lib/postgresql/data lfittl/postgres:9.3 pg_basebackup -h $MASTER -D /var/lib/postgresql/data -U replication -v -P -X s

Copy postgresql.conf and pg_hba.conf from master into DATADIR

Create recovery.conf in DATADIR:

standby_mode          = 'on'
primary_conninfo      = 'host=$MASTER port=5432 user=$REPL_USER password=$REPL_PASSWORD'

Start the slave:

$ docker run --name slave-db -d -v $DATADIR:/var/lib/postgresql/data lfittl/postgres:9.3

Verify that its working as expected:

$ docker logs -f slave-db

2015-01-25 20:53:44 UTC LOG:  database system was interrupted; last known up at 2015-01-25 20:48:58 UTC
2015-01-25 20:53:44 UTC LOG:  creating missing WAL directory "pg_xlog/archive_status"
2015-01-25 20:53:44 UTC LOG:  entering standby mode
2015-01-25 20:53:44 UTC LOG:  redo starts at 124F/B38D1B28
2015-01-25 20:53:48 UTC LOG:  restartpoint starting: xlog
2015-01-25 20:54:01 UTC LOG:  consistent recovery state reached at 1250/BD937B48
2015-01-25 20:54:01 UTC LOG:  database system is ready to accept read only connections
2015-01-25 20:54:01 UTC LOG:  started streaming WAL from primary at 1250/BE000000 on timeline 1
@defp
Copy link

defp commented Feb 4, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment