PgSQL Replication

From IMSMA Wiki
Revision as of 09:36, 6 August 2014 by Evinek (talk | contribs)
Jump to navigation Jump to search

This page describes how to set up direct replication with pgSQL. Generally, replication allows having two database servers (typically one for data entry and one for reporting) that stay synchronised. Concretely, when data is added, deleted or updated on the master server, the changes get immediately replicated on the slave or standby server. The standby database is read-only, i.e. no changes can be made directly there - only changes on the master are being replicated. Currently (August 2014) this mechanism is only in place at MACCA in Kabul.

Steps for stetting up the replication with pgSQL

On the master server, with IMSMA database already installed:

  1. Create a new user on the database by running the following SQL on the IMSMA database:
    CREATE USER replicator REPLICATION LOGIN ENCRYPTED PASSWORD 'password';
  2. Uncomment and edit the following lines in the postgresql.conf file in C:/IMSMAng/pgsql/data:
    153 wal_level = hot_standby
    174 checkpoint_segments = 8
    196 max_wal_senders = 3
    199 wal_keep_segments = 8
    
  3. Add the following line in the pg_hba.conf file in C:/IMSMAng/pgsql/data:
    host replication replicator <standby-ip>/32 trust
    
    <standy-ip> is the IP address of the standby server (to be determined by running the command
    ipconfig /all
    in a command window)
  4. Restart the PGSQL service

On the standby server, also with IMSMA installed:

  1. Stop the PGSQL service and delete all the contents of C:/IMSMAng/pgsql/data
  2. Open a command window, cd into the directory C:/IMSMAng/pgsql/bin and run the following command:
    pg_basebackup –h <master-ip> -D c:/IMSMAng/pgsql/data –U replicator –v –P
    

    This command makes the initial synchronisation, i.e. copies the entire database from the master to the standby. This can take some time, depending on the size of the database. The progress can be followed on the command window as shown below.

    The hot standby replication mechanism can only be applied to the entire database server, i.e. all the databases from the master will be replication to the standby -- not only the database imsma but any database that is running on the master.

Template:NavBox Hub