How to Install & Configure PostgreSQL on Redhat Linux

Install & Configure PostgreSQL on Redhat

If you want to install PostgreSQL on Redhat Linux, this guide is for you. It covers installation, configuration, and enabling remote connection.

Install PostgreSQL on Redhat Linux

Follow the steps given below to install the latest version of PostgreSQL on RedHat Linux

Step 1: Add the PostgreSQL Yum Repo

Execute the following command to add the PostgreSQL yum repo.

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

Step 2: Install the latest version of the PostgreSQL Server

The latest version at the time of writing this guide is PostgreSQL 15. You can check the latest version available for Rehat on this page.

sudo yum install -y postgresql15-server

Step 3: Initialize the Database and Enable Automatic Start

Execute the following commands to initialize the PostgreSQL database and add services to the server startup.

sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15

Enable PostgreSQL Remote Connection

By default the remote PostgreSQL connection is disabled. However, you can connect to the database locally. If you try to connect from a remote machine you will get an error like the following.

psql: error: connection to server at "34.221.35.108", port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?

First login to the database using the following command.

sudo -u postgres psql

Set the password for the Postgres user so that we can use it to log in remotely. Replace myPassword with the required password.

ALTER USER postgres PASSWORD 'myPassword';

To enable PostgreSQL remote connection on Redhat, you need to open the following file.

sudo vi /var/lib/pgsql/15/data/postgresql.conf

Under CONNECTIONS AND AUTHENTICATION section you will find the following commented parameter.

#listen_addresses = 'localhost' 

Replace it with the following and save the file. With this configuration, we are enabling PostgreSQL server connections to accept connections from all IP addresses.

listen_addresses = '*' 
PostgreSQL listen_addresses parameter

To apply the change, restart the PostgreSQL service using the following command. If you are using any version other than 15, replace 15 with the relevant version number in the command.

sudo systemctl restart postgresql-15

We have allowed connections on all interfaces. However, If you try to connect to the PostgreSQL RedHat server from a remote machine, you will get the following error because you need to enable client connections to all the Databases and users.

psql: error: connection to server at "34.221.35.108", port 5432 failed: FATAL:  no pg_hba.conf entry for host "49.205.132.96", user "postgres", database "postgres", no encryption

To allow client connections to all databases, you need to edit the pg_hba.conf file.

Open pg_hba.conf file.

sudo vi /var/lib/pgsql/15/data/pg_hba.conf 

Add the following line to the end of the file.

host    all          all            0.0.0.0/0  md5

Restart the PostgreSQL server to apply changes.

sudo systemctl restart postgresql-15

Now, validate the remote connection by connecting to the PostgreSQL server using the following command. Replace IP with your server IP.

psql -h 34.221.35.108 -U postgres

When prompted for the password, use the password you set using the ALTER command.

If you don’t have a password set for the Postgres user, you will get the following error.

Password for user postgres:
psql: error: connection to server at "34.221.35.108", port 5432 failed: FATAL:  password authentication failed for user "postgres"

Important PostgreSQL Server Configurations on Redhat

The following table contains important PostgreSQL configurations on the Redhat server. Replace 15 with the version of PostgreSQL you are using.

ConfigDetails
PostgreSQL default port5432
Default userpostgres
Config files location (postgresql.conf & pg_hba.conf )/var/lib/pgsql/15/data/pg_hba.conf
/var/lib/pgsql/15/data/postgresql.conf
Default databasepostgres
Default data directory/var/lib/pgsql/15/data

Conclusion

As a DevOps engineer, it is essential to know the basic configurations involved in a Database. in this guide we looked at the steps to install PostgreSQL on a Redhat server.

For the Ubuntu server, Checkout the PostgreSQL installation on Ubuntu.

For the Amazon Linux server, Checkout the PostgreSQL installation on Amazon Linux

Also, if you are using Kubernetes, check out the guide on setting up PostgreSQL statefulset on Kubernetes

If you are starting your DevOps engineer journey, look at my comprehensive guide to becoming a DevOps engineer.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like