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.
1 |
su root |
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;
1 |
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.
1 |
cd /etc/yum.repos.d |
List the files in the folder with the command below.
1 |
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.
1 |
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)
1 |
cd / |
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.
1 2 |
mkdir pg_data mkdir pg_xlog |
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.
1 |
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.
1 2 |
chown -R postgres /postgres chmod -R 700 /postgres |
Create a Password for PostgreSQL
Create a password for postgres user with the below command.
1 |
passwd postgres |
Change the use as postgres with the command below.
1 |
su - postgres |
Go to the executable files directory with the following command to perform initdb.
1 |
cd /usr/pgsql-10/bin |
initdb
Perform initdb with the following command.
1 |
/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.
1 |
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.
1 |
ssh-copy-id 192.168.1.29 |
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.
1 |
ssh postgres@192.168.1.29 |
If the connection is not established, turn off the firewall with the following command on the standby server.
Stop Firewalld
1 |
service firewalld stop |
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.
1 |
vim /etc/selinux/config |
Start PostgreSQL with pg_ctl
Then, start the postgresql with pg_ctl with the help of the following command.
1 |
/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.
1 |
vi /lib/systemd/system/postgresql-10.service |
Find the Environment line and change it as follows.
1 |
Environment=PGDATA=/postgres/pg_data/ |
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.
1 2 3 |
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.
1 2 |
su - postgres psql |
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“