How to Install and Secure PostgreSQL on RHEL 9
PostgreSQL is a strong choice for production workloads on Red Hat Enterprise Linux 9, but a default installation should not be treated as secure. A sound deployment combines trusted packages, a controlled network boundary, least-privilege database roles, encrypted connections, reliable backups, and useful audit information.
The commands below use the PostgreSQL packages supplied through RHEL 9 repositories. They suit a lab server, an internal application host, or a small production system in Sydney, Melbourne, Brisbane, or another Australian location. Test package versions and configuration changes before applying them to a live database.
Australian operators should also consider the Privacy Act 1988 and the handling of personal information across hosting regions. Confirm where backups are stored, restrict administrative access, and use Australian Eastern Standard Time or Australian Eastern Daylight Time consistently when reviewing scheduled jobs and incident records.
Select And Install PostgreSQL Packages
Refresh the package metadata and inspect the available PostgreSQL module stream:
sudo dnf update -y
sudo dnf module list postgresql
Install the server and useful client utilities:
sudo dnf install -y postgresql-server postgresql-contrib
RHEL 9 may provide a stable PostgreSQL stream through AppStream. If an application requires a newer major release, use the PostgreSQL community repository instead, but document the repository choice and avoid mixing packages from unrelated sources.
Initialise The Database Cluster
Create the initial database cluster, enable the service at boot, and start it immediately:
sudo postgresql-setup --initdb
sudo systemctl enable --now postgresql
sudo systemctl status postgresql
Check that PostgreSQL is accepting local connections:
sudo -u postgres psql -c "SELECT version();"
The main configuration files are commonly located in /var/lib/pgsql/data/. Use the active server to confirm the exact location:
sudo -u postgres psql -c "SHOW config_file;"
sudo -u postgres psql -c "SHOW hba_file;"
Create Least Privilege Roles
Do not use the operating-system postgres account for application traffic. Create a separate database and login role with a long, randomly generated password:
sudo -u postgres psql
CREATE ROLE app_user LOGIN PASSWORD 'replace-with-a-long-random-secret';
CREATE DATABASE app_db OWNER app_user;
\c app_db
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
GRANT CONNECT ON DATABASE app_db TO app_user;
\q
Store credentials in a protected secret manager or root-readable environment file rather than in shell history. A developer or administrator may also consult this technical reference when comparing PostgreSQL deployment practices, but production access should remain specific to named staff and service accounts.
Limit Network And Authentication Access
By default, PostgreSQL is safest when it listens only on the local host. In postgresql.conf, retain or set:
listen_addresses = 'localhost'
If a separate application server requires access, specify the database host’s private address rather than using '*'. Add a narrow rule to pg_hba.conf, preferably with SCRAM authentication:
host app_db app_user 10.20.30.15/32 scram-sha-256
Reload the service after editing:
sudo systemctl reload postgresql
| Control | Safer setting | Reason |
|---|---|---|
| Listener | Private address or localhost | Reduces exposure |
| Authentication | scram-sha-256 |
Protects stored password verifiers |
| Firewall | Only trusted source addresses | Blocks unsolicited connections |
| Database role | Application-specific login | Limits permissions |
| Encryption | TLS for remote traffic | Protects data in transit |
Configure Firewalld And SELinux
If remote PostgreSQL access is genuinely required, open TCP port 5432 only to the application subnet or a specific host:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.20.30.15/32" port port="5432" protocol="tcp" accept'
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
Do not expose port 5432 directly to the public internet. Cloud security groups, VLANs, VPNs, and firewalld should reinforce each other rather than serve as a single control.
Keep SELinux enforcing and check denials if PostgreSQL uses a non-standard data directory:
getenforce
sudo ausearch -m AVC -ts recent
Changing SELinux to permissive mode can hide a configuration error and should not be used as a permanent fix.
Protect Backups And Log Management
A PostgreSQL backup is useful only when restoration has been tested. Create logical backups with pg_dump, or use a physical backup tool for larger databases and point-in-time recovery. Keep at least one copy separate from the database host, encrypt it, and restrict its permissions.
Schedule backup jobs according to business requirements rather than a convenient default. A retail system serving Melbourne customers may need a different recovery point objective from an internal reporting database. Record the backup location and retention period, particularly when Australian personal information is involved.
PostgreSQL logs should be rotated and retained long enough to investigate failed logins, schema changes, and service faults. This logrotate guide provides a practical pattern for managing growing Linux log files.
Verify The Deployment Regularly
Use a short operational checklist after installation and after major configuration changes.
Operational Checks For A Safer Server
- Confirm that
postgresql, firewalld, and SELinux are running in their expected modes. - Test a permitted application connection and verify that an unauthorised source is rejected.
- Check that no application role has superuser, replication, or database-creation privileges without a documented reason.
- Perform a restore into a separate test database and record the elapsed recovery time.
- Review authentication failures, disk usage, package updates, and PostgreSQL logs on a defined schedule.
Apply security updates through the normal RHEL change process, and restart PostgreSQL only when the update requires it. Monitor free space in the data and WAL locations, because a full filesystem can stop writes even when the database process remains active.
For a server used across Australian offices, schedule maintenance in an agreed local window and account for daylight-saving changes in New South Wales, Victoria, Tasmania, and the Australian Capital Territory. Finally, keep operational notes and copyright-aware documentation practices aligned with the site’s copyright policy.
A secure RHEL 9 deployment is established by verifying each layer: install supported packages, initialise the cluster, create limited roles, restrict network paths, preserve SELinux enforcement, protect backups, and test recovery. The practical takeaway is to treat every PostgreSQL connection, privilege, firewall rule, and backup as an item that must be explicitly justified and periodically checked.