PostgreSQL 17 is a powerful open-source relational database used for web apps, analytics platforms, and backend services. In this guide, you will install and configure PostgreSQL 17 on Rocky Linux 9 or AlmaLinux 9, then secure it for production use.
By the end, you will know how to:
- Install PostgreSQL 17 from the official PostgreSQL RPM repository
- Start and enable the database service with
systemd - Create users and databases
- Enable secure remote access
- Open firewall ports safely
- Run basic backup and restore commands
- Troubleshoot common startup and connection issues
Prerequisites
Before you begin, make sure you have:
- A server running Rocky Linux 9 or AlmaLinux 9
- A user with
sudoprivileges - Internet access to download PostgreSQL packages
- Basic Linux terminal familiarity
1. Update the Server
Update installed packages first:
| |
2. Install PostgreSQL 17
Add the PostgreSQL repository
| |
Disable the built-in PostgreSQL module
Rocky/Alma include a default PostgreSQL module. Disable it to avoid package conflicts:
| |
Install PostgreSQL server and contrib packages
| |
Install postgresql17-contrib for useful extensions and utilities:
| |
3. Initialize and Start PostgreSQL Service
Initialize the database cluster:
| |
Start PostgreSQL:
| |
Enable it at boot:
| |
Verify service status:
| |
If you see Active: active (running), PostgreSQL is running correctly.
Check the installed version:
| |
Output:
| |
4. Basic PostgreSQL Setup (User and Database)
Switch to the postgres system user:
| |
Open the PostgreSQL prompt:
| |
Create an application user and database (replace placeholders):
| |
Exit:
| |
Then exit the shell:
| |
5. Configure Local and Remote Access
PostgreSQL connection behavior is controlled by:
postgresql.conf(server settings)pg_hba.conf(client authentication rules)
On Rocky/Alma with PGDG packages, these files are usually under:
| |
Allow PostgreSQL to listen on server IP
Edit postgresql.conf:
| |
Update or uncomment:
| |
For stricter security, use a specific IP instead of *.
Add client authentication rule
Edit pg_hba.conf:
| |
Add a rule for your trusted network:
| |
Use your real subnet. Avoid broad ranges like 0.0.0.0/0 in production.
Restart PostgreSQL after changes:
| |
6. Open Firewall Port 5432
If firewalld is enabled, allow PostgreSQL traffic:
| |
Confirm:
| |
7. Secure PostgreSQL for Production
Apply these hardening steps:
- Use strong passwords for all login roles
- Restrict
pg_hba.confto known IP ranges only - Use
scram-sha-256authentication (recommended) - Keep OS and PostgreSQL packages updated
- Enable TLS for encrypted client/server traffic when needed
- Rotate credentials periodically
Optional: in postgresql.conf, confirm password encryption:
| |
Reload config:
| |
8. Backup and Restore Essentials
Backup one database
| |
Backup all databases
| |
Restore a database dump
| |
9. Useful Verification Commands
Check listening port:
| |
Check PostgreSQL logs (via systemd journal):
| |
List databases:
| |
List roles:
| |
10. Common Troubleshooting
Service fails to start
- Check logs:
sudo journalctl -u postgresql-17 -xe --no-pager - Verify data directory permissions
- Confirm configuration syntax in
postgresql.confandpg_hba.conf
Cannot connect remotely
- Ensure
listen_addressesis set correctly - Ensure
pg_hba.confhas a matchinghostrule - Confirm firewall allows port 5432
- Confirm cloud/network security rules allow inbound traffic
Authentication failed
- Verify username/password
- Confirm matching
pg_hba.confrule and auth method (scram-sha-256) - Reset password if needed:
| |
Final Thoughts
You now have a complete PostgreSQL 17 setup on Rocky Linux 9 or AlmaLinux 9, including installation, service management, remote access, hardening, and backup basics.
If this server is for production, the next best step is to configure TLS, monitoring, and automated backups.