Menu

How to Install PostgreSQL on CentOS Stream 9: A Step-by-Step Tutorial

Install PostgreSQL 18 on CentOS Stream 9 with its AppStream module, initialize the cluster, and start the service. CentOS Stream 9 ends May 31, 2027.

Posted on By
On this page

PostgreSQL, often referred to as Postgres, is a powerful and open-source relational database management system widely used for various applications. In this step-by-step tutorial, we will guide you through the process of installing PostgreSQL on a CentOS Stream 9 system. We will also cover essential database management tasks, ensuring you have the knowledge to set up and work with PostgreSQL on your CentOS Stream 9 server.

Support status: The CentOS Project lists CentOS Stream 9 end of life as May 31, 2027. If you deploy this release, plan its migration before that date and check the CentOS release information for updates.

Version note: The CentOS Stream 9 package metadata includes PostgreSQL 18. This guide selects that module stream explicitly. The PostgreSQL project supports major version 18 through November 2030; see its versioning policy.

Preconditions

Before you begin the installation process, make sure you meet the following prerequisites:

  1. A CentOS Stream 9 server with root or sudo access.
  2. A stable internet connection to download PostgreSQL packages.
  3. Familiarity with basic Linux command-line operations.

With these prerequisites met, let’s proceed with the installation.

Installing PostgreSQL on CentOS Stream 9

Step 1: Update the System

Start by ensuring your CentOS Stream 9 system is up to date. Open a terminal and run the following commands:

sudo dnf update

This command updates the package list and upgrades existing packages on your system.

Step 2: Install PostgreSQL

Install the PostgreSQL 18 server module:

sudo dnf module install postgresql:18/server

This selects the PostgreSQL 18 stream and its server profile.

Step 3: Initialize the PostgreSQL Database

After the installation, initialize the PostgreSQL database cluster with the following command:

sudo postgresql-setup --initdb

This command creates the necessary directory structure and configuration files for PostgreSQL.

Step 4: Start and Enable PostgreSQL

To start the PostgreSQL service and enable it to start automatically at boot, use these commands:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Creating a PostgreSQL User and Database

Let’s create a new PostgreSQL user and database. Replace your_user and your_password with your desired values:

sudo -u postgres createuser your_user
sudo -u postgres createdb -O your_user your_database

To demonstrate PostgreSQL’s capabilities, let’s create a simple table in the newly created database. Access the PostgreSQL command-line tool, psql, using the following command:

sudo -u postgres psql -d your_database

Once in the psql prompt, execute the following SQL commands to create a basic table:

CREATE TABLE example (
    id serial PRIMARY KEY,
    name VARCHAR (100),
    age INT
);

Managing the PostgreSQL Service

To manage the PostgreSQL service on CentOS Stream 9, you can use the following commands:

  • Start PostgreSQL service: sudo systemctl start postgresql
  • Stop PostgreSQL service: sudo systemctl stop postgresql
  • Restart PostgreSQL service: sudo systemctl restart postgresql
  • Check PostgreSQL service status: sudo systemctl status postgresql

Conclusion

Congratulations! You have successfully installed PostgreSQL on your CentOS Stream 9 server, created a database, and learned how to perform basic management tasks. PostgreSQL’s extensive feature set makes it an excellent choice for various data storage needs. To unleash the full potential of PostgreSQL, explore its documentation and adapt it to your specific requirements.

With PostgreSQL now up and running, you are well-prepared to build and manage databases for your applications, leveraging its scalability and reliability for your data-driven projects.

If you want to learn more about PostgreSQL, please use our PostgreSQL tutorials and PostgreSQL Reference.