Installing PostgreSQL 12 on Oracle Linux 7 / CentOS 7 / RedHat 7

PostgreSQL 12 on Oracle Linux 7.5

There are various methods available to install PostgreSQL database on your Linux server, however yum is one of the always preferred methods when it comes to RedHat Family Distributions.
As this demonstration is done on Oracle Linux 7.5 you can proceed as it is for the installation on Oracle Linux 7.x RedHat Enterprise Linux 7.x & CentOS 7.x. If you’re planning to install it in any
other distribution or version, you may want to visit PostgreSQL download page, select distro and versions and use correct package to install PostgreSQL as per your OS.

1. Install the RPM of repository so that yum can find and install correct version for your OS, in our case EL-7-86_64. Use below command to install repository RPM.

[[email protected] ~]# yum install

2. Install the psql client package from the above added repository. Use below command to install PosgreSQL Client.

[[email protected] ~]# yum install postgresql12

3. Install PostgreSQL Server package using below command.

[[email protected] ~]# yum install postgresql12-server postgresql12-devel postgresql12-libs postgresql12-contrib

After the successful installation decide the DATA AREA for DATABASE CLUSTER(Where the database files will be stored).
I have attached virtual hard drive of 20 GB.

4. Created partition using fdisk utility.

[[email protected] ~]# fdisk /dev/sdc
n - to create new partition.   THEN
p - to select type as primary.   THEN
w - to write changes to partition table.   THEN

5. Formatting newly created partition using mkfs utility.

[[email protected] ~]# mkfs -t ext3 /dev/sdc1

6. Created directory postgresql under /mnt.

[[email protected] mnt]# mkdir postgresql

7. Mount the storage to the Directory.

[[email protected] data]# mount -t ext3 /dev/sdc1 /mnt/postgresql

8. Verify the storage using below command.

[[email protected] data]# df -hTP
Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs  1.5G     0  1.5G   0% /dev
tmpfs               tmpfs     1.5G     0  1.5G   0% /dev/shm
tmpfs               tmpfs     1.5G  9.6M  1.5G   1% /run
tmpfs               tmpfs     1.5G     0  1.5G   0% /sys/fs/cgroup
/dev/mapper/ol-root xfs        50G  6.4G   44G  13% /
/dev/mapper/ol-home xfs        26G  6.9G   20G  27% /home
/dev/sda1           xfs      1014M  295M  720M  30% /boot
tmpfs               tmpfs     301M   12K  301M   1% /run/user/42
tmpfs               tmpfs     301M     0  301M   0% /run/user/0
/dev/sdb1           ext3       20G   16G  3.5G  82% /u01
/dev/sdc1           ext3       20G   45M   19G   1% /mnt/postgresql

9. Create OS user named postgres for database server.

[[email protected] ~]# useradd postgres

Set Password

[[email protected] ~]# passwd postgres
Changing password for user postgres.
New password: 
BAD PASSWORD: The password contains the user name in some form
Retype new password: 
passwd: all authentication tokens updated successfully.

10. Give ownership of DATA AREA to user postgres.

[[email protected] ~]# chown postgres:postgres -R /mnt/postgresql

Verify it.

[[email protected] mnt]# ls -lrth | grep postgresql
drwxr-xr-x. 3 postgres postgres 4.0K Oct  4 22:45 postgresql

11. Switch to postgres user and create complete structured directory to store data.

[[email protected] ~]# su - postgres 
Last login: Fri Oct  4 23:29:56 IST 2019 on pts/0
[[email protected] ~]$ mkdir -p /mnt/postgresql/12/data

12. Set environment variable in .bash_profile of postgres user.

[[email protected] ~]$ vi .bash_profile

Add below entries.

#PostgreSQL Server Environment Variable Settings.

export PATH
export PGDATA
echo "PG Profile Loaded"
echo "DATA AREA is /mnt/postgresql/12/data"
echo "PostgreSQL Version is 12"

[ESC :wq Enter to save and exit]

13. Initialize Database Cluster.

[[email protected] ~]$ initdb 
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /mnt/postgresql/12/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Kolkata
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /mnt/postgresql/12/data -l logfile start

14. Now start the database server using pg_ctl command.

[[email protected] ~]$ pg_ctl -D /mnt/postgresql/12/data -l logfile start
waiting for server to start.... done
server started

15. Now try to connect to database server using psql command.

[[email protected] ~]$ psql
psql (12.0)
Type "help" for help.


There you go, it’s connected now.

Let’s list pre-created databases.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

16. Create database.

postgres=# create database demprod;

17. Change current database.

postgres=# \c demprod
You are now connected to database "demprod" as user "postgres".

18. You can also set environment variable PGDATABASE to “demprod” to connect to demprod database by default while invoking psql.


Include this environment setting in .bash_profile load profile using . .bash_profile and invoke psql.

[[email protected] ~]$ psql
psql (12.0)
Type "help" for help.


There you go.

 Do follow me on social media and also catch me on 9 different podcast platforms here.

Listen to Podcast of this post, available on :

Peace 🙂