How to install PostgreSQL 12 server on Centos 8

A simple set of instruction on how to install PostgreSQL 12 on Centos 8.x

INSTALLATION

Install latest repos for EL-8:

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

Let’s check if Postgres 12 is listed as an installable server.

dnf module list postgresql | grep 12

If you see the ‘PostgreSQL 12’ option listed, we’re good to go.

Let’s install the PostgreSQL 12 packages:

sudo dnf install @postgresql:12

Initialize the database service:

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

Let’s enable the database service.

sudo systemctl enable --now postgresql-12

Confirm that the service is running:

$ sudo systemctl status postgresql-12 | grep Active:

On Centos 8, the default firewall service needs to be enabled if you expect to connect to Postgres from a remote computer.

sudo firewall-cmd --add-service=postgresql --permanent && sudo firewall-cmd --reload

Set the password for the PostgreSQL admin user:

$ sudo su - postgres 
~]$ psql -c "alter user postgres with password 'MySecurePassword'" 
ALTER ROLE

Once done, type: exit, to leave Postgres.

If you plan on logging to PostgreSQL 12 from a remote computer, we’ll need to amend the postgresql config file and allow it to listed on all interfaces.

Open this file:

sudo vi /var/lib/pgsql/12/data/pg_hba.conf

And add to it:

# Accept connections from any machine on any trusted subnet
host all all 0.0.0.0/0 md5
host all all 192.168.1.0/24 md5

It should look like this, then save it.

Now, configure PostgreSQL to run on the IP address dedicated to the server. Open postgresql.conf

sudo vi /var/lib/pgsql/12/data/postgresql.conf

Then change the directive called: ‘listen_addresses’

Save the file and now restart the PostgreSQL 12 database:

sudo systemctl restart postgresql-12

Let’s try to connect to it, using your PostgreSQL client, in my case HeidiSQL:

And Voila!, we’re connected: