|
| 1 | +# Postgres Replication Setup |
| 2 | + |
| 3 | +Postgres supports multiple methods of replication. |
| 4 | +The replication method we are going to setup is Streaming Replication or Log Replication. |
| 5 | +Refer [this documentation](https://www.postgresql.org/docs/12/warm-standby.html#STREAMING-REPLICATION) to read more about Streaming Replication. |
| 6 | + |
| 7 | + |
| 8 | +### Steps to set up Streaming Replication in PostgreSQL 12 |
| 9 | + |
| 10 | +#### MASTER SETUP |
| 11 | + |
| 12 | +##### Step 1 |
| 13 | +``` |
| 14 | +Initialize and start PostgreSQL, if not done already on the Master. |
| 15 | +``` |
| 16 | + |
| 17 | +##### Step 2 |
| 18 | +``` |
| 19 | +Modify the parameter listen_addresses to allow a specific IP interface or all (using *). |
| 20 | +Modifying this parameter requires a restart of the PostgreSQL instance to get the change into effect. |
| 21 | +``` |
| 22 | + |
| 23 | +##### Step 3 |
| 24 | +``` |
| 25 | +Create a User for replication in the Master. |
| 26 | +It is discouraged to use superuser postgres in order to setup replication, though it works. |
| 27 | +``` |
| 28 | + |
| 29 | +##### Step 4 |
| 30 | +``` |
| 31 | +Allow replication connections from Standby to Master by appending a similar line as following to the pg_hba.conf file of the Master. |
| 32 | +
|
| 33 | +# Config need to added |
| 34 | +host replication all 0.0.0.0/0 md5 |
| 35 | +``` |
| 36 | + |
| 37 | +---- |
| 38 | + |
| 39 | +#### SLAVE SETUP |
| 40 | + |
| 41 | +##### Step 1 |
| 42 | +``` |
| 43 | +Initialize and start PostgreSQL, if not done already on the Master. |
| 44 | +``` |
| 45 | + |
| 46 | +##### Step 2 |
| 47 | +``` |
| 48 | +Modify the parameter listen_addresses to allow a specific IP interface or all (using *). |
| 49 | +Modifying this parameter requires a restart of the PostgreSQL instance to get the change into effect. |
| 50 | +``` |
| 51 | + |
| 52 | +#### Step 3 |
| 53 | +``` |
| 54 | +Delete everything in the data directory |
| 55 | +``` |
| 56 | + |
| 57 | +##### Step 4 |
| 58 | +``` |
| 59 | +You may use `pg_basebackup` to backup the data directory of the Master from the Standby. |
| 60 | +While creating the backup, you may also tell pg_basebackup to create the replication specific files and entries in the data directory using "-R" . |
| 61 | +``` |
| 62 | + |
| 63 | +---- |
| 64 | + |
| 65 | +### If you think it's a lot of steps to setup the replication ??? |
| 66 | + |
| 67 | +### Answer is here |
| 68 | + |
| 69 | +``` |
| 70 | +All the steps above mentioned has been automated into two steps. |
| 71 | +First step is to deploy the master stack. |
| 72 | +Another step is to deploy the slave stack with the replication configuration. |
| 73 | +
|
| 74 | +Info: Master and slave runs on a two different machines. |
| 75 | +Note: Please modify credentials and port before deploying it |
| 76 | +``` |
| 77 | + |
| 78 | +#### Deploy Master |
| 79 | +Please ensure the data volume path is correct in yml file |
| 80 | +``` |
| 81 | +sudo docker-compose -f master-stack.yml start |
| 82 | +# ensure master stack is up |
| 83 | +``` |
| 84 | + |
| 85 | +#### Deploy slave |
| 86 | +Please ensure the data volume path is correct in yml file |
| 87 | +``` |
| 88 | +sudo docker-compose -f slave-stack.yml start |
| 89 | +# ensure replication works by creating a table in master node and check if the table present in slave node |
| 90 | +``` |
0 commit comments