Project

General

Profile

Support #698

Updated by Daniel Curtis over 8 years ago

{{>toc}} 

 This is a guide for setting up master-slave replication with PostgreSQL 9.4 on Debian 8. 

 *NOTE*: The example servers will be  
 # pg1.example.com as the *master* at 192.168.0.10 
 # pg2.example.com as the *slave* at 192.168.0.20 

 h2. Prepare Master Server 

 * Enable the contributor apt repositories: 
 <pre> 
 sudo sed -i '' -e 's/main/main\ contrib/' /etc/apt/sources.list 
 </pre> 

 * Make sure the system is up to date: 
 <pre> 
 sudo apt-get update && sudo apt-get upgrade 
 </pre> 

 * Install postgresql: 
 <pre> 
 sudo apt-get install postgresql postgresql-contrib postgresql-client rsync 
 </pre> 

 * Set a password for the postgresql admin user: 
 <pre> 
 sudo passwd postgres 
 </pre> 

 * Switch to Postgres account: 
 <pre> 
 sudo su - postgres 
 </pre> 

 * Generate ssh key: 
 <pre> 
 ssh-keygen 
 </pre> 

 * Copy SSH key to opposite server: 
 <pre> 
 ssh-copy-id pg2.example.com 
 </pre> 
 *NOTE*: Make sure to see the postgres password on pg2.example.com before copying the SSH id 

 * Create an account for replication 
 <pre> 
 psql -c "CREATE USER replicator REPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD 'SuperSecretReplicationPassword';" 
 </pre> 

 * Exit from the postgres user: 
 <pre> 
 exit 
 </pre> 

 * Modify postgres access config file: 
 <pre> 
 sudo nano vi /etc/postgresql/9.4/main/pg_hba.conf 
 </pre> 
 #* Add content below: 
 <pre> 
 host      replication       replicator rep       192.168.0.20/32     md5 
 </pre> 

 * Then modify postgres config file: 
 <pre> 
 sudo nano vi /etc/postgresql/9.4/main/postgresql.conf 
 </pre> 
 #* The config value should be: 
 <pre> 
 listen_addresses = 'localhost,192.168.0.10' 
 wal_level = 'hot_standby' 
 archive_mode = on 
 archive_command = 'cd .' 
 max_wal_senders = 1 
 hot_standby = on 
 </pre> 

 * Restart Master server: 
 <pre> 
 sudo service postgresql restart 
 </pre> 

 h2. Prepare Slave Server 

 * Enable the contributor apt repositories: 
 <pre> 
 sudo sed -i '' -e 's/main/main\ contrib/' /etc/apt/sources.list 
 </pre> 

 * Make sure the system is up to date: 
 <pre> 
 sudo apt-get update && sudo apt-get upgrade 
 </pre> 

 * Install postgresql: 
 <pre> 
 sudo apt-get install postgresql postgresql-contrib postgresql-client rsync 
 </pre> 

 * Set a password for the postgresql admin user: 
 <pre> 
 sudo passwd postgres 
 </pre> 

 * Switch to Postgres account: 
 <pre> 
 sudo su - postgres 
 </pre> 

 * Generate ssh key: 
 <pre> 
 ssh-keygen 
 </pre> 

 * Copy SSH key to opposite server: 
 <pre> 
 ssh-copy-id pg1.example.com 
 </pre> 
 *NOTE*: Make sure to see the postgres password on pg1.example.com before copying the SSH id 

 * Create an account for replication 
 <pre> 
 psql -c "CREATE USER replicator REPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD 'SuperSecretReplicationPassword';" 
 </pre> 

 * Exit from the postgres user: 
 <pre> 
 exit 
 </pre> 

 * Stop PostgreSQL: 
 <pre> 
 sudo service postgres stop 
 </pre> 

 * Modify postgres access config file: 
 <pre> 
 sudo nano vi /etc/postgresql/9.4/main/pg_hba.conf 
 </pre> 
 #* Add content below: 
 <pre> 
 host      replication       rep       192.168.0.10/32    md5 
 </pre> 

 * And modify postgres config file: 
 <pre> 
 sudo nano vi /etc/postgresql/9.4/main/postgresql.conf 
 </pre> 
 #* The config value should be: 
 <pre> 
 listen_addresses = 'localhost,192.168.0.20' 
 wal_level = 'hot_standby' 
 archive_mode = on 
 archive_command = 'cd .' 
 max_wal_senders = 1 
 hot_standby = on 
 </pre> 

 h2. Start the Replication 

 h3. Master Server 

 * Begin the replication 
 <pre> 
 sudo su - postgres 

 psql -c "select pg_start_backup('initial_backup');" 

 
 rsync -cva --inplace --exclude=*pg_xlog* /var/lib/postgresql/9.4/main/ -e ssh pg2.example.com:main/ 

 pg2.example.com:/var/lib/postgresql/9.4/main/ 
 psql -c "select pg_stop_backup();" 

 exit 
 </pre> 

 h3. Slave Server 

 * Create recovery.conf file: 
 <pre> 
 sudo nano vi /var/lib/postgresql/9.4/main/recovery.conf 
 </pre> 
 #* And add the content below to that file: 
 <pre> 
 standby_mode = 'on' 
 primary_conninfo = 'host=192.168.0.10 port=5432 user=replicator password=SuperSecretReplicationPassword' 
 trigger_file = '/tmp/postgresql.trigger.5432' 
 </pre> 

 * And restart Slave Server to complete the setup: 
 <pre> 
 sudo service postgresql start 
 </pre> 

 h2. Resources 

 * https://sonnguyen.ws/replication-master-slave-with-postgresql-9-4-in-ubuntu-14-04/ 
 * https://www.debian-administration.org/article/677/How_to_setup_Postgresql_9.1_Streaming_Replication_Debian_Squeeze

Back