Support #698
Updated by Daniel Curtis about 9 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 the Master PostgreSQL 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 </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> * Create an account for replication <pre> psql -c "CREATE USER replicator REPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD 'SuperSecretReplicationPassword';" </pre> * Modify postgres access config file: <pre> vi /etc/postgresql/9.4/main/pg_hba.conf </pre> #* Add content below: <pre> host replication rep 192.168.0.20/32 md5 </pre> * Then modify postgres config file: <pre> 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> service postgresql restart </pre> h2. Prepare the Slave PostgreSQL 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 </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> * Create an account for replication <pre> psql -c "CREATE USER replicator REPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD 'SuperSecretReplicationPassword';" </pre> * Stop PostgreSQL: <pre> service postgres stop </pre> * Modify postgres access config file: <pre> 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> 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> psql -c "select pg_start_backup('initial_backup');" rsync -cva --inplace --exclude=*pg_xlog* /var/lib/postgresql/9.4/main/ pg2.example.com:/var/lib/postgresql/9.4/main/ psql -c "select pg_stop_backup();" </pre> h3. Slave Server * Create recovery.conf file: <pre> 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> 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