Menu

How to Install PostgreSQL on CentOS Stream 8 (Legacy Guide)

Install PostgreSQL on CentOS Stream 8 for legacy systems. Updates ended in May 2024; see CentOS Stream 9 for a maintained CentOS guide.

Posted on By Updated on
On this page

PostgreSQL, often referred to as Postgres, is a robust and open-source relational database management system. It is widely used for various applications, from small-scale projects to large enterprise solutions. In this step-by-step tutorial, we will guide you through the process of installing PostgreSQL on a CentOS Stream 8 system. We will also cover essential database management tasks, providing you with the knowledge to get started with PostgreSQL on your CentOS Stream 8 server.

Support status: CentOS Stream 8 reached end of life on May 31, 2024. Keep this guide for legacy systems; for a CentOS Stream installation, see our PostgreSQL guide for CentOS Stream 9. The CentOS Project lists Stream 9 support through May 31, 2027 in its release information.

Package Version

The postgresql-server package in the commands below installs the version provided by CentOS Stream 8’s package repositories; it does not select a PostgreSQL version independently of the operating system. Check the installed version with psql --version if you need to identify an existing legacy installation.

Preconditions

Before you begin the installation process, ensure you have met the following prerequisites:

  1. A CentOS Stream 8 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 in place, let’s proceed with the installation.

Installing PostgreSQL on CentOS Stream 8

Step 1: Update the System

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

sudo dnf update

This command will update the package list and upgrade existing packages on your system.

Step 2: Install PostgreSQL

To install PostgreSQL on CentOS Stream 8, use the following command:

sudo dnf install postgresql-server postgresql-contrib

This command will install both the PostgreSQL server and additional contrib packages that include useful extensions and utilities.

Step 3: Initialize the PostgreSQL Database

After the installation, initialize the PostgreSQL database cluster by running:

sudo postgresql-setup --initdb

This command will create 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

Now, let’s create a new PostgreSQL user and database. Replace your_user and your_password with your preferred values:

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

To illustrate PostgreSQL’s capabilities, let’s create a simple table in the newly created database. Access the PostgreSQL command-line tool, psql, with 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 8, 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

You have successfully installed PostgreSQL on your CentOS Stream 8 server, created a database, and learned how to perform basic management tasks. PostgreSQL’s feature-rich nature makes it an excellent choice for a wide range of data storage needs. Explore PostgreSQL’s documentation to fully leverage its capabilities and adapt it to your specific requirements.

With PostgreSQL now installed and operational, you are well-equipped to build and manage databases for your applications, harnessing 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.