How to Install PostgreSQL on AlmaLinux/Rocky Linux

The default AlmaLinux/Rocky Linux repositories include an older PostgreSQL version. This guide covers installing a current version directly from the official PostgreSQL repository.

Prerequisites

  • AlmaLinux 9 or Rocky Linux 9 VPS
  • Root or sudo access

Step 1 — Add the Official PostgreSQL Repository

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

Step 2 — Disable the Built-In PostgreSQL Module

sudo dnf -qy module disable postgresql

This prevents conflicts between the default repository's older version and the official PostgreSQL repository's current version.

Step 3 — Install PostgreSQL

sudo dnf install postgresql16-server postgresql16 -y

Adjust the version number to the current PostgreSQL release.

Step 4 — Initialize the Database

sudo /usr/pgsql-16/bin/postgresql-16-setup initdb

Step 5 — Enable and Start PostgreSQL

sudo systemctl enable --now postgresql-16

Step 6 — Set a Password for the postgres User

sudo -i -u postgres
psql
ALTER USER postgres WITH PASSWORD 'CHANGE_ME_STRONG_PASSWORD';

Step 7 — Create a Database and User

CREATE DATABASE myapp;
CREATE USER appuser WITH ENCRYPTED PASSWORD 'CHANGE_ME_STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON DATABASE myapp TO appuser;
\q
exit

Step 8 — Configure Authentication

sudo nano /var/lib/pgsql/16/data/pg_hba.conf

By default, PostgreSQL on RHEL-family systems uses ident authentication, which requires matching OS and database usernames — change to password-based authentication for application use:

host    all    all    127.0.0.1/32    scram-sha-256
sudo systemctl restart postgresql-16

Step 9 — Allow Remote Connections (If Needed)

sudo nano /var/lib/pgsql/16/data/postgresql.conf
listen_addresses = '*'
sudo firewall-cmd --permanent --add-service=postgresql
sudo firewall-cmd --reload
sudo systemctl restart postgresql-16

SELinux Considerations

If PostgreSQL needs to make outbound network connections (unusual, but relevant for some extensions):

sudo setsebool -P postgresql_can_network_connect 1

Common Errors

"Peer authentication failed" — the default ident/peer authentication method requires connecting as a matching OS user; switch to scram-sha-256 in pg_hba.conf as shown above for application-style authentication.

PostgreSQL won't start after installation — confirm initdb was run before attempting to start the service; unlike Ubuntu/Debian's automatic initialization, RHEL-family PostgreSQL requires this explicit step.

Best Practices

  • Always run the version-specific postgresql-XX-setup initdb command immediately after installation
  • Switch from default ident/peer authentication to scram-sha-256 for application database connections
  • Restrict remote access to specific trusted IPs, same as any database engine

Continue Reading

Browse more articles in AlmaLinux & Rocky Linux.

  • postgresql almalinux, postgresql rocky linux, rhel postgresql, pgdg repository
  • 0 أعضاء وجدوا هذه المقالة مفيدة
هل كانت المقالة مفيدة ؟

مقالات مشابهة

AlmaLinux vs Rocky Linux vs CentOS: What Happened to CentOS?

If you're choosing a RHEL-compatible Linux distribution for your VPS, understanding the CentOS...

How to Get Started with AlmaLinux/Rocky Linux on a VPS

This guide covers the essential first steps after deploying a new AlmaLinux or Rocky Linux VPS...

How to Use dnf: The Package Manager for AlmaLinux & Rocky Linux

dnf (Dandified YUM) is the package manager for AlmaLinux, Rocky Linux, and other RHEL-family...

How to Configure firewalld on AlmaLinux/Rocky Linux

firewalld is the default firewall management tool on AlmaLinux and Rocky Linux, using a...

How to Install Nginx on AlmaLinux/Rocky Linux

This guide covers installing and configuring Nginx on AlmaLinux or Rocky Linux, including the...