Sunday, 11 August 2024

Configuring Streaming Replication in PostgreSQL 13

 
Table of Contents

1. Introducing Streaming Replication

2. Prepare the environment

3. Execute on Server Master
-- Put PostgreSQL in Archive log mode
-- Parameter wal_level=replica
-- Network configuration between primary and standby

4. Execute on Slave Server
-- Backup and restore 
-- Start database

5. Check the results


================================================

1. Introducing Streaming Replication

Streaming Replication is a feature that helps you build a backup database system for PostgreSQL database. Moreover, it also allows you to query data on the backup database, helping to reduce the load on the main database.

Streaming Replication works by transferring WAL files from the primary database (or master) to the standby database (or slave). Then, apply these WAL files to the standby database. In the documents, this process is often called recovery, apply or replay.


2. Prepare the environment

My simulation environment has 2 servers both with PostgreSQL installed.

1. Server master:

  • IP: 192.168.1.43
  • Operating System: Red Hat Enterprise Linux Server release 8
  • PostgreSQL version: 13

2. Server slave:

  • IP: 192.168.1.44
  • Operating System: Red Hat Enterprise Linux Server release 8
  • PostgreSQL version: 13




Basic Check:-

postgres=# show config_file;
              config_file
----------------------------------------
 /var/lib/pgsql/13/data/postgresql.conf
(1 row)


postgres=# show hba_file ;
              hba_file
------------------------------------
 /var/lib/pgsql/13/data/pg_hba.conf
(1 row)


3. Execute on Server Master

1. PostgreSQL database put in archive mode

Check if the database is in archive mode

# show archive_mode ;
 archive_mode 
--------------
 on
(1 row)

If not, please put the database into Archive mode according to the instructions below.


2. Parameter wal_level=replica

Check the wal_level parameter again to see if the value is already replica.

# show wal_level ;
 wal_level 
-----------
 replica
(1 row)

If not, run the following command to change the parameter:

alter system set wal_level=replica;

And remember to restart the instance for the new value to take effect.

pg_ctl restart
[postgres@rac09-p data]$ pg_ctl -D /var/lib/pgsql/13/data start
waiting for server to start....2024-08-11 11:44:56.305 IST [12573] LOG:  redirecting log output to logging collector process
2024-08-11 11:44:56.305 IST [12573] HINT:  Future log output will appear in directory "log".
 done
server started



3. Network configuration between primary

Create user to sync

postgresql# CREATE USER repuser WITH REPLICATION PASSWORD 'repuser';



Then, you edit the pg_hba.conf file to allow standby to connect to primary via user replication.

vi pg_hba.conf

and add the following line:

host  replication     replication     192.168.1.44/32         trust

[postgres@rac09-p data]$ cat pg_hba.conf
# replication privilege.
local   replication      all                                                   peer
host    replication     all             127.0.0.1/32                   scram-sha-256
host    replication     all             ::1/128                            scram-sha-256
host    replication     repuser     192.168.1.44/32        trust



After editing the pg_hba.conf file , remember to run the following command for the changes to take effect.

pg_ctl reload

Note: The reload option only reloads the parameter values ​​and does not restart the database, so it does not affect any ongoing transactions on the database.


[postgres@rac09-p ~]$ psql -c "alter system set listen_addresses to '*'"

ALTER SYSTEM



4. Execute on Slave Server


Making a Base Backup to Bootstrap the Standby Server (Slave)
- You need to make a base backup of the master server from the standby server, this helps to bootstrap the standby server.

Stop the PostgreSQL severvice 


[postgres@rac10-p data]$ pg_ctl stop -D /var/lib/pgsql/13/data

[postgres@rac10-p ~]$

[postgres@rac10-p 13]$ pwd
/var/lib/pgsql/13
[postgres@rac10-p 13]$ /usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data stop
waiting for server to shut down.... done
server stopped


[postgres@rac10-p ~]$ cd $PGDATA
[postgres@rac10-p data]$ ls -lrt
total 132
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_twophase
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_snapshots
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_serial
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_notify
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_dynshmem
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_commit_ts
-rw-------. 1 postgres postgres     3 Aug 11 00:11 PG_VERSION
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_tblspc
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_replslot
drwx------. 4 postgres postgres  4096 Aug 11 00:11 pg_multixact
-rw-------. 1 postgres postgres 28086 Aug 11 00:11 postgresql.conf
-rw-------. 1 postgres postgres    88 Aug 11 00:11 postgresql.auto.conf
-rw-------. 1 postgres postgres  1636 Aug 11 00:11 pg_ident.conf
-rw-------. 1 postgres postgres  4548 Aug 11 00:11 pg_hba.conf
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_xact
drwx------. 3 postgres postgres  4096 Aug 11 00:11 pg_wal
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_subtrans
drwx------. 5 postgres postgres  4096 Aug 11 00:11 base
drwx------. 2 postgres postgres  4096 Aug 11 00:12 log
-rw-------. 1 postgres postgres    30 Aug 11 08:52 current_logfiles
-rw-------. 1 postgres postgres    58 Aug 11 08:52 postmaster.opts
-rw-------. 1 postgres postgres    99 Aug 11 08:52 postmaster.pid
drwx------. 2 postgres postgres  4096 Aug 11 08:52 pg_stat
drwx------. 2 postgres postgres  4096 Aug 11 08:53 global
drwx------. 4 postgres postgres  4096 Aug 11 08:57 pg_logical
drwx------. 2 postgres postgres  4096 Aug 11 12:51 pg_stat_tmp
[postgres@rac10-p data]$ cd ..
[postgres@rac10-p 13]$ ls -lrt
total 12
drwx------.  2 postgres postgres 4096 Aug  9 03:25 backups
-rw-------.  1 postgres postgres  920 Aug 11 00:11 initdb.log
drwx------. 20 postgres postgres 4096 Aug 11 08:52 data
[postgres@rac10-p 13]$


