PostgreSQL : Load Balancing and Replication with PgPool II-92 (Part 3)

Previous : Part 2  http://willyristanto.blogspot.com/2014/10/clustering-postgresql-with-pgpool-ii-92_15.html

12. Testing Replication

Now, we can test replication via application. First, make sure connection to pgpool is OK













Execute query:
CREATE TABLE "public"."test_1" (
    "id"      int NULL primary key,
    "name"    varchar(25) NULL
    )

INSERT INTO "public"."test_1"("id", "name")
VALUES(1, 'one')
;
INSERT INTO "public"."test_1"("id", "name")
VALUES(2, 'two')
;

when we check both on server postgres1 and server postgres2


references :
http://www.pgpool.net/docs/latest/tutorial-en.html#install
http://www.keyup.eu/en/blog/89-replication-and-load-balancing-with-postgresql-and-pgpool2
http://www.milliondollarserver.com/install-postgresql-9-2-on-oracle-enterprise-linux-centos-redhat-6-3/

PostgreSQL : Load Balancing and Replication with PgPool II-92 (Part 2)

Part 1 : http://willyristanto.blogspot.com/2014/10/clustering-postgresql-with-pgpool-ii-92.html

5. Install pgpool
we can download pgpool from here :
http://www.pgpool.net/mediawiki/index.php/Downloads

[root@postgres1 pgp]# ls
pgpool-II-92-3.3.2-1.rhel6.x86_64.rpm
pgpool-II-92-debuginfo-3.3.2-1.rhel6.x86_64.rpm
pgpool-II-92-devel-3.3.2-1.rhel6.x86_64.rpm
[root@postgres1 pgp]#yum install pgpool-II-92-3.3.2-1.rhel6.x86_64.rpm
[root@postgres1 pgp]#yum install pgpool-II-92-debuginfo-3.3.2-1.rhel6.x86_64.rpm
[root@postgres1 pgp]#yum install pgpool-II-92-debuginfo-3.3.2-1.rhel6.x86_64.rpm

6. Edit file pgpool-recovery.sql and pgpool-regclass.sql
[root@postgres1 pgp]# nano /usr/pgsql-9.2/share/extension/pgpool-recovery.sql
CREATE OR REPLACE FUNCTION pgpool_recovery(text, text, text)
RETURNS bool
AS '/usr/pgsql-9.2/lib/pgpool-recovery', 'pgpool_recovery'
LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION pgpool_remote_start(text, text)
RETURNS bool
AS '/usr/pgsql-9.2/lib/pgpool-recovery', 'pgpool_remote_start'
LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION pgpool_pgctl(text, text)
RETURNS bool
AS '/usr/pgsql-9.2/lib/pgpool-recovery', 'pgpool_pgctl'
LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION pgpool_switch_xlog(text)
RETURNS text
AS '/usr/pgsql-9.2/lib/pgpool-recovery', 'pgpool_switch_xlog'
LANGUAGE C STRICT;

make sure file pgpool-recovery.so is exist in the correct path

[root@postgres1 lib]# nano /usr/pgsql-9.2/share/extension/pgpool-regclass.sql
CREATE OR REPLACE FUNCTION pg_catalog.pgpool_regclass(cstring)
RETURNS oid
AS '/usr/pgsql-9.2/lib/pgpool-regclass', 'pgpool_regclass'
LANGUAGE C STRICT;
make sure file pgpool-regclass.so is exist in the correct path

7. Create Database
[root@postgres1 pgp]# su - postgres
-bash-4.1$ psql
psql (9.2.9)
Type "help" for help.

postgres=# create database mydb owner postgres;
CREATE DATABASE
postgres=#

8. Execute function on database
-bash-4.1$ psql -h localhost -U postgres -W -f /usr/pgsql-9.2/share/extension/pgpool-recovery.sql mydb                                                      
Password for user postgres:
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION

