In my experience I have found that Postgres has proved to the be most reliable relational database platform. This is largely based on the fact that is open source and the community is huge. It is scalable across platforms and is a widely used and well-loved tool. In this tutorial, we’ll show you how to install PostgreSQL on CentOS 7!

PostgreSQL uses SQL for relational queries and JSON for non-relational queries. Being open source means that a lot of developers contribute to this utility’s growth.

PostgreSQL is extremely simple to start using and master.

Here we will demonstrate how to install PostgreSQL on CentOS 7.

Now there are two ways to do it:

  • First, using CentOS repos.
  • Second, Postgres repos.

First Method – Install PostgreSQL on CentOS 7 using the CentOS repositories

The CentOS 7 repository contains PostgreSQL. Note that it may not have the latest version of PostgreSQL. At the time of writing the repository hosts PostgreSQL version 9.2.15.

1. Install PostgreSQL on CentOS 7

On your CentOS Terminal run the commands mentioned below:

yum -y install postgresql

yum -y install postgresql-contrib

yum -y install postgresql-server

This might take some time to complete.

After execution is completed check the postgres version that is installed on your system. Using

postgres -V

As you can see the version installed on my system is Postgres 9.2.

3. Initialize the Database

Once the installation is done, you can initialize the database using the below command:

postgresql-setup initdb

4. Start the Database

After initializing the database, you can start the database using:

systemctl start postgresql

5. (Optional) Enable PostgreSQL

This completes our database installation and initialization. If required you can configure PostgreSQL to start on every system reboot automatically.

systemctl enable postgresql

Second Method  – Install PostgreSQL on CentOS 7 using the PostgreSQL repository

Using this method you are in better control of which exact Postgres version you want to install on your system and also get the latest Postgres installed if needed.

1. Download PostgreSQL using wget

Open the officical PostgresSQL download page.

2. Select your desired Postgres version and your Linux Distro.

3. Run the commands from the script provided by Postgres Download Page.

You can either run the individual commands from the script OR create a small executable script on your machine and run it.


You can take this slightly modified script that will not prompt you during installation, set the system path and install some additional libraries


# Install the repository RPM:

yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL:

yum -y install postgresql12

yum -y install postgresql12-server

yum -y install postgresql12-contrib

# Optionally initialize the database and enable automatic start:

/usr/pgsql-12/bin/postgresql-12-setup initdb

systemctl enable postgresql-12

systemctl start postgresql-12

#Setting path

export PATH=/usr/pgsql-12/bin:$PATH

4. Create a new script

Create a new file and open it using vim

vi postgres_install.sh

5. Copy the contents from the web page to your file

6. Make the script executable

chmod a+x postgres_install.sh

7. Run script

From the directory where the script is located run


If you used the script from Postgres official site you will be prompted multiple times during installation, type ‘y’ and press enter.

8. Check installation

Using postgres -V check the installed version

If you can see the above messages it means that you have successfully installed Postgres on your system.

Cheers! See you in the next one.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: