Skip to content

Instantly share code, notes, and snippets.

@v-thomp4
Created June 2, 2018 07:35
Show Gist options
  • Save v-thomp4/1885718025a11ed3a798fbf45207c0ec to your computer and use it in GitHub Desktop.
Save v-thomp4/1885718025a11ed3a798fbf45207c0ec to your computer and use it in GitHub Desktop.
Setup Postgres Replication with Hot Standby
Configuring the primary server
$ sudo -u postgres /usr/pgsql-9.5/bin/createuser -U postgres repuser -P -c 5 --replication
Create the archive directory
$ mkdir -p /var/lib/postgresql/9.5/main/mnt/server/archivedir
Edit pg_hba.conf
$ nano /etc/postgresql/9.5/main/pg_hba.conf
# Allow replication connections
host replication repuser ip_standby_server/32 md5
Edit postgresql.conf
$ nano /etc/postgresql/9.5/main/postgresql.conf
wal_level = hot_standby
archive_mode = on
archive_command = 'test ! -f mnt/server/archivedir/%f && cp %p mnt/server/archivedir/%f'
max_wal_senders = 3
$ sudo service postgresql restart
Backing up the primary server to the standby server
Stop standby server
$ sudo service postgresql stop
Run the backup utility
$ mv /var/lib/postgresql/9.5/main /var/lib/postgresql/9.5/main_old
$ sudo -u postgres pg_basebackup -h ip_primary_server -D /var/lib/postgresql/9.5/main -U repuser -v -P --xlog-method=stream
Configuring the standby server
Edit postgresql.conf
$ nano /etc/postgresql/9.5/main/postgresql.conf
hot_standby = on
Create the recovery configuration file
$ cp -avr /usr/share/postgresql/9.5/recovery.conf.sample /var/lib/postgresql/9.5/main/recovery.conf
$ nano /var/lib/postgresql/9.5/main/recovery.conf
standby_mode = on
primary_conninfo = 'host= port=5432 user=repuser password='
trigger_file = '/tmp/postgresql.trigger.5432'
Start the standby server
$ service postgresql start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment