ROOT User Approach – How to Install and Configure PostgreSQL 13 in RHEL 7 –

How to install PostgreSQL 13 in REHL 7 From Repository

PostgreSQL can be installed by means of two ways

  1. Installing from source
  2. Installing binary packages

Each method has its own advantages and disadvantages, however, we will limit this post to learn how to install PostgreSQL using binaries packages.

Follow the steps here to get the automated scripts to install PostgreSQL 13 in RHEL 7.

Install PostgreSQL 13 – Yum Repository Method [step by step]:

I assume that all the prerequisites for installing PostgreSQL have met, I highly recommend to follow this link to learn more about prerequisites.

To use the PostgreSQL Yum Repository, follow these steps:

Open the following link to select Linux version.

Choose the following things from the repository list

  1. Select Version (I choose 13)
  2. Select Platform  (I choose Rhel 7)
  3. Select Architecture  ( I choose x86_64)

Install the repository RPM:

Install PostgreSQL 13:

The output looks like this

 

The above step completes PostgreSQL 13 Server installation. It installs below packages

postgresql13: Key clients and libraries, and documentation
postgresql13-server: Server executables and data files
postgresql13-libs: Client shared libraries

How to initialize the PostgreSQL 13 database cluster

Once the software is installed, as a root initialize the cluster with below command.

/usr/pgsql-13/bin/postgresql-13-setup initdb

Then you will need to start PostgreSQL with below command

systemctl start postgresql-13.service

Upon successful cluster startup you can check the postmaster status with

If you want your postmaster to startup during the subsequent machine reboots run the below command

systemctl enable postgresql-13.service

Post Installation checks:

  • The user ‘postgres’ is created during installation of the server subpackage.
    This user by default is UID and GID 26. The user has the default shell set to
    bash, and the home directory set to /var/lib/pgsql.
  • This user also has no default password. If you want to be able to su to it from a non-root account or login as ‘postgres’ you will need to set a password using passwd.

  • The file /var/lib/pgsql/13/.bash_profile is packaged to help with the
    setting of environment variables.Users should not edit this file, because
    it may be overwritten during every new installation.

  • Default data directory location is /var/lib/pgsql/13/data

How to initialize PostgreSQL 13 database cluster in a non default location

Stop PostgreSQL 13 database cluster if it is already running

systemctl stop postgresql-13.service

Create a cluster directory and provide necessary permissions.

mkdir -p /u01/pgsql/13
chown -R postgres:postgres /u01/pgsql/13
chmod 750 /u01/pgsql/13

Then, customize the systemd service by changing PGDATA section.

vi /lib/systemd/system/postgresql-13.service

Reload systemd:

systemctl daemon-reload

Initialize the cluster:

/usr/pgsql-13/bin/postgresql-13-setup initdb

Enable and start the service

systemctl start postgresql-13.service

systemctl enable postgresql-13.service

Optionally login and confirm

How to have multiple PostgreSQL clusters in PostgreSQL 13:

  • As we have edited the file postgresql-13.service to change the cluster location, here to have a new cluster, you need to create a new unit file.

cp /lib/systemd/system/postgresql-13.service /etc/systemd/system/postgresql-13-secondary.service

  • Edit this file, and change PGDATA.

Environment=PGDATA=/u01/pgsql/14

  • Create a required directory at OS level and give necessary permissions

mkdir -p /u01/pgsql/14
chown -R postgres:postgres /u01/pgsql/14
chmod 750 /u01/pgsql/14

  • Initialize the cluster:

/usr/pgsql-13/bin/postgresql-13-setup initdb postgresql-13-secondary

  • Edit postgresql.conf to change the port, address, tcpip settings, etc.

  • Start the postmaster with ‘systemctl start postgresql-13-secondary.service’

Check if both the services are running ?

Complete list of processes

What happens when you reboot your server where your database is hosted?

During a graceful reboot at OS level, PostgreSQL received a fast shutdown request.

The same can be found in log during graceful reboot.

cat /var/log/messages

Nov 23 16:39:54 postgreshelp systemd: Stopping RPC bind service…
Nov 23 16:39:54 postgreshelp systemd: Stopping PostgreSQL 13 database server…

PostgreSQL log

2020-11-23 16:39:55.445 IST [4851] LOG: received fast shutdown request
2020-11-23 16:39:55.458 IST [4851] LOG: aborting any active transactions
2020-11-23 16:39:55.465 IST [4851] LOG: background worker “logical replication launcher” (PID 4860) exited with exit code 1
2020-11-23 16:39:55.465 IST [4855] LOG: shutting down
2020-11-23 16:39:55.499 IST [4851] LOG: database system is shut down

How to start the PostgreSQL service as a postgres user

Add postgres user to sudoer file

Now, postgres user can run systemctl command to run postgres service.

 

 

Words from postgreshelp

Thank you for giving your valuable time to read the above information. I hope the content served your purpose in reaching out the blog.
Suggestions for improvement of the blog are highly appreciable. Please contact us for any information/suggestion/feedback.

If you want to be updated with all our articles

please follow us on Facebook Twitter
Please subscribe to our newsletter.

 

Leave a Reply