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 initdbcommand immediately after installation - Switch from default
ident/peerauthentication toscram-sha-256for application database connections - Restrict remote access to specific trusted IPs, same as any database engine
Related Articles
- How to Configure firewalld on AlmaLinux/Rocky Linux
- How to Back Up and Restore PostgreSQL Databases
- Database Security Checklist: Protecting MySQL, PostgreSQL & MongoDB
