While there’s absolutely nothing new in this blog post that isn’t covered by the wonderful docs I’ve been asked multiple times now by customers if we had some kind of ‘crib notes’ format for how to get replication up and running. And since I just had to set this up and document it for a customer, I figured I might as well post it so that I can simply point people to it in the future. So here we are.
Now, let’s get started. I assume you already have two PostgreSQL servers up with the binaries installed. For simplicity’s sake, we will these machines ‘master’ and ‘slave’. Note too that I’m using
replication slots which needs PostgreSQL 9.4.0 or later; if you’re using something earlier, simply ignore the slot stuff.
Let’s get started!
On the master, do the following:
cat << EOF >> postgresql.org wal _ level = hot _ standby full _ page _ writes = on wal _ log _ hints = on max _ wal _ senders = 6 max _ replication _ slots = 6 hot _ standby = on hot _ standby _ feedback = on EOF
On the master, add the external IP addresses of the servers to
cat << EOF >> pg _ hba.conf host replication repl _ user IP _ of _ master/32 md5 host replication repl _ user IP _ of _ slave/32 md5 EOF
Restart PostgreSQL on the master for the changes to take affect
On the master, create the replication user:
psql -d postgres -U postgres -c "CREATE ROLE repl _ user LOGIN REPLICATION ENCRYPTED PASSWORD 'secretpasswordhere';"
On the master, create the replication slot for the slave:
psql -d postgres -U postgres -c "SELECT * FROM pg _ create _ physical _ replication _ slot('slave1');"
On the slave, wipe the existing cluster:
cd /var/lib/pgsql/9.4/data pg _ ctl -D $PWD -mf stop cd .. rm -rfv data
On the slave, use the
pg_basebackup command to clone the master (enter the repl _ user’s password from above when prompted):
pg _ basebackup -D data -Fp -R -Xs -c fast -l 'initial clone' -P -v -h IP _ of _ master -U repl _ user
On the slave, tweak the
recovery.conf that was created for you and add the replication slot name:
cd data cat << EOF >> recovery.conf primary _ slot _ name = 'slave1' EOF
Start the slave up
And that’s it. You should be all done. Easy, right?