Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Asynchronous Master-Slave Replication of PostgreSQL Databases in One Click

DZone's Guide to

Asynchronous Master-Slave Replication of PostgreSQL Databases in One Click

Replicating your data has obvious benefits, but can be a bit tedious to setup. Take a look at how to easily set it up using PostgreSQL and Jelastic's cloud.

· Database Zone
Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

As a critical component for most applications, data has to be available for end users in a dynamically changing environment. Storing data in one location is a subject to risk in case of a system failure. Therefore, you need to come up with a strategy on how to ensure high availability — and one of the solutions is to maintain the data on multiple synchronized servers.

Having your data replicated to another database can be very helpful for a variety of reasons, such as improving performance, running backup services, analyzing information, and by all means alleviating system failures.

Among numerous database clustering solutions, Jelastic provides the PostgreSQL package with master-slave asynchronous replication that can be installed automatically and run with no additional configurations required.

Now let’s take a quick look at the package specs and follow the step-by-step installation guide.

The presented PostgreSQL Replication solution for one-click installation is based on Jelastic certified PostgreSQL 9.5.5 template. By default, it deploys two database containers (one per role: master and slave) with asynchronous replication between them.

The primary (master) server runs active database which handles read-write operations, whilst the standby (slave) server operates with this DB copy and handles read-only operations. Therefore, if the primary database fails, the system will run data from the fail-over slave container.

Asynchronous Master-Slave Replication

Within the package, each database container has a default vertical scaling from 1 reserved to 24 dynamic cloudlets (up to 3 GiB of RAM and 9.6 GHz of CPU) that are allocated dynamically based on the incoming load. Subsequently, you can change the resource allocation limit by following the above-linked guide.

Now, let’s proceed further to get the PostgreSQL package up and running in the Cloud.

Deploying PostgreSQL Replication Package

The package installation is a fairly simple process that you can perform in just a few clicks.

  1. Log into the Jelastic Platform with your credentials and click Marketplace in the top pane.

    marketplace

    Within the Apps section, use the search box to find the PostgreSQL Database Replication package and click Install.

    Tip:  The PostgreSQL Replication package sources are available within  Jelastic JPS Collection alongside with other useful solutions, any of which can be integrated through  importing a  manifest.jps file from the appropriate repository.
  2. In the installation window, type Environment name and Display Name (alias), select a Region (if several regions are available) and click Install.

    installation

    When the package installation is completed, you’ll see a confirmation message with the master node URL and simultaneously receive an email notification with your administration data.

    success

    Now, let’s put the master database into action to check that data is successfully replicated to the slave.

    To ensure that data is replicated properly, we’ll create a new DB instance in the master container and check its presence inside the slave.

Create New PG Database

  1. Click Open in browser next to the PostgreSQL master node to launch the phpPgAdmin web interface.

    postgresql admin

  2. Under Servers, click PostgreSQL and log in with credentials you’ve received via email after the package installation.

    login postgresql

  3. Click Create database, fill in the Name box (e.g. Jelastic), and click Create.  

    create database

    Now, when the database is created in the master node, let’s check the replicated data within the slave container.

Check the Replication

To ensure that data replication from the master to the slave container works as intended, perform the following steps.

  1. Click Open in browser next to the PostgreSQL slave node to launch the phpPgAdmin web interface.

    open postgresql admin panel

  2. Access the admin interface with the same username and password that you’ve received via email after the package installation.

    postgresql database

    Once logged in, you can see and manage the database (Jelastic, in our example) that was earlier created in the master container.

That’s it! The PostgreSQL environment with master-slave asynchronous replication is up and ready for data processing.

As a next step, you can already connect these PostgreSQL replicated databases to your project. The process depends on application specifics, so follow the instructions below for:

Create flexible schemas using dynamic columns for semi-structured data. Learn how.

Topics:
replication ,postgresql ,database ,master-slave ,tutorial

Published at DZone with permission of Tetiana Markova, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}