Postgresql installation on redhat 9 and Postgresql 15

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
  • User AvatarAshiwini
  • 09 Jul, 2024
  • 3 Mins Read

Postgresql installation on redhat 9 and Postgresql 15

The Red Hat family of distributions includes:

  • Red Hat Enterprise Linux
  • Rocky Linux
  • AlmaLinux
  • CentOS (7 and 6 only)
  • Fedora
  • Oracle Linux

PostgreSQL is available on these platforms by default. However, each version of the platform normally “snapshots” a specific version of PostgreSQL that is then supported throughout the lifetime of this platform. Since this can often mean a different version than preferred, the PostgreSQL project provides a repository of packages of all supported versions for the most common distributions.

PostgreSQL Yum Repository

The PostgreSQL Yum Repository will integrate with your normal systems and patch management, and provide automatic updates for all supported versions of PostgreSQL throughout the support lifetime of PostgreSQL.

The PostgreSQL Yum Repository currently supports:

  • Red Hat Enterprise Linux
  • Rocky Linux
  • AlmaLinux
  • CentOS (7 and 6 only)
  • Oracle Linux
  • Fedora*

Note: due to the shorter support cycle on Fedora, all supported versions of PostgreSQL are not available on this platform. We do not recommend using Fedora for server deployments.

To use the PostgreSQL Yum Repository, follow these steps:

Postgresql installation on redhat 9 and Postgresql 15

Step 1: Update Your System

First, ensure that your system is up-to-date by running:

sudo dnf update -y
Step 2: Add the PostgreSQL Repository

PostgreSQL provides repositories for various versions. Add the PostgreSQL 15 repository to your system:

sudo dnf install -y
Step 3: Disable the Default PostgreSQL Module

Red Hat Enterprise Linux 9 may come with a default PostgreSQL module enabled. Disable it to avoid conflicts:

sudo dnf -qy module disable postgresql
Step 4: Install PostgreSQL 15

Now, install PostgreSQL 15:

sudo dnf install -y postgresql15-server
Step 5: Initialize the Database

Initialize the PostgreSQL database with the following command:

sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
Step 6: Start and Enable PostgreSQL Service

Start the PostgreSQL service and enable it to start on boot:

sudo systemctl enable --now postgresql-15
sudo systemctl start postgresql-15
Step 7: Verify Installation

Verify that PostgreSQL is running:

sudo systemctl status postgresql-15
Step 8: Configure PostgreSQL (Optional)

You may want to configure PostgreSQL to allow remote connections or set up other configurations. The primary configuration files are located in /var/lib/pgsql/15/data/.

To allow remote connections, edit the postgresql.conf and pg_hba.conf files:

sudo nano /var/lib/pgsql/15/data/postgresql.conf

Uncomment and set the listen_addresses line to:

listen_addresses = '*'

Then, configure pg_hba.conf to allow remote connections:

sudo nano /var/lib/pgsql/15/data/pg_hba.conf

Add the following line (replace with the appropriate subnet or IP address):

host    all             all               md5
Step 9: Restart PostgreSQL

After making configuration changes, restart the PostgreSQL service:

sudo systemctl restart postgresql-15
Step 10: Access PostgreSQL

Switch to the postgres user and access the PostgreSQL prompt:

sudo -i -u postgres

How to set bash profile

step 1:Open or Create the .bash_profile File

Open the .bash_profile file in your home directory using a text editor. If the file doesn’t exist, this command will create it:

nano ~/.bash_profile
step 2:Add Your Custom Configurations

You can add various configurations, such as environment variables, aliases, and function definitions. Here’s an example of what you might add:

# Add PostgreSQL binaries to PATH 
export PATH=$PATH:/usr/pgsql-15/bin 
# Set PostgreSQL data directory 
export PGDATA=/var/lib/pgsql/15/data 
# Alias for starting PostgreSQL 
alias pgstart='sudo systemctl start postgresql-15' 
alias pgstop='sudo systemctl stop postgresql-15' 
alias pgstatus='sudo systemctl status postgresql-15' 
# Custom prompt 
export PS1="\u@\h:\w\$ "
step 3:Save and Close the File

If you are using nano, you can save the file by pressing Ctrl+O, then Enter, and exit by pressing Ctrl+X.

step 4:Apply the Changes

To apply the changes made to the .bash_profile, you need to source the file. This will load the new configurations into your current shell session:

source ~/.bash_profile
step 5:Verify the Changes

Verify that your changes have been applied. For instance, check the PATH variable or any aliases you set:

echo $PATH pgstatus


Due to policies for Red Hat family distributions, the PostgreSQL installation will not be enabled for automatic start or have the database initialized automatically. To make your database installation complete, you need to perform the following steps, based on your distribution:

For RHEL / Rocky Linux / AlmaLinux / CentOS / SL / OL 7, 8, 9 Or Fedora 39 And Later Derived Distributions:
postgresql-setup --initdb
systemctl enable postgresql.service
systemctl start postgresql.service