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.
yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
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.
ll or ls -al
You need to see the package named pgdg-10-centos.repo in the list. Then install PostgreSQL 10 by using the repo we downloaded.
yum install --disablerepo="*" --enablerepo=pgdg-10-centos.repo postgresql10.x86_64 postgresql10-contrib.x86_64 postgresql10-devel.x86_64 postgresql10-libs.x86_64 postgresql10-server.x86_64
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.
chown -R postgres:postgres /postgres
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.
chown -R postgres /postgres
chmod -R 700 /postgres
Create a Password for PostgreSQL
Create a password for postgres user with the below command.
Change the use as postgres with the command below.
su - postgres
Go to the executable files directory with the following command to perform initdb.
Perform initdb with the following command.
/usr/pgsql-10/bin /initdb -D /postgres/pg_data/ -X /postgres/pg_xlog/
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.
ssh-keygen -t rsa
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.
service firewalld stop
In addition, you need to 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.
/usr/pgsql-10/bin/pg_ctl -D /postgres/pg_data/ start
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.
systemctl enable postgresql-10.service
systemctl start postgresql-10.service
systemctl reload postgresql-10.service
Then switch to the postgres user and connect to postgres using the below command.
su - postgres
If you want to install postgresql replication you can read one of the below articles.