I have already "data" folder in my server so just moving existing data folder to data_bkp

[postgres@rac10-p 13]$ mv data data_bkp

Create DATA directory once again :-

[postgres@rac10-p 13]$
[postgres@rac10-p 13]$ mkdir -p data/
[postgres@rac10-p 13]$ chmod 700 data/
[postgres@rac10-p 13]$ chown -R postgres:postgres data/
[postgres@rac10-p 13]$
[postgres@rac10-p 13]$



6) 
Then user the pg_basebackup tool to take the base backup with the right ownership.
(the database system under i.e. postgres, within the postgres user account) and with the right permissions.


Below command is  used to setting up the Replication 

[postgres@rac10-p bin]$ pg_basebackup -h 192.168.1.43 -U repuser -p 5432 -D /var/lib/pgsql/13/data -Fp -Xs -P -R

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


With above command, Slave server will connect to the Master (Primary) server and will copy all the data directory from Master server. The connection will be trough a PostgreSQL database user, in our case this user is repuser. And at the end of the command we created the replication slot. What is a replication slot? Basically, the Master server is able to delete the WAL files if it is not needed anymore. But maybe the Slave server did not receive and applied it yet. In this case, the replication will fail. But if we have the replication slot configured, this process will not let the Master server delete the WAL files which did not receive by Slave server yet. At the end the standby.signal file has been created which make it the replica.


Crosscheck the data with is restored or not ..

[postgres@rac10-p bin]$ cd /var/lib/pgsql/13/data
[postgres@rac10-p data]$ ls -lrt
total 268
-rw-------. 1 postgres postgres    224 Aug 11 13:03 backup_label
-rw-------. 1 postgres postgres  28086 Aug 11 13:03 postgresql.conf
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_twophase
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_snapshots
drwx------. 4 postgres postgres   4096 Aug 11 13:03 pg_multixact
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_commit_ts
drwx------. 3 postgres postgres   4096 Aug 11 13:03 pg_wal
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_notify
-rw-------. 1 postgres postgres   4619 Aug 11 13:03 pg_hba.conf
drwx------. 2 postgres postgres   4096 Aug 11 13:03 log
drwx------. 5 postgres postgres   4096 Aug 11 13:03 base
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_xact
-rw-------. 1 postgres postgres      3 Aug 11 13:03 PG_VERSION
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_tblspc
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_subtrans
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_stat_tmp
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_stat
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_serial
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_replslot
drwx------. 4 postgres postgres   4096 Aug 11 13:03 pg_logical
-rw-------. 1 postgres postgres   1636 Aug 11 13:03 pg_ident.conf
drwx------. 2 postgres postgres   4096 Aug 11 13:03 pg_dynshmem
drwx------. 2 postgres postgres   4096 Aug 11 13:03 global
-rw-------. 1 postgres postgres     30 Aug 11 13:03 current_logfiles
-rw-------. 1 postgres postgres      0 Aug 11 13:03 standby.signal
-rw-------. 1 postgres postgres    478 Aug 11 13:03 postgresql.auto.conf
-rw-------. 1 postgres postgres 135417 Aug 11 13:03 backup_manifest




[postgres@rac10-p data]$ cat postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
archive_mode = 'on'
archive_command = 'test ! -f /backup/PostgreSQL13_arch/%f && cp %p //backup/PostgreSQL13_arch/%f'
listen_addresses = '*'
primary_conninfo = 'user=repuser passfile=''/var/lib/pgsql/.pgpass'' channel_binding=prefer host=192.168.1.43 port=5432 sslmode=prefer sslcompression=0 ssl_min_protocol_version=TLSv1.2 gssencmode=prefer krbsrvname=postgres target_session_attrs=any'



Check the PostgreSQL service start or not ..

[postgres@rac10-p data]$ ps -ef| grep postgres
root       10000    9940  0 11:48 pts/0    00:00:00 su - postgres
postgres   10001   10000  0 11:48 pts/0    00:00:00 -bash
postgres   12991   10001  0 13:06 pts/0    00:00:00 ps -ef
postgres   12992   10001  0 13:06 pts/0    00:00:00 grep --color=auto postgres
[postgres@rac10-p data]$


