How to Install PostgreSQL 16 on Red Hat Enterprise Linux (RHEL) 9.8 – Step-by-Step Guide
How to Install PostgreSQL 16 on Red Hat Enterprise Linux (RHEL) 9.8 – Step-by-Step Guide
Introduction
PostgreSQL is one of the most popular open-source relational database management systems (RDBMS). It is widely used in enterprise applications because of its reliability, scalability, security, and advanced database features.
In this blog, we will learn how to install PostgreSQL 16 on Red Hat Enterprise Linux (RHEL) 9.8 using the official PostgreSQL Global Development Group (PGDG) repository.
We will cover:
- Installing the PostgreSQL 16 repository.
- Disabling the default PostgreSQL module.
- Installing PostgreSQL 16 server packages.
- Initializing the database cluster.
- Configuring PostgreSQL services.
- Setting environment variables.
- Configuring the PostgreSQL command prompt.
- Verifying the installation.
1. Prerequisites
Before starting the installation, ensure that you have:
| Requirement | Details |
|---|---|
| Operating System | Red Hat Enterprise Linux 9.8 |
| PostgreSQL Version | PostgreSQL 16 |
| User | Root or sudo-enabled user |
| Internet | Required |
| Package Manager | DNF |
| Architecture | x86_64 |
First, verify your operating system version.
cat /etc/redhat-release
Expected output:
Red Hat Enterprise Linux release 9.8 (Plow)
2. Update the Linux Operating System
Before installing PostgreSQL, update the system packages.
sudo dnf update -y
This command updates the installed packages and ensures that the system has the latest available updates.
3. Configure the PostgreSQL Official Repository
PostgreSQL provides an official PGDG repository that allows us to install specific PostgreSQL versions.
Execute the following command:
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
This command downloads and installs the PostgreSQL repository configuration package.
You can verify the repository using:
dnf repolist | grep pgdg
4. Disable the Default PostgreSQL Module
RHEL provides PostgreSQL packages through its own module streams.
Since we are installing PostgreSQL 16 using the official PGDG repository, disable the default PostgreSQL module to avoid package conflicts.
Execute:
sudo dnf -qy module disable postgresql
Clean the DNF cache:
sudo dnf clean all
5. Install PostgreSQL 16 Server
Now install the PostgreSQL 16 server package.
sudo dnf install -y postgresql16-server
If you also require additional PostgreSQL extensions and client utilities, install the following packages:
sudo dnf install -y postgresql16 postgresql16-contrib
The installation creates the PostgreSQL binaries under:
/usr/pgsql-16/bin/
Verify the installed PostgreSQL version:
/usr/pgsql-16/bin/psql --version
Expected output:
psql (PostgreSQL) 16.x
6. Initialize the PostgreSQL Database Cluster
After installing PostgreSQL, we need to initialize the database cluster.
A database cluster is the collection of databases managed by a PostgreSQL server instance.
Execute:
sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
This command initializes the PostgreSQL data directory.
The default data directory is:
/var/lib/pgsql/16/data
It creates important configuration files, including:
- postgresql.conf
- pg_hba.conf
- pg_ident.conf
Important: Initialization is generally a one-time operation for a new database cluster. Do not execute it on an existing database directory because it can fail if the directory is already initialized.
7. Enable and Start PostgreSQL 16 Service
After initializing the database cluster, configure PostgreSQL to start automatically whenever the Linux server reboots.
Enable the service:
sudo systemctl enable postgresql-16
Start PostgreSQL:
sudo systemctl start postgresql-16
Alternatively, both operations can be performed together:
sudo systemctl enable --now postgresql-16
Check the service status:
sudo systemctl status postgresql-16
Expected status:
Active: active (running)
At this point, PostgreSQL 16 is installed and running.
8. Configure PostgreSQL Environment Variables
Now switch to the PostgreSQL operating system user.
sudo su - postgres
Check the current directory:
pwd
Expected output:
/var/lib/pgsql
Edit the .bash_profile file:
vi ~/.bash_profile
Add the following configuration:
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# PostgreSQL 16 Environment Variables
export PATH=/usr/pgsql-16/bin:$PATH
export PGDATA=/var/lib/pgsql/16/data
# User-specific environment settings
PATH=$PATH:$HOME/bin
export PATH
export PS1="[\u@\h \W]\$ "
Save and exit the file.
Reload the environment:
source ~/.bash_profile
Verify the environment variables:
echo $PATH
echo $PGDATA
Expected PGDATA output:
/var/lib/pgsql/16/data
Check the PostgreSQL binary location:
which psql
Expected output:
/usr/pgsql-16/bin/psql
Note: Make sure you use PostgreSQL 16 in the PATH. Do not use /usr/pgsql-15/bin because that points to PostgreSQL 15.
Also, keep PGDATA consistent with the actual initialized data directory.
9. Configure the PostgreSQL Command Prompt Using .psqlrc
PostgreSQL provides a configuration file called .psqlrc.
This file allows us to customize the PostgreSQL interactive terminal (psql).
For example, we can configure the command prompt to display:
- PostgreSQL username.
- Current database name.
- Current connection status.
First, ensure that you are logged in as the PostgreSQL operating system user.
su - postgres
Navigate to the PostgreSQL home directory:
cd /var/lib/pgsql
Create or edit the .psqlrc file:
vi ~/.psqlrc
Add the following configuration:
\set PROMPT1 '%n@%/%# '
Save and exit.
Explanation:
| Parameter | Description |
|---|---|
| %n | Current PostgreSQL username |
| %/ | Current database name |
| %# | Displays # for a superuser and > for a normal user |
For example, after connecting to PostgreSQL:
postgres@postgres=#
This makes the PostgreSQL command-line environment easier to identify and work with.
10. Connect to PostgreSQL 16
Now connect to PostgreSQL using the psql utility.
psql
Alternatively:
psql -U postgres
Expected output:
psql (16.x)
Type "help" for help.
postgres@postgres=#
You have successfully connected to PostgreSQL 16.
11. Verify PostgreSQL Installation
Execute the following SQL commands to verify your installation.
Check PostgreSQL version:
SELECT version();
Check current database:
SELECT current_database();
Check current user:
SELECT current_user;
Check data directory:
SHOW data_directory;
Check server port:
SHOW port;
By default, PostgreSQL listens on port:
5432
Check database list:
\l
Check database connection information:
\conninfo
12. Useful PostgreSQL Service Commands
Here are some important Linux commands for PostgreSQL database administrators.
| Task | Command |
|---|---|
| Start PostgreSQL | sudo systemctl start postgresql-16 |
| Stop PostgreSQL | sudo systemctl stop postgresql-16 |
| Restart PostgreSQL | sudo systemctl restart postgresql-16 |
| Check status | sudo systemctl status postgresql-16 |
| Enable at boot | sudo systemctl enable postgresql-16 |
| Disable at boot | sudo systemctl disable postgresql-16 |
| Check version | psql --version |
Conclusion
In this blog, we successfully installed PostgreSQL 16 on Red Hat Enterprise Linux 9.8 using the official PGDG repository.
We also initialized the database cluster, configured PostgreSQL services, set environment variables using .bash_profile, customized the PostgreSQL terminal prompt using .psqlrc, and verified the installation.
These steps provide a foundation for learning PostgreSQL database administration.

