Introduction
PostgreSQL is a powerful, open-source relational database management system widely used for enterprise applications, web platforms, analytics, and mission-critical workloads. Its reliability, extensibility, and support for advanced SQL features make it a popular choice for database administrators and application developers.
In this article, we will walk through the installation and initial configuration of PostgreSQL on Red Hat Enterprise Linux (RHEL) and Oracle Linux using the PostgreSQL Global Development Group (PGDG) repository.
The guide covers repository configuration, PostgreSQL server installation, database initialization, service management, and PostgreSQL user environment variables.
Prerequisites
Before starting the installation, ensure that:
- You have root or sudo privileges on the Linux server.
- The server has network access to the PostgreSQL repository, or the installation packages are available locally.
- The operating system version is compatible with the selected PostgreSQL repository.
- Required firewall rules and storage capacity have been reviewed.
Note: The commands in this guide use PostgreSQL 16 as the example version. PostgreSQL 15 can also be installed by replacing the version-specific commands and package names where applicable. The repository URL must match your operating system version.
Step 1: Install the PostgreSQL Repository
The PGDG repository provides PostgreSQL packages for supported Linux distributions.
For RHEL 7 or Oracle Linux 7 on x86_64, use: Download
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
For newer operating system releases, download and install the repository RPM that matches your EL major version. For example, use the appropriate EL-8 or EL-9 repository path for RHEL 8/9 or compatible Oracle Linux releases.
Verify the repository configuration:
yum repolist
On systems using DNF, you can run:
dnf repolist
Confirm that the relevant PostgreSQL repository is listed and enabled.
Step 2: Install PostgreSQL Server
Install the PostgreSQL 16 server package:
dnf install -y postgresql16-server
On systems using YUM, the equivalent command is:
yum install -y postgresql16-server
For PostgreSQL 15, use:
yum install -y postgresql15-server
After installation, verify the installed package:
rpm -qa | grep postgresql
You should see the PostgreSQL server package corresponding to the selected version.
Step 3: Initialize the PostgreSQL Database Cluster
Before starting PostgreSQL for the first time, initialize the database cluster.
For PostgreSQL 16:
/usr/pgsql-16/bin/postgresql-16-setup initdb
For PostgreSQL 15:
/usr/pgsql-15/bin/postgresql-15-setup initdb
This process creates the initial database cluster, including the system catalogs and default configuration files.
The default data directory is typically:
- PostgreSQL 15:
/var/lib/pgsql/15/data - PostgreSQL 16:
/var/lib/pgsql/16/data
Important: Initialize the cluster only when setting up a new instance. Do not run the initialization command against an existing database cluster, as it can fail or create serious configuration problems if the data directory is handled incorrectly.
Step 4: Enable the PostgreSQL Service
Configure PostgreSQL to start automatically when the server boots.
For PostgreSQL 16:
systemctl enable postgresql-16
For PostgreSQL 15:
systemctl enable postgresql-15
To enable and start the service in one command:
systemctl enable --now postgresql-16
Use the service name matching your installed PostgreSQL version.
Step 5: Start and Verify the PostgreSQL Service
Start the PostgreSQL 16 service:
systemctl start postgresql-16
Check the service status:
systemctl status postgresql-16.service
If the installation and initialization are successful, the service should report an active running state.
You can also verify the PostgreSQL processes:
ps -ef | grep '[p]ostgres'
Check whether PostgreSQL is listening on its configured port:
ss -lntp | grep 5432
The default PostgreSQL TCP port is 5432, unless it has been changed in the configuration.
Step 6: Switch to the PostgreSQL Operating System User
PostgreSQL packages typically create a Linux operating system account named postgres.
Switch to this account:
su - postgres
The hyphen (-) starts a login shell and loads the user’s login environment.
Check the PostgreSQL processes:
ps -ef | grep '[p]ostgres'
You can also connect to the local PostgreSQL instance:
psql -p 5432 -d postgres
Inside the PostgreSQL prompt, verify the server version:
SELECT version();
Exit the SQL prompt using:
\q
Step 7: Configure PostgreSQL Environment Variables
Environment variables simplify PostgreSQL administration by allowing you to run client utilities without specifying their full paths.
First, inspect the PostgreSQL user’s home directory:
ls -al
Edit the .bash_profile file:
vi ~/.bash_profile
Add the following configuration for PostgreSQL 16:
PATH=/usr/pgsql-16/bin:$PATH:$HOME/.local/bin:$HOME/bin
PGDATA=/var/lib/pgsql/16/data
PGPORT=5432
PGUSER=postgres
export PATH PGDATA PGPORT PGUSER
For PostgreSQL 15, update the version-specific paths:
PATH=/usr/pgsql-15/bin:$PATH:$HOME/.local/bin:$HOME/bin
PGDATA=/var/lib/pgsql/15/data
PGPORT=5432
PGUSER=postgres
export PATH PGDATA PGPORT PGUSER
Save the file and exit the editor.
Apply the environment variables without logging out:
source ~/.bash_profile
Alternatively, exit the PostgreSQL user session and log in again.
Important: PGDATA should match the actual database cluster directory. If PostgreSQL was initialized in a custom location, update this variable accordingly.
Step 8: Verify the Environment Variables
Confirm that the environment variables have been applied:
echo $PATH
echo $PGDATA
echo $PGPORT
echo $PGUSER
Expected values for PostgreSQL 16 include:
PGDATA=/var/lib/pgsql/16/data
PGPORT=5432
PGUSER=postgres
Check the location of the PostgreSQL client:
which psql
Expected output:
/usr/pgsql-16/bin/psql
Verify the installed client version:
psql --version
You can also type pg and press the Tab key twice to display matching commands available in the configured PATH.
Step 9: Perform a Basic Database Connectivity Test
Once the service is running and the environment variables are configured, test the connection:

psql -d postgres
If you need to specify the host and port explicitly:
psql -h localhost -p 5432 -U postgres -d postgres
Execute the following SQL statement:
SELECT current_database(),
current_user,
inet_server_port(),
version();
This verifies the connected database, database user, server port, and PostgreSQL version.
Troubleshooting Common Installation Issues
1. PostgreSQL service fails to start
Check the service status:
systemctl status postgresql-16.service
Review the service logs:
journalctl -u postgresql-16.service -xe
Verify that the data directory exists, has the correct ownership, and contains an initialized database cluster.
2. The psql command is not found
Check the PostgreSQL binary directory:
ls -l /usr/pgsql-16/bin/psql
If the binary exists, verify that the correct directory is included in PATH:
echo $PATH
Then reload the environment:
source ~/.bash_profile
3. PostgreSQL is not listening on port 5432
Check the configured port:
grep -E '^[[:space:]]*port[[:space:]]*=' "$PGDATA/postgresql.conf"
Review the service status and logs to identify startup or configuration errors.
4. Repository installation fails
Verify that the repository RPM matches your operating system version and architecture. Check network connectivity and confirm that the required repositories are accessible.
For offline installations, download the correct RPM packages and dependencies on a connected system, transfer them to the database server, and install them according to your organization’s package-management procedures.
Conclusion
Installing PostgreSQL on Red Hat Enterprise Linux or Oracle Linux involves configuring the appropriate PGDG repository, installing the server package, initializing the database cluster, enabling the service, and configuring the PostgreSQL operating system user’s environment.
Following these steps provides a foundation for further database administration, including user and role management, network access configuration, backup and recovery, monitoring, replication, and high availability.
For production deployments, also review authentication rules in pg_hba.conf, network exposure, firewall configuration, database storage layout, backup procedures, and security hardening before allowing application access.





Comments 1