Start the PostgreSQL service as below:-

[postgres@rac10-p data]$ pg_ctl start -D /var/lib/pgsql/13/data
waiting for server to start....2024-08-11 13:06:17.934 IST [13003] LOG:  redirecting log output to logging collector process
2024-08-11 13:06:17.934 IST [13003] HINT:  Future log output will appear in directory "log".
 done
server started



[postgres@rac10-p data]$ ps -ef| grep postgres
root       10000    9940  0 11:48 pts/0    00:00:00 su - postgres
postgres   10001   10000  0 11:48 pts/0    00:00:00 -bash
postgres   13003       1  0 13:06 ?        00:00:00 /usr/pgsql-13/bin/postgres -D /var/lib/pgsql/13/data
postgres   13004   13003  0 13:06 ?        00:00:00 postgres: logger
postgres   13005   13003  0 13:06 ?        00:00:00 postgres: startup recovering 000000010000000000000004
postgres   13006   13003  0 13:06 ?        00:00:00 postgres: checkpointer
postgres   13007   13003  0 13:06 ?        00:00:00 postgres: background writer
postgres   13008   13003  0 13:06 ?        00:00:00 postgres: stats collector
postgres   13009   13003  1 13:06 ?        00:00:00 postgres: walreceiver streaming 0/4000060
postgres   13010   10001  0 13:06 pts/0    00:00:00 ps -ef
postgres   13011   10001  0 13:06 pts/0    00:00:00 grep --color=auto postgres
[postgres@rac10-p data]$

Check receiver background  process started or not :-

[postgres@rac10-p data]$ ps -eaf | grep receiver
postgres   13009   13003  0 13:06 ?        00:00:00 postgres: walreceiver streaming 0/4000060
postgres   13021   10001  0 13:07 pts/0    00:00:00 grep --color=auto receiver
[postgres@rac10-p data]$
[postgres@rac10-p data]$
[postgres@rac10-p data]$ psql
psql (13.16)
Type "help" for help.

postgres=# select pg_is_in_recovery();
 pg_is_in_recovery
-------------------
 t
(1 row)



Primary - Master :-

postgres=# select pg_is_in_recovery();
 pg_is_in_recovery
-------------------
 f


The result f (false) from select pg_is_in_recovery(); indicates that the PostgreSQL server is not in recovery mode. This means the server is running as the primary (or standalone) instance and is not in standby mode, as it would be if it were replicating from another primary server or undergoing a failover recovery process.






Standby - Slave :-

postgres=# select pg_is_in_recovery();
 pg_is_in_recovery
-------------------
 t


The result t (true) from select pg_is_in_recovery(); indicates that the PostgreSQL server is in recovery mode. This typically means the server is running as a standby instance in a replication setup, such as:

  1. Streaming Replication: The server could be continuously replicating data from a primary PostgreSQL instance.
  2. Point-in-Time Recovery: The server might be performing a recovery operation, applying WAL (Write-Ahead Log) files to bring it to a specific state.

While in recovery mode, the server operates in a read-only mode, meaning you won’t be able to perform any write operations. This is normal for standby servers in a replication environment.






Run below query on MASTER server for outgoing replication details:-

postgres=# \x
postgres=# select * from pg_stat_replication;

Run below query on SLAVE server.

postgres=# \x
postgres=# select pg_is_wal_replay_paused();






NOTE: f means, recovery is running fine. t means it is stopped.


Check for last wal received on SLAVE




You can check the Master and Slave databases info by running below command separately on both sides. If your system can not find the pg_controldata program, then you should set the path to this program. Because some of programs include pg_controldata placed under /usr/lib/postgresql/14/bin/ location.



[postgres@rac09-p data]$ pg_controldata -D $PGDATA
pg_control version number:            1300
Catalog version number:               202007201
Database system identifier:           7401588754142404987
Database cluster state:               in production
pg_control last modified:             Sun 11 Aug 2024 01:08:01 PM IST
Latest checkpoint location:           0/4000098
Latest checkpoint's REDO location:    0/4000060
Latest checkpoint's REDO WAL file:    000000010000000000000004
Latest checkpoint's TimeLineID:       1
Latest checkpoint's PrevTimeLineID:   1
Latest checkpoint's full_page_writes: on
Latest checkpoint's NextXID:          0:489
Latest checkpoint's NextOID:          24576
Latest checkpoint's NextMultiXactId:  1
Latest checkpoint's NextMultiOffset:  0
Latest checkpoint's oldestXID:        479
Latest checkpoint's oldestXID's DB:   1
Latest checkpoint's oldestActiveXID:  489
Latest checkpoint's oldestMultiXid:   1
Latest checkpoint's oldestMulti's DB: 1
Latest checkpoint's oldestCommitTsXid:0
Latest checkpoint's newestCommitTsXid:0
Time of latest checkpoint:            Sun 11 Aug 2024 01:08:01 PM IST
Fake LSN counter for unlogged rels:   0/3E8
Minimum recovery ending location:     0/0
Min recovery ending loc's timeline:   0
Backup start location:                0/0
Backup end location:                  0/0
End-of-backup record required:        no
wal_level setting:                    replica
wal_log_hints setting:                off
max_connections setting:              100
max_worker_processes setting:         8
max_wal_senders setting:              10
max_prepared_xacts setting:           0
max_locks_per_xact setting:           64
track_commit_timestamp setting:       off
Maximum data alignment:               8
Database block size:                  8192
Blocks per segment of large relation: 131072
WAL block size:                       8192
Bytes per WAL segment:                16777216
Maximum length of identifiers:        64
Maximum columns in an index:          32
Maximum size of a TOAST chunk:        1996
Size of a large-object chunk:         2048
Date/time type storage:               64-bit integers
Float8 argument passing:              by value
Data page checksum version:           0
Mock authentication nonce:            e45f66c4810dad4d3b1a41c4ca3e1a84d56ea017d71a13e85f3bf365512ac661
[postgres@rac09-p data]$



