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

In this step-by-step tutorial, we will guide you through the process of installing PostgreSQL on a CentOS 8 server.

Posted on

PostgreSQL is a powerful, open-source relational database management system that is widely used for various applications, from small projects to large-scale enterprise systems. In this step-by-step tutorial, we will guide you through the process of installing PostgreSQL on a CentOS 8 server. We will also cover basic database management tasks and provide an example of creating a database and table. By the end of this article, you’ll have a fully functional PostgreSQL installation up and running on your CentOS 8 system.

Preconditions

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

  1. A CentOS 8 server with root or sudo access.
  2. A stable internet connection to download PostgreSQL packages.
  3. Basic knowledge of the Linux command line.

Now that you have the preconditions in place, let’s proceed with the installation.

Step 1: Update the System

It’s essential to ensure your CentOS system is up to date before installing any software. Open a terminal and run the following commands:

sudo dnf update

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

Step 2: Install PostgreSQL

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

sudo dnf install postgresql-server postgresql-contrib

This command will install the PostgreSQL server and additional contrib packages, which provide useful extensions and utilities for PostgreSQL.

Step 3: Initialize the PostgreSQL Database

Once PostgreSQL is installed, you need to initialize the database cluster. Run the following command:

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 the following commands:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Step 5: Create a PostgreSQL User and Database

Now, 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

Step 6: Example - Create a Table

Let’s create a simple table in the newly created database. You can use the PostgreSQL command-line tool psql:

sudo -u postgres psql -d your_database

Once you’re in the psql prompt, run 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

Here are some common commands to manage the PostgreSQL service:

  • 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 8 server, created a database, and learned how to perform basic management tasks. PostgreSQL offers a wide range of features and capabilities, making it a versatile choice for your data storage needs. Explore PostgreSQL’s documentation to harness its full potential and tailor it to your specific requirements.

With PostgreSQL up and running, you can start building and managing databases for your applications, and leverage its scalability and reliability for your data-driven projects.

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