Sunteți pe pagina 1din 33

PostgreSQL

Revision History

Author

Ramesh S Raj
Ramesh S Raj
Ramesh S Raj

Source

Revision

Date

Release
Number

Installation & Configuration


Streaming Replication
Failover

01/17/2014
01/20/2014
03/04/2014

1.0
1.1
1.2

Table of Contents
Introduction ...................................................................................................................................... 4
Download Software .......................................................................................................................... 4
VMware Server Installation .................................................................................................... 4
Install PostgreSQL ....................................................................................................................... 4
Download PostgreSQL Installer for Linux ..................................................................... 5
Verify the Installation ......................................................................................................... 10
Accessing Server from Remote Client ............................................................................. 12
Connecting to Postgres from Client using PgAdminIII ...................................... 14
Load PostgreSQL Sample Database ...................................................................................... 15
Creating a new DVD rental database .............................. 15
Load the DVD rental database .................................... 16
Streaming Replication ............................................. 21
How to do Failover/Switchover ....................................... 30

Installation & Configuration

Introduction
In this tutorial, I will show you how to install PostgreSQL in your
local system/virtual Machine for learning and practicing PostgreSQL.

Download Software
Software required in building your own PostgreSQL Server.
1. Oracle Enterprise Linux Release 6
2. PostgreSQL 9.3

VMware Server Installation


I setup a virtual machine with Oracle Enterprise Linux 6.0 operating
systems.

Install PostgreSQL
There are three steps to complete the PostgreSQL installation:
1. Download PostgreSQL installer for Linux
2. Install PostgreSQL
3. Verify the installation

Download PostgreSQL Installer for Linux


It is recommended to download latest PostgreSQL 9.x from following
link:
http://www.enterprisedb.com/downloads/postgres-postgresql-downloads

Install PostgreSQL step by step

The following illustrates each step and its options for installation.
If you install a different version, you may get additional steps.
Download the PostgreSQL 9.3 downloader for Linux x64 and ftp to the
Server.

Execute the file as shown above.


Click on the next button.

Specify the folder, choose your own or keep the default.

Specify the directory where the data will be stored.

Enter the password for the database superuser(Postgres) and service


account.

Enter the port for PostgreSQL. Make sure that no other applications
are using this port.

Choose the default locale

Click the Next button to let PostgreSQL installer to install


PostgreSQL

Installation may take few minutes to complete

Uncheck Stack Builder check box and Click Finish button to complete
the installation

Verify the Installation


There are several ways to verify the installation. You can try to
connect to the PostgreSQL database server from any client application
e.g., psql, pgAdmin, etc.
The quick way to verify the installation is through the pgAdmin
application.
Click on pgAdmin III to launch it. Enter the connection Parameter as
shown below:

Click on SQL Shell(psql) to launch it

Verify the Version of the server.

Accessing Server from Remote Client


To make our server accessible from the remote client, edit a file
called pg_hba.conf, this basically dictates which users connecting
from which places may access which databases. For simplicity lets
say that everyone connecting with an encrypted password may connect to
any database that they have permissions to.
Edit the File and append the following (marked ones) to the file.

We need to tell it to actually listen on the net. Use VI editor to


open /opt/PostgreSQL/9.3/data/postgresql.conf (path may vary depending
on your installation location)
Change:
#listen_addresses = localhost
on;

# what IP address(es) to listen

To this:
listen_addresses = *

# what IP address(es) to listen on;

After youve saved the file, restart the database server.


Restarting PostgreSQL 9.3 database server

Connecting to Postgres from Client using PgAdminIII


Click on pgAdmin III to launch it from the client machine and, enter
the connection Parameter as shown below:

Youve successfully installed PostgreSQL database server.

Load PostgreSQL Sample Database


I will show you how to load the PostgreSQL sample database into the
PostgreSQL database server.

Creating a new DVD rental database


You need to create a new database in the PostgreSQL database server
before loading database schema and data into the database.
Launch the psql tool.
Enter accounts information to login to the PostgreSQL database server
and enter the following statement to create a new dvdrental database.
CREATE DATABASE dvdrental;
Or Using GUI

Click Ok

PostgreSQL will create a new database named dvdrental.

Load the DVD rental database


Download the attached PostgreSQL DVD Rental sample database

dvdrental.tar

Copy the DVD rental database to a folder and unzip it

You can use pg_restore tool to load data into the dvdrental database
or use pgAdmin to load the data.

Steps to restore the dvdrental data is shown below:

Select the format as directory and in the filename, select the folder
where you have extracted the zip file.

Loaded the dvdrental sample database into the PostgreSQL database


server.

Verify by running the query tool.

Streaming Replication