[postgres@rac10-p data]$ pg_controldata -D $PGDATA
pg_control version number:            1300
Catalog version number:               202007201
Database system identifier:           7401588754142404987
Database cluster state:               in archive recovery
pg_control last modified:             Sun 11 Aug 2024 01:11:18 PM IST
Latest checkpoint location:           0/4000098
Latest checkpoint's REDO location:    0/4000060
Latest checkpoint's REDO WAL file:    000000010000000000000004
Latest checkpoint's TimeLineID:       1
Latest checkpoint's PrevTimeLineID:   1
Latest checkpoint's full_page_writes: on
Latest checkpoint's NextXID:          0:489
Latest checkpoint's NextOID:          24576
Latest checkpoint's NextMultiXactId:  1
Latest checkpoint's NextMultiOffset:  0
Latest checkpoint's oldestXID:        479
Latest checkpoint's oldestXID's DB:   1
Latest checkpoint's oldestActiveXID:  489
Latest checkpoint's oldestMultiXid:   1
Latest checkpoint's oldestMulti's DB: 1
Latest checkpoint's oldestCommitTsXid:0
Latest checkpoint's newestCommitTsXid:0
Time of latest checkpoint:            Sun 11 Aug 2024 01:08:01 PM IST
Fake LSN counter for unlogged rels:   0/3E8
Minimum recovery ending location:     0/4000148
Min recovery ending loc's timeline:   1
Backup start location:                0/0
Backup end location:                  0/0
End-of-backup record required:        no
wal_level setting:                    replica
wal_log_hints setting:                off
max_connections setting:              100
max_worker_processes setting:         8
max_wal_senders setting:              10
max_prepared_xacts setting:           0
max_locks_per_xact setting:           64
track_commit_timestamp setting:       off
Maximum data alignment:               8
Database block size:                  8192
Blocks per segment of large relation: 131072
WAL block size:                       8192
Bytes per WAL segment:                16777216
Maximum length of identifiers:        64
Maximum columns in an index:          32
Maximum size of a TOAST chunk:        1996
Size of a large-object chunk:         2048
Date/time type storage:               64-bit integers
Float8 argument passing:              by value
Data page checksum version:           0
Mock authentication nonce:            e45f66c4810dad4d3b1a41c4ca3e1a84d56ea017d71a13e85f3bf365512ac661


PostgreSQL 13 installation on Linux 8

 
[root@rac10-p ~]#
[root@rac10-p ~]#
[root@rac10-p ~]# cd /etc/yum.repos.d

[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]# ls -lrt
total 12
-rw-r--r--. 1 root root  243 May 16  2023 virt-ol8.repo
-rw-r--r--. 1 root root  941 Jul  8 11:46 uek-ol8.repo
-rw-r--r--. 1 root root 3662 Jul 16 19:07 oracle-linux-ol8.repo
[root@rac10-p yum.repos.d]#

Step 1. Download PostgreSql repository: 
Use OS command dnf to download PostgreSQL repository, 
Please note yum command require Internet Connection on Server. You can use yum repolist command to verify PostgreSQL repository created in Server.


[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]# dnf install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Oracle Linux 8 BaseOS Latest (x86_64)                                                                                                         18 kB/s | 4.3 kB     00:00
Oracle Linux 8 BaseOS Latest (x86_64)                                                                                                        7.0 MB/s |  79 MB     00:11
Oracle Linux 8 Application Stream (x86_64)                                                                                                    52 kB/s | 4.5 kB     00:00
Oracle Linux 8 Application Stream (x86_64)                                                                                                   5.5 MB/s |  62 MB     00:11
Oracle Linux 8 CodeReady Builder (x86_64) - Unsupported                                                                                      9.7 kB/s | 3.8 kB     00:00
Oracle Linux 8 CodeReady Builder (x86_64) - Unsupported                                                                                      5.0 MB/s |  11 MB     00:02
Latest Unbreakable Enterprise Kernel Release 7 for Oracle Linux 8 (x86_64)                                                                   1.9 kB/s | 3.5 kB     00:01
Last metadata expiration check: 0:00:01 ago on Sat 10 Aug 2024 11:43:22 PM IST.
pgdg-redhat-repo-latest.noarch.rpm                                                                                                            18 kB/s |  15 kB     00:00
Dependencies resolved.
=============================================================================================================================================================================
 Package                                       Architecture                        Version                                   Repository                                 Size
