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

In this comprehensive tutorial, we will guide you through the process of installing PostgreSQL on a CentOS 7 server.

Posted on

PostgreSQL is a robust, open-source relational database management system that is widely used for a variety of applications, from small projects to large-scale enterprise systems. In this comprehensive tutorial, we will guide you through the process of installing PostgreSQL on a CentOS 7 server. We will also cover essential database management tasks, providing you with a solid foundation for working with PostgreSQL. By the end of this article, you’ll have PostgreSQL up and running on your CentOS 7 system.

Preconditions

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

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

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

Installing PostgreSQL on CentOS 7

Step 1: Update the System

To ensure your CentOS 7 system is up to date, open a terminal and run the following commands:

sudo yum update

This command updates the package list and upgrades existing packages.

Step 2: Install PostgreSQL

To install PostgreSQL on CentOS 7, execute the following command:

sudo yum install postgresql-server postgresql-contrib

This command installs both the PostgreSQL server and additional contrib packages that provide useful extensions and utilities.

Step 3: Initialize the PostgreSQL Database

After installing PostgreSQL, you need to initialize the database cluster. Run the following command:

sudo postgresql-setup initdb

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

Step 4: Start and Enable PostgreSQL

Start the PostgreSQL service and enable it to start automatically at boot with 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 demonstrate PostgreSQL’s functionality, 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 you’re 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 7, 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 7 server, created a database, and learned how to perform basic management tasks. PostgreSQL offers a wide range of features and capabilities, making it an excellent choice for your data storage needs. Explore PostgreSQL’s documentation to harness its full potential and adapt it to your specific requirements.

With PostgreSQL up and running, you can start building and managing databases for your applications, taking advantage of 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.