
PostgreSQL 16 is the latest release of the powerful open-source relational database system, offering enhanced performance, security, and new features. This guide walks you through installing PostgreSQL 16 (for previous version click here) on RHEL 9 with step-by-step instructions, commands, and best practices.
Step 1: Update the System
Before installing PostgreSQL, update your system packages to ensure you get the latest security updates and dependencies:
yum update -y
Step 2: Download and Install the PostgreSQL Repository
PostgreSQL is not included by default in RHEL’s package repositories, so you need to add the official PostgreSQL repository:
rpm -Uvh https://download.postgresql.org/pub/repos/yum/16/redhat/rhel-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Step 3: Disable the Default PostgreSQL Module
RHEL includes a built-in version of PostgreSQL, which we need to disable before installing the new version:
dnf -qy module disable postgresql
Step 4: installing PostgreSQL 16
Now, install PostgreSQL 16 along with additional utilities:
yum install -y postgresql16 postgresql16-server postgresql16-contrib
Step 5: Initialize the PostgreSQL Database Cluster
After installation, you need to initialize the database cluster:
/usr/pgsql-16/bin/postgresql-16-setup initdb
Step 6: Start and Enable PostgreSQL
Enable PostgreSQL to start automatically on system boot and start the service:
systemctl enable --now postgresql-16
systemctl status postgresql-16
Ensure that PostgreSQL is running correctly by checking its status.
Step 7: Switch to PostgreSQL User
By default, PostgreSQL runs under the postgres
user. Switch to it using:
su - postgres
Step 8: Create a New User and Grant Superuser Privileges
Create a new user named dataverse
and grant it superuser privileges (⚠️ Warning: Superuser privileges allow full control over the database system, use with caution):
psql -c "CREATE USER dataverse WITH PASSWORD 'StrongPassword';"
psql -c "ALTER USER dataverse WITH SUPERUSER;"
Step 9: Create a New Database
Create a new database named dbadtv
owned by the dataverse
user:
psql -c "CREATE DATABASE dbadtv OWNER dataverse;"
Step 10: Create a Schema in the Database
Switch to the new database and create a schema named dtvs
:
psql -d dbadtv -c "CREATE SCHEMA dtvs AUTHORIZATION dataverse;"
Step 11: Configure Authentication (pg_hba.conf)
By default, PostgreSQL uses peer authentication. To allow password-based login, update pg_hba.conf
:
vi /var/lib/pgsql/16/data/pg_hba.conf
Modify the following lines to allow password authentication:
host all all 0.0.0.0/0 md5
host all all ::/0 md5
Save and exit the file.
Step 12: Restart PostgreSQL for Changes to Take Effect
Restart PostgreSQL to apply the authentication changes:
systemctl restart postgresql-16
Step 13: Verify the Installation
To ensure PostgreSQL 16 is running properly, connect to the database:
psql -U dataverse -d dbadtv -W
You should be prompted for the password, and upon entering it correctly, you’ll be connected to the database.
Conclusion
This guide provided a step-by-step installation of PostgreSQL 16 on RHEL 9, covering repository setup, database initialization, user creation, and authentication configuration. PostgreSQL 16 offers significant improvements, and with proper configuration, it can serve as a powerful database backend for your applications.
Next Steps:
- Secure your PostgreSQL installation by restricting remote access.
- Tune PostgreSQL parameters for better performance.
- Automate backups for data safety.
📢 Let us know if you have any questions in the comments! 🚀