=============================================================================================================================================================================
Installing:
 pgdg-redhat-repo                              noarch                              42.0-43PGDG                               @commandline                               15 k
Transaction Summary
=============================================================================================================================================================================
Install  1 Package
Total size: 15 k
Installed size: 15 k
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                     1/1
  Installing       : pgdg-redhat-repo-42.0-43PGDG.noarch                                                                                                                 1/1
  Verifying        : pgdg-redhat-repo-42.0-43PGDG.noarch                                                                                                                 1/1
Installed:
  pgdg-redhat-repo-42.0-43PGDG.noarch
Complete!



[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]# ls -lrt
total 28
-rw-r--r--. 1 root root   243 May 16  2023 virt-ol8.repo
-rw-r--r--. 1 root root 13280 Apr 10 12:30 pgdg-redhat-all.repo
-rw-r--r--. 1 root root   941 Jul  8 11:46 uek-ol8.repo
-rw-r--r--. 1 root root  3662 Jul 16 19:07 oracle-linux-ol8.repo
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#



[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]# dnf repolist
repo id                                                     repo name
ol8_UEKR7                                                   Latest Unbreakable Enterprise Kernel Release 7 for Oracle Linux 8 (x86_64)
ol8_appstream                                               Oracle Linux 8 Application Stream (x86_64)
ol8_baseos_latest                                           Oracle Linux 8 BaseOS Latest (x86_64)
ol8_codeready_builder                                       Oracle Linux 8 CodeReady Builder (x86_64) - Unsupported
pgdg-common                                                 PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64
pgdg12                                                      PostgreSQL 12 for RHEL / Rocky / AlmaLinux 8 - x86_64
pgdg13                                                      PostgreSQL 13 for RHEL / Rocky / AlmaLinux 8 - x86_64
pgdg14                                                      PostgreSQL 14 for RHEL / Rocky / AlmaLinux 8 - x86_64
pgdg15                                                      PostgreSQL 15 for RHEL / Rocky / AlmaLinux 8 - x86_64
pgdg16                                                      PostgreSQL 16 for RHEL / Rocky / AlmaLinux 8 - x86_64
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#



[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]# dnf module disable postgresql
PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                               457  B/s | 659  B     00:01
PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                               2.4 MB/s | 2.4 kB     00:00
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Is this ok [y/N]: y
PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                               143 kB/s | 467 kB     00:03
PostgreSQL 16 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        268  B/s | 659  B     00:02
PostgreSQL 16 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        2.4 MB/s | 2.4 kB     00:00
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Is this ok [y/N]: y
PostgreSQL 16 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        131 kB/s | 418 kB     00:03
PostgreSQL 15 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        544  B/s | 659  B     00:01
PostgreSQL 15 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        2.4 MB/s | 2.4 kB     00:00
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Is this ok [y/N]: y
PostgreSQL 15 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        260 kB/s | 676 kB     00:02
PostgreSQL 14 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        585  B/s | 659  B     00:01
PostgreSQL 14 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        2.4 MB/s | 2.4 kB     00:00
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Is this ok [y/N]: y
PostgreSQL 14 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        155 kB/s | 1.0 MB     00:06
PostgreSQL 13 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        470  B/s | 659  B     00:01
PostgreSQL 13 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        2.4 MB/s | 2.4 kB     00:00
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Is this ok [y/N]: y
PostgreSQL 13 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        428 kB/s | 1.2 MB     00:02
PostgreSQL 12 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        412  B/s | 659  B     00:01
PostgreSQL 12 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        2.4 MB/s | 2.4 kB     00:00
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Is this ok [y/N]: y
PostgreSQL 12 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        491 kB/s | 1.3 MB     00:02
Dependencies resolved.
=============================================================================================================================================================================
 Package                                  Architecture                            Version                                     Repository                                Size
=============================================================================================================================================================================
Disabling modules:
 postgresql
Transaction Summary
=============================================================================================================================================================================
Is this ok [y/N]: y
Complete!





[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]# dnf list "postgresql*-server"
Last metadata expiration check: 0:01:22 ago on Sat 10 Aug 2024 11:48:44 PM IST.
Available Packages
postgresql12-server.x86_64                                                              12.20-2PGDG.rhel8                                                              pgdg12
postgresql13-server.x86_64                                                              13.16-2PGDG.rhel8                                                              pgdg13
postgresql14-server.x86_64                                                              14.13-2PGDG.rhel8                                                              pgdg14
postgresql15-server.x86_64                                                              15.8-1PGDG.rhel8                                                               pgdg15
postgresql16-server.x86_64                                                              16.4-1PGDG.rhel8                                                               pgdg16

