|
| 1 | +# Stream Replication Setup for PGSQL |
| 2 | + |
| 3 | +1. Install postgres in the primary and standby server as usual. This requires only configure, make and make install. |
| 4 | +2. Create the initial database cluster in the primary server as usual, using initdb. |
| 5 | +3. Create an user named replication with REPLICATION privileges. |
| 6 | + |
| 7 | +``` |
| 8 | +CREATE ROLE replication WITH REPLICATION PASSWORD 'password' LOGIN |
| 9 | +``` |
| 10 | +4. Set up connections and authentication on the primary so that the standby server can successfully connect to the replication pseudo-database on the primary. |
| 11 | +$ $EDITOR postgresql.conf |
| 12 | + |
| 13 | +listen_addresses = '192.168.0.10' |
| 14 | +$ $EDITOR pg_hba.conf |
| 15 | + |
| 16 | +# The standby server must connect with a user that has replication privileges. |
| 17 | +# TYPE DATABASE USER ADDRESS METHOD |
| 18 | + host replication replication 192.168.0.20/32 md5 |
| 19 | + |
| 20 | +The following parameters on the master are considered as mandatory when setting up streaming replication. |
| 21 | + |
| 22 | +archive_mode : Must be set to ON to enable archiving of WALs. |
| 23 | +wal_level : Must be at least set to hot_standby until version 9.5 or replica in the later versions. |
| 24 | +max_wal_senders : Must be set to 3 if you are starting with one slave. For every slave, you may add 2 wal senders. |
| 25 | +wal_keep_segments : Set the WAL retention in pg_xlog (until PostgreSQL 9.x) and pg_wal (from PostgreSQL 10). Every WAL requires 16MB of space unless you have explicitly modified the WAL segment size. You may start with 100 or more depending on the space and the amount of WAL that could be generated during a backup. |
| 26 | +archive_command : This parameter takes a shell command or external programs. It can be a simple copy command to copy the WAL segments to another location or a script that has the logic to archive the WALs to S3 or a remote backup server. |
| 27 | +listen_addresses : Specifies which IP interfaces could accept connections. You could specify all the TCP/IP addresses on which the server could listen to connections from client. ‘*’ means all available IP interfaces. The default : localhost allows only local TCP/IP connections to be made to the postgres server. |
| 28 | +hot_standby : Must be set to ON on standby/replica and has no effect on the master. However, when you setup your replication, parameters set on the master are automatically copied. This parameter is important to enable READS on slave. Otherwise, you cannot run your SELECT queries against slave. |
| 29 | + |
| 30 | +$ $EDITOR postgresql.conf |
| 31 | + |
| 32 | +# To enable read-only queries on a standby server, wal_level must be set to |
| 33 | +# "hot_standby". But you can choose "archive" if you never connect to the |
| 34 | +# server in standby mode. |
| 35 | +wal_level = hot_standby |
| 36 | + |
| 37 | +# Set the maximum number of concurrent connections from the standby servers. |
| 38 | +max_wal_senders = 5 |
| 39 | + |
| 40 | +# To prevent the primary server from removing the WAL segments required for |
| 41 | +# the standby server before shipping them, set the minimum number of segments |
| 42 | +# retained in the pg_xlog directory. At least wal_keep_segments should be |
| 43 | +# larger than the number of segments generated between the beginning of |
| 44 | +# online-backup and the startup of streaming replication. If you enable WAL |
| 45 | +# archiving to an archive directory accessible from the standby, this may |
| 46 | +# not be necessary. |
| 47 | +wal_keep_segments = 32 |
| 48 | + |
| 49 | +# Enable WAL archiving on the primary to an archive directory accessible from |
| 50 | +# the standby. If wal_keep_segments is a high enough number to retain the WAL |
| 51 | +# segments required for the standby server, this is not necessary. |
| 52 | +archive_mode = on |
| 53 | +archive_command = 'cp %p /path_to/archive/%f' |
| 54 | + |
| 55 | +ALTER SYSTEM SET wal_level TO 'hot_standby'; |
| 56 | +ALTER SYSTEM SET archive_mode TO 'ON'; |
| 57 | +ALTER SYSTEM SET max_wal_senders TO '5'; |
| 58 | +ALTER SYSTEM SET wal_keep_segments TO '10'; |
| 59 | +ALTER SYSTEM SET listen_addresses TO '*'; |
| 60 | +ALTER SYSTEM SET hot_standby TO 'ON'; |
| 61 | + |
| 62 | +psql -U postgres -p 5432 -c "select pg_reload_conf()" |
| 63 | + |
| 64 | +On Slave |
| 65 | + |
| 66 | +pg_basebackup -h 192.168.0.28 -U replicator -p 5432 -D $PGDATA -P -Xs -R |
| 67 | + |
| 68 | +Potential error |
| 69 | + |
| 70 | +pg_basebackup: error: directory "/var/lib/pgsql/12/data" exists but is not empty |
| 71 | + |
| 72 | +Create a recovery command file in the standby server; the following parameters are required for streaming replication. |
| 73 | +$ $EDITOR recovery.conf |
| 74 | +# Note that recovery.conf must be in $PGDATA directory. |
| 75 | +# It should NOT be located in the same directory as postgresql.conf |
| 76 | + |
| 77 | +# Specifies whether to start the server as a standby. In streaming replication, |
| 78 | +# this parameter must to be set to on. |
| 79 | +standby_mode = 'on' |
| 80 | + |
| 81 | +# Specifies a connection string which is used for the standby server to connect |
| 82 | +# with the primary. |
| 83 | +primary_conninfo = 'host=192.168.0.10 port=5432 user=replication password=password' |
| 84 | + |
| 85 | +# Specifies a trigger file whose presence should cause streaming replication to |
| 86 | +# end (i.e., failover). |
| 87 | +trigger_file = '/path_to/trigger' |
| 88 | + |
| 89 | +# Specifies a command to load archive segments from the WAL archive. If |
| 90 | +# wal_keep_segments is a high enough number to retain the WAL segments |
| 91 | +# required for the standby server, this may not be necessary. But |
| 92 | +# a large workload can cause segments to be recycled before the standby |
| 93 | +# is fully synchronized, requiring you to start again from a new base backup. |
| 94 | +restore_command = 'cp /path_to/archive/%f "%p"' |
| 95 | + |
| 96 | +Start postgres in the standby server. It will start streaming replication. |
| 97 | + |
| 98 | +How to do failover |
| 99 | +Create the trigger file in the standby after the primary fails. |
| 100 | +How to stop the primary or the standby server |
| 101 | +Shut down it as usual (pg_ctl stop). |
| 102 | +How to restart streaming replication after failover |
| 103 | +Repeat the operations from 6th; making a fresh backup, some configurations and starting the original primary as the standby. The primary server doesn't need to be stopped during these operations. |
0 commit comments