Streaming Replication
This topic explains, how to setup Streaming replication with Postgres
9.3 on RHEL6 in master slave configuration, also called as streaming
replication. This topic assumes that you have at-least two Postgres 9.3
servers for establishing replication between two database servers.

Master Server Configuration


In Order to enable replication, lets begin with the master server
configuration.
Edit the configuration file:
[root@cauvery data]# vi /opt/PostgreSQL/9.3/data/postgresql.conf

Update the below Variables


wal_level = hot_standby
max_wal_senders = 1
wal_keep_segments = 50
Edit the host based authentication file and add the following:

IP should be standby/slave servers IP.


Restart the Service

We need to take snapshot of data from master and then move that to
slave server.

Enable the backup mode and tar the data folder:

Tar the data folder:

Disable the backup mode.

Move the file to slave server.

Slave Server Configuration


Stop the service on Slave/stand by Server.

Untar the db_backup.tar file to /opt/PostgreSQL/9.3


[root@ganga ~]# tar xvf db_backup.tar

Remove the postmaster.pid from Slave /opt/PostgreSQL/9.3/data folder.


Edit the postgresql.conf, file and enable hot standby mode.
Set up replication-related parameters, connections and authentication
in the standby server like the primary, so that the standby might work
as a primary after failover. Enable read-only queries on the standby
server. But if wal_level is archive on the primary, leave hot_standby
unchanged (i.e., off).

Create a recovery.conf file for this slave server to start receiving


logs from master. Postgres installation comes with a sample recovery
file, copy it from appropriate location to data location as below.
[root@ganga postgresql]# cp
/opt/PostgreSQL/9.3/share/postgresql/recovery.conf.sample
/opt/PostgreSQL/9.3/data/recovery.conf
Edit the /opt/PostgreSQL/9.3/data/recovery.conf file and update the
standby server setting.
standby_mode = on
primary_conninfo = 'host=16.150.56.52 port=5432'

(Primary Server IP)

Change the owner of the recovery.conf file.


Start postgres service in the standby server. It will start streaming
replication.

To test replication, simple add/insert into a table on master server


and query the same from slave server.
Connect to master and do the following.

Connect to Slave and query the same.

You can calculate the replication lag by comparing the current WAL
write location on the primary with the last WAL location
received/replayed by the standby. They can be retrieved using
pg_current_xlog_location on the primary and the
pg_last_xlog_receive_location /pg_last_xlog_replay_location on the
standby, respectively.
On the Primary:
psql -c "SELECT pg_current_xlog_location()" -h16.150.56.52

After Another Insert:

On the Slave:
-bash-4.1$ psql -c "select pg_last_xlog_replay_location()" h16.150.56.53
-bash-4.1$ psql -c "select pg_last_xlog_receive_location()" h16.150.56.53

Also check the progress of streaming replication by using ps command.

Sender Process:

Receiver Process:

Failover

How to do Failover/Switchover
Let us try to simulate disasters in test environment.
Create a recovery command file in the standby server; the following
parameters are required for streaming replication and failover.

At startup, the standby begins by restoring all WAL available in the


archive location, calling restore_command. Once it reaches the end of
WAL available there and restore_command fails, it tries to restore any
WAL available in the pg_xlog directory. If that fails, and streaming
replication has been configured, the standby tries to connect to the
primary server and start streaming WAL from the last valid record
found in archive or pg_xlog. If that fails or streaming replication is
not configured, or if the connection is later disconnected, the
standby goes back to step 1 and tries to restore the file from the
archive again. This loop of retries from the archive, pg_xlog, and via
streaming replication goes on until the server is stopped or failover
is triggered by a trigger file.
If MASTER is not down, make sure you stop it first, before you tell
STANDBY to take up that role. This is to avoid the MASTER from
processing further queries leading to a split-brain problem.
To trigger the failover, simply touch/create the below file.
/tmp/postgresql.trigger.5432
Standby mode is exited and the server switches to normal operation
when pg_ctl promote is run or a trigger file is found (trigger_file).
Before failover, any WAL immediately available in the archive or in
pg_xlog will be restored, but no attempt is made to connect to the
master.
We can tell that STANDBY has become MASTER from the messages in the
log that read archive recovery complete, database system is ready to
accept connections.

Now that STANDBY has turned into MASTER, point your application
servers to it. Even if the old MASTER is running at this point, the
new MASTER is not going to replicate any changes from it.
Fail-over will not be auto the moment the primary DB or Server goes
down. It requires for us to manually intervene to introduce the
trigger_file, to overcome this we can do the following:
Create a Monitoring File in Master to:
1. Check the status of Postgres on Master every Minute/second.
2. When Server goes down, copy the log files from pg_xlog to Remote
Archive directory.
3. Create trigger file in remote machine, this will trigger recovery
on Stand-by and will be the New Master.

-x-

S-ar putea să vă placă și