**************************************************************************************************************
Step 2.  Install  PostgreSQL Server: Use OS command “dnf” to  install  PostgreSQL Version 13.
**************************************************************************************************************
[root@rac10-p yum.repos.d]# dnf install postgresql13-server postgresql13
Last metadata expiration check: 0:03:12 ago on Sat 10 Aug 2024 11:54:13 PM IST.
Dependencies resolved.
=============================================================================================================================================================================
 Package                                         Architecture                       Version                                         Repository                          Size
=============================================================================================================================================================================
Installing:
 postgresql13                                    x86_64                             13.16-2PGDG.rhel8                               pgdg13                             1.5 M
 postgresql13-server                             x86_64                             13.16-2PGDG.rhel8                               pgdg13                             5.5 M
Installing dependencies:
 postgresql13-libs                               x86_64                             13.16-2PGDG.rhel8                               pgdg13                             420 k
Transaction Summary
=============================================================================================================================================================================
Install  3 Packages
Total download size: 7.4 M
Installed size: 31 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): postgresql13-13.16-2PGDG.rhel8.x86_64.rpm                                                                                             563 kB/s | 1.5 MB     00:02
(2/3): postgresql13-server-13.16-2PGDG.rhel8.x86_64.rpm                                                                                      1.5 MB/s | 5.5 MB     00:03
(3/3): postgresql13-libs-13.16-2PGDG.rhel8.x86_64.rpm                                                                                        102 kB/s | 420 kB     00:04
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                        1.8 MB/s | 7.4 MB     00:04
PostgreSQL 13 for RHEL / Rocky / AlmaLinux 8 - x86_64                                                                                        2.4 MB/s | 2.4 kB     00:00
Importing GPG key 0x08B40D20:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: D4BF 08AE 67A0 B4C7 A1DB CCD2 40BC A2B4 08B4 0D20
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                     1/1
  Installing       : postgresql13-libs-13.16-2PGDG.rhel8.x86_64                                                                                                          1/3
  Running scriptlet: postgresql13-libs-13.16-2PGDG.rhel8.x86_64                                                                                                          1/3
  Installing       : postgresql13-13.16-2PGDG.rhel8.x86_64                                                                                                               2/3
  Running scriptlet: postgresql13-13.16-2PGDG.rhel8.x86_64                                                                                                               2/3
  Running scriptlet: postgresql13-server-13.16-2PGDG.rhel8.x86_64                                                                                                        3/3
  Installing       : postgresql13-server-13.16-2PGDG.rhel8.x86_64                                                                                                        3/3
  Running scriptlet: postgresql13-server-13.16-2PGDG.rhel8.x86_64                                                                                                        3/3
  Verifying        : postgresql13-13.16-2PGDG.rhel8.x86_64                                                                                                               1/3
  Verifying        : postgresql13-libs-13.16-2PGDG.rhel8.x86_64                                                                                                          2/3
  Verifying        : postgresql13-server-13.16-2PGDG.rhel8.x86_64                                                                                                        3/3
Installed:
  postgresql13-13.16-2PGDG.rhel8.x86_64                postgresql13-libs-13.16-2PGDG.rhel8.x86_64                postgresql13-server-13.16-2PGDG.rhel8.x86_64
Complete!
[root@rac10-p yum.repos.d]#
[root@rac10-p yum.repos.d]#

Go to /usr/pgsql-13/bin

[root@rac10-p]# cd /usr/pgsql-13/bin
[root@rac10-p bin]# pwd
/usr/pgsql-13/bin
[root@rac10-p bin]#
[root@rac10-p bin]#
[root@rac10-p bin]# ls -rlt
total 11264
lrwxrwxrwx. 1 root root       8 Aug  9 03:25 postmaster -> postgres
-rwxr-xr-x. 1 root root    9622 Aug  9 03:25 postgresql-13-setup
-rwxr-xr-x. 1 root root    2170 Aug  9 03:25 postgresql-13-check-db-dir
-rwxr-xr-x. 1 root root   81920 Aug  9 03:26 vacuumdb
-rwxr-xr-x. 1 root root   81744 Aug  9 03:26 reindexdb
-rwxr-xr-x. 1 root root  678648 Aug  9 03:26 psql
-rwxr-xr-x. 1 root root 7914808 Aug  9 03:26 postgres
-rwxr-xr-x. 1 root root  102248 Aug  9 03:26 pg_waldump
-rwxr-xr-x. 1 root root   94112 Aug  9 03:26 pg_verifybackup
-rwxr-xr-x. 1 root root  156824 Aug  9 03:26 pg_upgrade
-rwxr-xr-x. 1 root root   38824 Aug  9 03:26 pg_test_timing
-rwxr-xr-x. 1 root root   47336 Aug  9 03:26 pg_test_fsync
-rwxr-xr-x. 1 root root  132048 Aug  9 03:26 pg_rewind
-rwxr-xr-x. 1 root root  187632 Aug  9 03:26 pg_restore
-rwxr-xr-x. 1 root root   68376 Aug  9 03:26 pg_resetwal
-rwxr-xr-x. 1 root root   86088 Aug  9 03:26 pg_receivewal
-rwxr-xr-x. 1 root root   68824 Aug  9 03:26 pg_isready
-rwxr-xr-x. 1 root root  111328 Aug  9 03:26 pg_dumpall
-rwxr-xr-x. 1 root root  431608 Aug  9 03:26 pg_dump
-rwxr-xr-x. 1 root root   72632 Aug  9 03:26 pg_ctl
-rwxr-xr-x. 1 root root   59608 Aug  9 03:26 pg_controldata
-rwxr-xr-x. 1 root root   46992 Aug  9 03:26 pg_config
-rwxr-xr-x. 1 root root   64208 Aug  9 03:26 pg_checksums
-rwxr-xr-x. 1 root root  161456 Aug  9 03:26 pgbench
-rwxr-xr-x. 1 root root  128464 Aug  9 03:26 pg_basebackup
-rwxr-xr-x. 1 root root   43008 Aug  9 03:26 pg_archivecleanup
-rwxr-xr-x. 1 root root  136248 Aug  9 03:26 initdb
-rwxr-xr-x. 1 root root   68816 Aug  9 03:26 dropuser
-rwxr-xr-x. 1 root root   68888 Aug  9 03:26 dropdb
-rwxr-xr-x. 1 root root   77696 Aug  9 03:26 createuser
-rwxr-xr-x. 1 root root   77352 Aug  9 03:26 createdb
-rwxr-xr-x. 1 root root   73200 Aug  9 03:26 clusterdb
[root@rac10-p bin]#


