Let’s start shall we!

Building the Setup

All the steps below will use the following terms:

dbhost1: first database host running on 192.168.0.100

dbhost2: second database host running on 192.168.0.101

pgbouncer-host: host running pgbouncer on 192.168.0.102

Setup Instructions:

  1. Install dependencies.
  2. Configure ssh access between db nodes.
  3. Configure databases.
  4. Configure PostgreSQL replication.
  5. Configure replication manager.
  6. Clone slave.
  7. Configure pgBouncer.
  8. Failover.

Each step title will have between parentheses the hosts that the step applies to.

1) Install Dependences (dbhost1, dbhost2, pgbouncer-host)

The default ubuntu 14.04 installation does not include PostgreSQL 9.4 so we will need to add the repository:
Create the file /etc/apt/sources.list.d/pgdg.list, and add a line for the repository

[/crayon]
Import the repository signing key, and update the package lists

[/crayon]
Install the dependencies for database hosts (dbhost1,dbhost2)

[/crayon]
Install the dependencies for pgBouncer host (pgbouncer-host)

[/crayon]
2) Configure password-less ssh between dbhosts (dbhost1,dbhost2)

by default postgreSQL creates a postgres local linux user which is used to do all database management operations. To create slaves or recreate a master after failure repmgr requires that the user running the commands have password-less ssh access to the source host. Since the postgres user is the user which has permissions on the postrgres directories, we will configure that user for ssh.
switch to the user

[/crayon]
create ssh key pair

[/crayon]
copy the publich key id_rsa.pub contents and add it to the authorized_keys file in the other host’s postgres user ssh directory. For more details see

test that you can ssh between servers before going to the next step.

3) Configure Databases and users (dbhost1)

We will need two databases one will be used by repmgr and the other will be our application database. All commands are to be executed using the postgres user.

for repmgr database

For our application database
createuser test_user
$ createdb test_db -O test_user

since this user will be used by the applications (clients) we will set a password for it. Run the following command from inside a psql shell with the default postgres user.

[/crayon]
4) Configure PostgreSQL replication (dbhost1)

We will now configure the master db host (dbhost1) for replication. This is acheived by editing the postgresql.conf file and updating the follwing items

[/crayon]
We also need to configure the authentication configuration ( pg_hba.conf) to allow repmgr to access replication database and also allow password based access for our application database (test_db). We will add the following lines:

[/crayon]
restart the postgres service after the changes are done

[/crayon]
5) Configure Replication Manager (dbhost1,dbhost2)

We will need to create a repmgr.conf on each database host. The file could be stored anywhere but we will use /etc/repmgr/repmgr.conf.

The file contents for the dbhost1 should be

[/crayon]
We are now ready to join our master to the replication topology. Using the postgres user run the command

[/crayon]
Видим
2016-10-19 15:45:31] [NOTICE] master node correctly registered for cluster test with id 1 (conninfo: host=dockerbase112 user=repmgr dbname=repmgr)

and similarly for dbhost2 we need to create the file /etc/repmgr/repmgr.confwith the contents

[/crayon]
6) Clone Slave (dbhost2)

ssh into our slave server dbhost

stop the postgresql service

[/crayon]
run the following command using the postgres user to clone the master

[/crayon]
start the postgresql server

[/crayon]
register the slave as a standby

[/crayon]
if all the previous steps ran successfully we should be able to see the state of our replication cluster using the command

[/crayon]
and it should return an output like this

[/crayon]
7) Configure pgBouncer (pgbouncer-host)

All pgBouncer needs is a configuration file pgbouncer.ini which can be stored anywhere in the system. The contents of the file should be

[/crayon]
The configuration above instructs pgBouncer to listen for connections on port 6543 and relay connections to the database test_db to our current master dbhost1 on port 5432.

Because pgBouncer uses md5 based authentication with postgreSQL 8.0 style files we need to create a users.txt file which will have the user credentials for the database users. A sample of this file is below

[/crayon]
To run pgBouncer we run the command

[/crayon]
Congratulations!! you now have a running master-slave replicated postgreSQL setup.

8) Failover (dbhost2,pgbouncer-host)

In case of failover due to the master node failure. All that is needed to do is first make sure the master is truely down. Preferably, stopping the postgresql service if it is running. Then we just need to promote our slave to become the new master using the command:

[/crayon]
and then change the pgBouncer configuration to point to dbhost2 and re run the command

[/crayon]

Проверка репликации

SELECT EXTRACT(EPOCH FROM (now() — pg_last_xact_replay_timestamp()))::INT;