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 "18.104.22.168", 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
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 = '*'
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 "22.214.171.124", port 5432 failed: FATAL: no pg_hba.conf entry for host "126.96.36.199", 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 188.8.131.52 -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 "184.108.40.206", 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.
|PostgreSQL default port||5432|
|Config files location (||/var/lib/pgsql/15/data/pg_hba.conf |
|Default data directory||/var/lib/pgsql/15/data|
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
If you are starting your DevOps engineer journey, look at my comprehensive guide to becoming a DevOps engineer.