postgresql-logo

This is guide, howto install PostgreSQL 15/14/13/12 database server on Fedora 38/37/36, CentOS/Red Hat (RHEL)/Rocky Linux 9/8.5. This guide uses PostgreSQL own YUM repos, which are always up-to-date and stable releases are available instantly. You can use this guide to install any Postgresql versions, like 15, 14, 13, 12 etc.

Check video guide howto install PostgreSQL 14/13/12 on Fedora 35/34 using PostgreSQL’s own dnf repo:

Check video guide howto install PostgreSQL 14/13/12 on Rocky Linux 8.5 using PostgreSQL’s own dnf repo:

Note: If you are upgrading PostgresSQL (from earlier version), then make sure that you backup (dump and copy) your database and configs.

1. Install PostgreSQL 15 Database Server on Fedora 38/37/36, CentOS/Red Hat (RHEL)/Rocky Linux 9/8.5

1.1 Change root user

su -
## OR ##
sudo -i

1.2 Exclude Fedora, CentOS, Red Hat (RHEL) and Rocky Linux own PostgreSQL Packages

This is important step to get PostgreSQL repository working properly. Exclude PostgreSQL packages from the repository of the distro.

Fedora

Add exclude to /etc/yum.repos.d/fedora.repo file [fedora] section:

[fedora]
...
exclude=postgresql*

Add exclude to /etc/yum.repos.d/fedora-updates.repo file [updates] section:

[updates]
...
exclude=postgresql*

CentOS / Red Hat (RHEL) / Rocky Linux

Disable postgresql module:

dnf -qy module disable postgresql

1.3 Install PostgreSQL 15 Repository

1.3.1 Fedora 38/37/36

## Fedora 38/37/36 ##
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/F-38-x86_64/pgdg-fedora-repo-latest.noarch.rpm

1.3.2 CentOS/RHEL/Rocky Linux 9

## CentOS/RHEL/Rocky Linux 9 - x86_64 - 64-bit ##
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm

1.3.3 CentOS/RHEL/Rocky Linux 8

## CentOS/RHEL/Rocky Linux 8 - x86_64 - 64-bit ##
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

1.4 Install PostgreSQL 15 using DNF

## Fedora 38/37/36 ##
dnf install -y postgresql15 postgresql15-server

## Centos/RHEL/Rocky Linux 9/8 ##
dnf install -y postgresql15 postgresql15-server

2. Configure PostgreSQL 15

2.1 Initialize Cluster with initdb Command

## Fedora 38/37/36 and CentOS/RHEL/Rocky 9/8 ##
/usr/pgsql-15/bin/postgresql-15-setup initdb

2.2 Set PostgreSQL Server to Listen Addresses and Set Port

Open /var/lib/pgsql/14/data/postgresql.conf file, and add/uncomment/modify following:

listen_addresses = '*'
port = 5432

If you want just localhost setup, then use following:

listen_addresses = 'localhost'
port = 5432

Or if you want use specific ip, then use following:

listen_addresses = '192.1.2.33'
port = 5432

2.3 Set PostgreSQL Permissions

Modify PostgreSQL /var/lib/pgsql/14/data/pg_hba.conf (host-based authentication) file:

# Local networks
host	all	all	                xx.xx.xx.xx/xx	scram-sha-256
# Example
host	all	all     	        10.20.4.0/24	scram-sha-256
# Example 2
host	test	testuser        127.0.0.1/32	scram-sha-256

You can find more examples and full guide from PostgreSQL pg_hba.conf manual.

2.4 Start PostgreSQL Server and Autostart PostgreSQL on Boot

2.4.1 Fedora 38/37/36 and CentOS / Red Hat (RHEL) / Rocky Linux 9/8

## Start PostgreSQL 15 ##
systemctl start postgresql-15.service

## Start PostgreSQL 15 on every boot ##
systemctl enable postgresql-15.service

2.5 Create Test Database and Create New User

2.5.1 Change to postgres user

su - postgres

2.5.2 Create test database (as postgres user)

createdb test

2.5.3 Login test database (as postgres user)

psql test

2.5.4 Create New “testuser” Role with Superuser and Password

CREATE ROLE testuser WITH SUPERUSER LOGIN PASSWORD 'test';

2.5.5 Test Connection from localhost (as Normal Linux User)

psql -h localhost -U testuser test

3. Enable Remote Connections to PostgreSQL Server –> Open PostgreSQL Port (5432) on Iptables Firewall on Fedora 35/34/33 and CentOS/Red Hat (RHEL)/Rocky Linux 8.5

3.1 List Your Active Firewalld Zones

firewall-cmd --get-active-zones

Example output:

public
  interfaces: wlp1s0

3.2 Add New Rule to Firewalld

You might have active zone like public, FedoraWorkstation, FedoraServer.

firewall-cmd --permanent --zone=public --add-service=postgresql

## OR ##

firewall-cmd --permanent --zone=public --add-port=5432/tcp

3.3 Restart firewalld.service

systemctl restart firewalld.service

4. Test remote connection

psql -h dbserver_name_or_ip_address -U testuser -W test

Note: You have to allow remote connections on pg_hba.conf, check step 2.3 and PostgreSQL pg_hba.conf manual.