Table of Contents
What you will read?
PostgreSQL is a powerful open-source relational database system that powers many enterprise applications and modern web platforms. On CentOS Stream 9, you can install it easily using the official repositories or the PostgreSQL Yum repo for newer versions.
Step 1: Update System Packages
Always make sure your system is up to date before installing new software:
sudo dnf update -y
Step 2: Install the PostgreSQL Module
CentOS Stream 9 provides PostgreSQL through AppStream modules. To check available versions:
dnf module list postgresql
Enable the desired version (for example, PostgreSQL 15):
sudo dnf module enable postgresql:15 -y
Now install PostgreSQL:
sudo dnf install postgresql-server postgresql-contrib -y
Step 3: Initialize the Database
Before starting the service, initialize the database cluster:
sudo postgresql-setup --initdb
Step 4: Start and Enable PostgreSQL
Enable and start the PostgreSQL service:
sudo systemctl enable postgresql
sudo systemctl start postgresql
Check status:
systemctl status postgresql
Step 5: Access PostgreSQL
Switch to the PostgreSQL user and enter the shell:
sudo -i -u postgres
psql
To exit the shell:
\q
Step 6: Create a Database and User
Inside the PostgreSQL shell, run:
CREATE DATABASE mydb;
CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Step 7: Enable Remote Connections (Optional)
To allow external connections, edit:
sudo nano /var/lib/pgsql/data/postgresql.conf
Uncomment and set:
listen_addresses = '*'
Then edit the pg_hba.conf file:
sudo nano /var/lib/pgsql/data/pg_hba.conf
Add:
host all all 0.0.0.0/0 md5
Restart PostgreSQL:
sudo systemctl restart postgresql
PostgreSQL is now installed and configured on CentOS Stream 9. Your system is ready to manage relational databases and power modern applications. For more Linux server tutorials, explore the full collection of guides on DropVPS.