-bash-4.1$ psql -h localhost -U postgres -W -f /usr/pgsql-9.2/share/extension/pgpool-regclass.sql mydb
Password for user postgres:
CREATE FUNCTION
-bash-4.1$

9. Install pgpool on pgpool server

[root@pgpserv pgp]# ls
pgpool-II-92-3.3.2-1.rhel6.x86_64.rpm
pgpool-II-92-debuginfo-3.3.2-1.rhel6.x86_64.rpm
pgpool-II-92-devel-3.3.2-1.rhel6.x86_64.rpm
[root@pgpserv pgp]#yum install pgpool-II-92-3.3.2-1.rhel6.x86_64.rpm
[root@pgpserv pgp]#yum install pgpool-II-92-debuginfo-3.3.2-1.rhel6.x86_64.rpm
[root@pgpserv pgp]#yum install pgpool-II-92-debuginfo-3.3.2-1.rhel6.x86_64.rpm

Login to server postgres database, we need to know passwd from pg_shadow from user postgres
mydb=# select passwd from pg_shadow where usename = 'postgres';
               passwd
-------------------------------------
 md53175bce1d3201d16594cebf9d7eb3f9d

back to pgpool server, edit file pcp.conf
#nano /etc/pgpool-II-92/pcp.conf

append line
postgres:3175bce1d3201d16594cebf9d7eb3f9d

please note that we get value 3175bce1d3201d16594cebf9d7eb3f9d from passwd_from pg_shadow (string "md5" is omitted)

10. Edit pgpool.conf
[root@pgpserv pgpool-II-92]# nano /etc/pgpool-II-92/pgpool.conf

listen_addresses = '*'
port = 9999
backend_hostname0 = '192.168.112.142'                              
backend_port0 = 5432                            
backend_weight0 = 1                             
backend_data_directory0 = '/var/lib/pgsql/9.2/data'                              
backend_flag0 = 'ALLOW_TO_FAILOVER'                                  
backend_hostname1 = '192.168.112.140'
backend_port1 = 5432
backend_weight1 = 1
backend_data_directory1 = '/var/lib/pgsql/9.2/data'
backend_flag1 = 'ALLOW_TO_FAILOVER'
connection_cache = on
replication_mode = true
load_balance_mode = true

11. Start pgpool
we can start pgpool by simply command
#pgpool


stop pgpool
#pgpool stop
or we can stop immediately
#pgpool -m fast stop

Next Part 3 :  http://willyristanto.blogspot.com/2014/10/clustering-postgresql-with-pgpool-ii-92_94.html

PostgreSQL : Load Balancing and Replication with PgPool II-92 (Part 1)

 Step by step configuration setup load balancing and replication postgresql with pgpool.


1. Install Postgresql 
install both on 192.168.112.142 and 192.168.112.140

# sed -i '7iexclude=postgresql*' /etc/yum.repos.d/public-yum-ol6.repo
# rpm -ivh http://yum.pgrpms.org/9.2/redhat/rhel-6-x86_64/pgdg-redhat92-9.2-7.noarch.rpm
# yum install postgresql92 postgresql92-libs postgresql92-server postgresql92-devel
# chkconfig postgresql-9.2 on
# service postgresql-9.2 initdb
# service postgresql-9.2 start

2. Edit /etc/hosts on each server
192.168.112.142 postgres1
192.168.112.140 postgres2
192.168.112.141 pgpserv

3. Edit postgresql.conf
nano /var/lib/pgsql/9.2/data/postgresql.conf

modify listen_address and port
listen_addresses = '*'                
port = 5432                            

4. Edit pg_hba.conf











Change all method to trust

Restart service postgresql to take effect
[root@postgres1 ~]# /etc/init.d/postgresql-9.2 restart
Stopping postgresql-9.2 service:                           [  OK  ]
Starting postgresql-9.2 service:                             [  OK  ]

Next : Part 2 http://willyristanto.blogspot.com/2014/10/clustering-postgresql-with-pgpool-ii-92_15.html