How To Install PostgreSQL On centos/redhat

First we go to https://yum.postgresql.org and into PostgreSQL where we will download the repo. Since we will install PostgreSQL 10 in our example, we will click on 10.

Then right click on “Centos 7 – x86_64” in the screen below and we click copy link location.

We connect to our Centos server via ssh and become root using the below command.

Then write yum install and right click and paste the link that we copied after leaving a space. Run the command to download the repo to the server.

Example Code;

Then, go to the folder which our package is downloaded with the command below to confirm that our package has been downloaded.

List the files in the folder with the command below.

You need to see the package named pgdg-10-centos.repo in the list. Then install PostgreSQL 10 by using the repo we downloaded.

Go to the root directory with the command below.(or the directory you want to create postgresql directories)

Here, you need to define two separate directories for the data and log files. Since this is an example, I will create two files in a single directory.

In our example, we create two folders in the directory named Postgres under the Home directory with the following commands.

Then we use the command cd .. to go back to the parent directory and assign the postgres user as the owner for each file and folder created under the postgres directory and the postgres directory with the following command.

We then specify these folders with the -D and -X parameters when performing initdb.

pg_data (data directory -D)

pg_xlog (wal directory X)

Use the following command to configure authorizations in the root directory.

Create a Password for PostgreSQL

Create a password for postgres user with the below command.

Change the use as postgres with the command below.

Go to the executable files directory with the following command to perform initdb.

initdb

Perform initdb with the following command.

Use the command below to create public and private keys.(ssh connectivity), We’ll be able to connect to the other server without a password between two postgresql server by copying these keys to the standby server. Run this command and press enter a few times.

Then copy the public key to the standby server with the command below.

192.168.1.29 is the IP of the standby server.

It will ask the password of postgres user. If you enter the password, you will get an error. Because the password was not created on the standby server.

Once you have created the password in the standby server, you should run the command again.

Then we will test to see if we can connect without password between two postgresql.

If the connection is not established, turn off the firewall with the following command on the standby server. 

Stop Firewalld

In addition, you need to disable selinux.

Disable selinux

When we are root, we open the file with the command below and change enforcing to disable.

Start PostgreSQL with pg_ctl

Then, start the postgresql with pg_ctl with the help of the following command.

I recommend that you start service with the systemctl command while you are the root user. First, we fix the path of the PostgreSQL service. To do this, open the service using the command below.

Find the Environment line and change it as follows.

Enable, Reload and Start PostgreSQL Service

Then enable and start the service with the command below. If you do not enable the postgres service, it does not start automatically after server restart.

Then switch to the postgres user and connect to postgres using the below command.

If you want to install postgresql replication you can read one of the below articles.

How To Install PostgreSQL Replication with Replication Slots

How To Install PostgreSQL Replication On Centos