Skip to content

Instantly share code, notes, and snippets.

@felipe3dfx
Last active June 1, 2020 15:42
Show Gist options
  • Save felipe3dfx/e3b8e34224eb711cfcd8991c791ad55c to your computer and use it in GitHub Desktop.
Save felipe3dfx/e3b8e34224eb711cfcd8991c791ad55c to your computer and use it in GitHub Desktop.
Setup master server Postgres 12

Setup master server:

base: https://medium.com/omis-engineering/django-multiple-database-system-using-postgresql-master-slave-database-architecture-f71d4e9e53ba

Create a user for the replication:

$ psql postgres
postgres=# CREATE USER replica REPLICATION LOGIN ENCRYPTED PASSWORD 'so-secret';

Change your postgres.conf:

$ sudo vim /etc/postgresql/12/main/postgresql.conf

Set this:

listen_addresses = '*'
wal_level = replica
max_wal_senders = 3
wal_keep_segments = 64

Change your pg_hba.conf:

$ sudo vim /etc/postgresql/12/main/pg_hba.conf

Add at the end of the file:

host    replication     replica      <slave-ip-address>/32            md5

Restart your master server:

$ sudo systemctl restart postgresql

Setup slave server:

Stop postgres server:

$ sudo systemctl stop postgresql

As postgres user:

$ sudo -i
$ su - postgres
$ rm -rf /var/lib/postgresql/12/main/*
$ pg_basebackup -h <master-ip-address> -D /var/lib/postgresql/12/main/  -Fp -Xs -P -R
Password: so-secret

Start postgres:

$ sudo systemctl start postgresql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment