Monday, June 20, 2022

PostgreSQL hot standby. ( DR solution for PostgreSQL)

I followed below steps to create the hot standby.


PRIMARY SERVER - 192.168.230.50

STANDBY SERVER - 192.168.230.51 


On Primary:
===========


postgres=# CREATE USER replica REPLICATION LOGIN ENCRYPTED PASSWORD 'H1j@ck123';

CREATE ROLE


postgres=# \du

                                     List of roles

 Role name  |                         Attributes                         |  Member of

------------+------------------------------------------------------------+--------------

 hecm_read | Password valid until infinity                              | {}

 postgres   | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

 read_user  |                                                            | {readaccess}

 readaccess | Cannot login                                               | {}

 replica    | Replication                                                | {}




Change below settings on postgresql.conf:


archive_mode = on

#archive_command = 'true'

archive_command = 'cp -i %p /u01/PostgreSQL/10/data/archive/%f'

hot_standby = on

max_wal_size = 1GB

max_wal_senders = 10

wal_level = hot_standby

wal_log_hints = on

wal_keep_segments = 10

wal_receiver_status_interval = 5s

hot_standby_feedback = on

listen_addresses = '*' 


Add below entries in pg_hba.conf


host    replication     replica      192.168.230.51/24            md5




On Standby:
===========


Set below parameters in postgresql.conf:


archive_mode = on

#archive_command = 'true'

archive_command = 'cp -i %p /u01/PostgreSQL/10/data/archive/%f'

hot_standby = on


max_wal_size = 1GB

max_wal_senders = 10

wal_level = hot_standby

wal_log_hints = on

wal_keep_segments = 10

wal_receiver_status_interval = 5s

hot_standby_feedback = on



-bash-4.2$ telnet 192.168.230.50 5432

Trying 192.168.230.50...

telnet: connect to address 192.168.230.50: No route to host


Firewall disabled on Primary so it able to connect from standby



-bash-4.2$ ./pg_basebackup -h 192.168.230.50 -D /u01/PostgreSQL/10/data -U replica -v -P

Password:

pg_basebackup: initiating base backup, waiting for checkpoint to complete

pg_basebackup: checkpoint completed

pg_basebackup: write-ahead log start point: 0/15000028 on timeline 1

pg_basebackup: starting background WAL receiver

293182/293182 kB (100%), 1/1 tablespace

pg_basebackup: write-ahead log end point: 0/15000130

pg_basebackup: waiting for background process to finish streaming ...

pg_basebackup: base backup completed


[root@pgstand ~]# cd /u01/PostgreSQL/10/data/

[root@pgstand data]# cat recovery.conf

standby_mode = 'on'

primary_conninfo = 'host=192.168.230.50 port=5432 user=replica password=replicauser@'

restore_command = 'cp ///u01/PostgreSQL/10/data/archive/%f %p'

trigger_file = '/tmp/postgresql.trigger.5432'


-bash-4.2$ ./pg_ctl start -D /u01/PostgreSQL/10/data

waiting for server to start....2020-06-18 18:15:52.293 IST [4117] LOG:  listening on IPv4 address "0.0.0.0", port 5432

2020-06-18 18:15:52.293 IST [4117] LOG:  listening on IPv6 address "::", port 5432

2020-06-18 18:15:52.295 IST [4117] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"

2020-06-18 18:15:52.406 IST [4117] LOG:  redirecting log output to logging collector process

2020-06-18 18:15:52.406 IST [4117] HINT:  Future log output will appear in directory "log".

 done

server started


Now standby is ready as a hot standby.



Check replication status on Primary:
====================================


postgres=# \x

Expanded display is on.

postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+---------------------------------
pid              | 24732
usesysid         | 75624
usename          | replica
application_name | walreceiver
client_addr      | 192.168.230.51
client_hostname  |
client_port      | 41052
backend_start    | 2020-06-18 18:15:52.940476+05:30
backend_xmin     | 219065
state            | streaming
sent_lsn         | 0/16000140
write_lsn        | 0/16000140
flush_lsn        | 0/16000140
replay_lsn       | 0/16000140
write_lag        | 00:00:00.000509
flush_lag        | 00:00:00.00106
replay_lag       | 00:00:00.00122
sync_priority    | 0
sync_state       | async




Testing:
========

Create an object in Primary and see if that replicates to Secondary:

On Primary:
===========

postgres=# create database newtestdb;

CREATE DATABASE



On Standby:
===========


postgres=# \l

                                  List of databases

    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges

------------+----------+----------+-------------+-------------+-----------------------

 newtestdb  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

 newdb      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres         +

            |          |          |             |             | postgres=CTc/postgres+

            |          |          |             |             | readaccess=c/postgres

 postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres         +

            |          |          |             |             | postgres=CTc/postgres+

            |          |          |             |             | readaccess=c/postgres

 template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +

            |          |          |             |             | postgres=CTc/postgres

 template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +

            |          |          |             |             | postgres=CTc/postgres


postgres=# \c newtestdb

You are now connected to database "newtestdb" as user "postgres".


newtestdb=# \dt+ t

 public | t    | table | postgres | 8192 bytes |


newtestdb=# create table D  ( name text);

ERROR:  cannot execute CREATE TABLE in a read-only transaction




Thanks,
























No comments:

Post a Comment