×


PostgreSQL installation on Centos 7

The installation of PostgreSQL on Centos can be done via the default Centos repositories or via PostgreSQL official repositories.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers with Software installations on their Servers.

In this context we shall look into how to install PostgreSQL on Centos.

Steps to install PostgreSQL on Centos from Centos repository

To begin, Log into the Server via an ssh tool such as putty and update the System first with the command below;

sudo yum update


After Centos update process is completed, you can install Centos repositories via Centos repositories with the command below;

sudo yum install postgresql-server postgresql-contrib


The nest step is to install the Postgres database and start PostgreSQL service with the commands below;

sudo postgresql-setup initdb
sudo systemctl start postgresql


It is recommended that you set PostgreSQL to start on boot using the command below;

sudo systemctl enable postgresql


PostgreSQL installation on Centos via PostgreSQL official repository

We discovered that the version of PostgreSQL available in the Centos 7 repository is usually obsolete. This is why it is better to get it via PostgreSQL official repository.

To do this, ensure that you exclude the packages from Centos repository relating to PostgreSQL so that the dependencies do not conflict with the official one from PostgreSQL official repository.

Now open the configuration file of the repository with the command below;

sudo vi /etc/yum.repos.d/CentOS-Base.repo


In this file, look out for the "[base]" and "[updates]" sections and integrate the exclude=postgresql* line in both sections so that it looks like this;

...
[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

exclude=postgresql*

#released updates
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
exclude=postgresql*
...



Then you can save and exit this file.

Next, you can proceed with the installation of a repository configuration package with the official PostgreSQL repository for Centos with the command below;

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


All the available PostgreSQL releases are contained in the PostgreSQL repository which can be listed with the command below;

um list postgresql*


From here you can choose any version of PostgreSQL and install.

Proceed with PostgreSQL server installation with the command;

sudo yum install postgresql11-server


You will prompted to enter a GPG key while going through this installation process. To import a GPG key, enter "y" to complete the installation.

Now that the installation is complete, you can set up database cluster for PostgreSQL.

How to create a new PostgreSQL Database Cluster?

A new PostgreSQL database cluster must be created before we can use the PostgreSQL database. To do this, you need to create the directories where the database data will be placed as well as generating the shared catalog tables and create the template1 and postgres databases.

With initdb, you can create a new PostgreSQL with the command below;

sudo /usr/pgsql-11/bin/postgresql-11-setup initdb


If successful, you will see the output as shown below;

Initializing database … OK

Next, you can use systemctl to start and enable PostgreSQL  with the commands below;

sudo systemctl start postgresql-11
sudo systemctl enable postgresql-11


You will get the following output;

Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-11.service to /usr/lib/systemd/system/postgresql-11.service.


Now PostgreSQL will be up and running on your server.

Need support to install PostgreSQL on Linux? We can help you today.


Conclusion

This article will show you how to install PostgreSQL via the default Centos repositories or from the official Postgres repositories.