Objective
This guide explains how to install PostgreSQL 18 on RHEL / AlmaLinux using the official PostgreSQL repository (PGDG).
Expected Result
PostgreSQL 18 is installed, running as a systemd service, and storing its database files in the selected data directory (/pgdata or the default location).
Implementation
-
Update the system (optional)
dnf update -y
-
Add the PostgreSQL 18 Repository
Install the official Postgresql repository package:
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
- Adds the official PostgreSQL repository to the system
- Allows
dnfto download and install PostgreSQL packages from the PostgreSQL Global Development Group (PGDG)
-
Verify the repository
dnf repolist
Or check the repository configuration:
ls -l /etc/yum.repos.d/pgdg*cat /etc/yum.repos.d/pgdg-redhat-all.repo | more
-
Check whether the PostgreSQL 18 repository is available
dnf repolist all | grep pgdg18
- If listed as
enabled, the repository is ready to use. - If listed as
disabled, it is represent but not currently enabled.
- If listed as
-
Disable the Built-in PostgreSQL Module
dnf -qy module disable postgresql
-
Install PostgreSQL 18 Server and Client
dnf install -y postgresql18-server postgresql18
Verify installation:
pgsql -V
-
Prepare the PostgreSQL Data Directory
-
Assume
/pgdatais the desired data directory.Note: Click here to learn how to create and configure a production-ready PostgreSQL data directory using Stratis Storage.
-
Set ownership and permissions:
chown -R postgres:postgres /pgdatachmod 700 /pgdata
postgres:postgres: PostgreSQL service user and group.700: Only the owner has access.
-
-
Initialize the Database Cluster
Initialize the database cluster. This creates the initial database files and system catalogs required by PostgreSQL.
Option A: Use the Default Data Directory
/usr/pgsql-18/bin/postgresql-18-setup initdb
- Initializes PostgreSQL in the default directory:
/var/lib/pgsql/18/data - Suitable for a simple installation where you don’t need a separate data disk.
Option B: Use a Custom Data Directory (Recommended)
If you have prepared a separate storage volume mounted at /pgdata, initialize PostgreSQL there
su - postgres/usr/pgsql-18/bin/initdb -D /pgdataexit
-D /pgdataspecifies the custom database data directory.- PostgreSQL will store it database files under
/pgdatainstead of the default directory.
Recommendation: Use a custom directory when PostgreSQL data is stored on a dedicated disk or Stratis filesystem. It makes storage management and capacity planning easier.
- Initializes PostgreSQL in the default directory:
-
Configure PostgreSQL to use
/pgdataBecause PostgreSQL normally uses
/var/lib/pgsql/18/data, configure the service to use the custom directory:Edit the systemd configuration
vi /usr/lib/systemd/system/postgresql-18.serviceChange to:
[Service]Environment=PGDATA=/pgdata
Reload the systemd configuration:
systemctl daemon-reload
Verify the configured environment:
systemctl show postgresql-18 -p Environment
-
Start and Enable PostgreSQL
Start PostgreSQL and enable it to start automatically during system boot.
systemctl enable --now postgresql-18
Verify the service:
systemctl status postgresql-18
-
Verify Database Connection
-
witch to the PostgreSQL service account:
su - postgres
-
Connect to PostgreSQL
psql -
Check the PostgreSQL version
SELECT version(); -
Verify the active data directory
SHOW data_directory; -
Exit PostgreSQL
\q
-
Important production notes:
- Do not initialize both the default and custom data directories unnecessarily.
- If using
/pgdata, ensure it is mounted before PostgreSQL starts. - Configure SELinux permissions for the custom directory.
- Ensure the
postgresuser owns the data directory. - Configure backups before putting production data into the database.