Step 3. Initialize  DB and Enable Auto Restart: Follow the below steps.
==============================================================================
[root@rac10-p ~]#
[root@rac10-p ~]# /usr/pgsql-13/bin/postgresql-13-setup initdb
Initializing database ... OK
[root@rac10-p ~]#
[root@rac10-p ~]#
[root@rac10-p ~]#
[root@rac10-p ~]# systemctl enable postgresql-13
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-13.service → /usr/lib/systemd/system/postgresql-13.service.
[root@rac10-p ~]#
[root@ra
[root@rac10-p ~]#
[root@rac10-p ~]# systemctl start postgresql-13
[root@rac10-p ~]#
[root@rac10-p ~]#
[root@rac10-p ~]#

[root@rac10-p ~]#
[root@rac10-p ~]# ps -ef| grep postgres
postgres    7707       1  0 00:12 ?        00:00:00 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
postgres    7709    7707  0 00:12 ?        00:00:00 postgres: logger
postgres    7711    7707  0 00:12 ?        00:00:00 postgres: checkpointer
postgres    7712    7707  0 00:12 ?        00:00:00 postgres: background writer
postgres    7713    7707  0 00:12 ?        00:00:00 postgres: walwriter
postgres    7714    7707  0 00:12 ?        00:00:00 postgres: autovacuum launcher
postgres    7715    7707  0 00:12 ?        00:00:00 postgres: stats collector
postgres    7716    7707  0 00:12 ?        00:00:00 postgres: logical replication launcher
root        7730    4893  0 00:12 pts/0    00:00:00 grep --color=auto postgres
[root@rac10-p ~]#
[root@rac10-p ~]#
[root@rac10-p ~]#


Step 4. Login to psql: Yum command will take care of all prerequisites and once all above steps are done you will notice OS user:  

****************************************************************************************************************************************
postgres and Binary Location: /usr/pgsql-13/bin/ 
and 
Data Directory Location: /var/lib/pgsql/13/data/ created. 
Perform switch user to  postgres and type psql (terminal-based front-end to PostgreSQL). 
When you enter psql you will be connected to psql terminal with database: 
postgres and user: postgres (Here postgres=# is DBName). Use command \conninfo to get connection details.

Binary Location : 
--------------------------

cd /usr/pgsql-13/bin/
[root@rac10-p ~]# cd /usr/pgsql-13/bin/
[root@rac10-p bin]#
[root@rac10-p bin]#
[root@rac10-p bin]# ls -lrt
total 11264
lrwxrwxrwx. 1 root root       8 Aug  9 03:25 postmaster -> postgres
-rwxr-xr-x. 1 root root    9622 Aug  9 03:25 postgresql-13-setup
-rwxr-xr-x. 1 root root    2170 Aug  9 03:25 postgresql-13-check-db-dir
-rwxr-xr-x. 1 root root   81920 Aug  9 03:26 vacuumdb
-rwxr-xr-x. 1 root root   81744 Aug  9 03:26 reindexdb
-rwxr-xr-x. 1 root root  678648 Aug  9 03:26 psql
-rwxr-xr-x. 1 root root 7914808 Aug  9 03:26 postgres
-rwxr-xr-x. 1 root root  102248 Aug  9 03:26 pg_waldump
-rwxr-xr-x. 1 root root   94112 Aug  9 03:26 pg_verifybackup
-rwxr-xr-x. 1 root root  156824 Aug  9 03:26 pg_upgrade
-rwxr-xr-x. 1 root root   38824 Aug  9 03:26 pg_test_timing
-rwxr-xr-x. 1 root root   47336 Aug  9 03:26 pg_test_fsync
-rwxr-xr-x. 1 root root  132048 Aug  9 03:26 pg_rewind
-rwxr-xr-x. 1 root root  187632 Aug  9 03:26 pg_restore
-rwxr-xr-x. 1 root root   68376 Aug  9 03:26 pg_resetwal
-rwxr-xr-x. 1 root root   86088 Aug  9 03:26 pg_receivewal
-rwxr-xr-x. 1 root root   68824 Aug  9 03:26 pg_isready
-rwxr-xr-x. 1 root root  111328 Aug  9 03:26 pg_dumpall
-rwxr-xr-x. 1 root root  431608 Aug  9 03:26 pg_dump
-rwxr-xr-x. 1 root root   72632 Aug  9 03:26 pg_ctl
-rwxr-xr-x. 1 root root   59608 Aug  9 03:26 pg_controldata
-rwxr-xr-x. 1 root root   46992 Aug  9 03:26 pg_config
-rwxr-xr-x. 1 root root   64208 Aug  9 03:26 pg_checksums
-rwxr-xr-x. 1 root root  161456 Aug  9 03:26 pgbench
-rwxr-xr-x. 1 root root  128464 Aug  9 03:26 pg_basebackup
-rwxr-xr-x. 1 root root   43008 Aug  9 03:26 pg_archivecleanup
-rwxr-xr-x. 1 root root  136248 Aug  9 03:26 initdb
-rwxr-xr-x. 1 root root   68816 Aug  9 03:26 dropuser
-rwxr-xr-x. 1 root root   68888 Aug  9 03:26 dropdb
-rwxr-xr-x. 1 root root   77696 Aug  9 03:26 createuser
-rwxr-xr-x. 1 root root   77352 Aug  9 03:26 createdb
-rwxr-xr-x. 1 root root   73200 Aug  9 03:26 clusterdb
[root@rac10-p bin]#


Data Directory Location : 
--------------------------

[root@rac10-p bin]# cd /var/lib/pgsql/13/data/
[root@rac10-p data]#
[root@rac10-p data]# ls -lrt
total 132
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_twophase
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_snapshots
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_serial
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_notify
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_dynshmem
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_commit_ts
-rw-------. 1 postgres postgres     3 Aug 11 00:11 PG_VERSION
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_tblspc
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_stat
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_replslot
drwx------. 4 postgres postgres  4096 Aug 11 00:11 pg_multixact
-rw-------. 1 postgres postgres 28086 Aug 11 00:11 postgresql.conf
-rw-------. 1 postgres postgres    88 Aug 11 00:11 postgresql.auto.conf
-rw-------. 1 postgres postgres  1636 Aug 11 00:11 pg_ident.conf
-rw-------. 1 postgres postgres  4548 Aug 11 00:11 pg_hba.conf
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_xact
drwx------. 3 postgres postgres  4096 Aug 11 00:11 pg_wal
drwx------. 2 postgres postgres  4096 Aug 11 00:11 pg_subtrans
drwx------. 2 postgres postgres  4096 Aug 11 00:11 global
drwx------. 5 postgres postgres  4096 Aug 11 00:11 base
drwx------. 4 postgres postgres  4096 Aug 11 00:11 pg_logical
drwx------. 2 postgres postgres  4096 Aug 11 00:12 log
-rw-------. 1 postgres postgres    30 Aug 11 00:12 current_logfiles
-rw-------. 1 postgres postgres    58 Aug 11 00:12 postmaster.opts
-rw-------. 1 postgres postgres    99 Aug 11 00:12 postmaster.pid
drwx------. 2 postgres postgres  4096 Aug 11 00:16 pg_stat_tmp
[root@rac10-p data]#



[root@rac10-p bin]# id postgres
uid=26(postgres) gid=26(postgres) groups=26(postgres)
[root@rac10-p bin]#
[root@rac10-p bin]#

Switch to the postgres - user

[root@rac10-p data]#
[root@rac10-p data]# su - postgres
[postgres@rac10-p ~]$
[postgres@rac10-p ~]$
[postgres@rac10-p ~]$
[postgres@rac10-p ~]$ psql
psql (13.16)
Type "help" for help.
postgres=#
postgres=#
postgres=#
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/run/postgresql" at port "5432".
postgres=#
postgres=#



Step 5. Verify  PostgreSql Version: Use command SELECT version() or SHOW server_version.



postgres=# SELECT version();
                                                 version
----------------------------------------------------------------------------------------------------------
 PostgreSQL 13.16 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-22), 64-bit
(1 row)

postgres=# SHOW server_version;
 server_version
----------------
 13.16
(1 row)








Master and Slave - Sync check - PostgreSQL

  1) Run the below Query on Primary:- SELECT     pid,     usename,     application_name,     client_addr,     state,     sync_state,     sen...