How to Install PostgreSQL in AlmaLinux 9

PostgreSQL is a powerful, open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.

Update the system

First, we will update the system to the latest with the following commands,

dnf update

Install PostgreSQL Repository

Add the official PostgreSQL Repository for the AlmaLinux system by running the below command,

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

Install the PostgreSQL Server

At the time of writing this article, version 14.4 is the latest version available from PostgreSQL, we will manually select this version for installation in our case.

Check their official website to find out the latest version available at the time of installation.

Install the latest version of PostgreSQL with the below command,

dnf install -y postgresql14-server

Output:

[root@vps ~]# dnf install -y postgresql14-server
PostgreSQL common RPMs for RHEL / Rocky 9 - x86 670  B/s | 195  B     00:00
PostgreSQL common RPMs for RHEL / Rocky 9 - x86 1.6 MB/s | 1.7 kB     00:00
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
 Fingerprint: 68C9 E2B9 1A37 D136 FE74 D176 1F16 D2E1 442D F0F8
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
PostgreSQL common RPMs for RHEL / Rocky 9 - x86 510 kB/s | 278 kB     00:00
PostgreSQL 14 for RHEL / Rocky 9 - x86_64       256  B/s | 195  B     00:00
PostgreSQL 14 for RHEL / Rocky 9 - x86_64       1.6 MB/s | 1.7 kB     00:00
Importing GPG key 0x442DF0F8:
Dependencies resolved.
================================================================================
 Package                  Arch        Version                 Repository   Size
================================================================================
Installing:
 postgresql14-server      x86_64      14.4-1PGDG.rhel9        pgdg14      5.8 M
Installing dependencies:
 libicu                   x86_64      67.1-9.el9              baseos      9.6 M
 lz4                      x86_64      1.9.3-5.el9             baseos       58 k
 postgresql14             x86_64      14.4-1PGDG.rhel9        pgdg14      1.4 M
 postgresql14-libs        x86_64      14.4-1PGDG.rhel9        pgdg14      281 k

Transaction Summary
================================================================================
Install  5 Packages

Creating a New PostgreSQL Database Cluster

Initialize a database storage area on disk by using the following command:

/usr/pgsql-14/bin/postgresql-14-setup initdb

Output:

[root@vps ~]# /usr/pgsql-14/bin/postgresql-14-setup initdb
Initializing database ... OK

Start the PostgreSQL service:

systemctl start postgresql-14

Enable the PostgreSQL service:

systemctl enable postgresql-14

Verify the PostgreSQL service:

systemctl status postgresql-14

Output:

[root@vps ~]# systemctl status postgresql-14
● postgresql-14.service - PostgreSQL 14 database server
     Loaded: loaded (/usr/lib/systemd/system/postgresql-14.service; enabled; vendor prese>
     Active: active (running) since Fri 2022-07-29 14:35:28 UTC; 16s ago
       Docs: https://www.postgresql.org/docs/14/static/
   Main PID: 28586 (postmaster)
      Tasks: 8 (limit: 11129)
     Memory: 16.5M
        CPU: 94ms
     CGroup: /system.slice/postgresql-14.service
             ├─28586 /usr/pgsql-14/bin/postmaster -D /var/lib/pgsql/14/data/
             ├─28588 "postgres: logger "
             ├─28592 "postgres: checkpointer "
             ├─28593 "postgres: background writer "
             ├─28594 "postgres: walwriter "
             ├─28595 "postgres: autovacuum launcher "
             ├─28596 "postgres: stats collector "
             └─28597 "postgres: logical replication launcher "

PostgreSQL Roles

We'll switch to postgres account for this.

sudo -i -u postgres

you can access a postgresql prompt using the psql utility.

psql

Output:

[postgres@vps ~]$ psql
psql (14.4)
Type "help" for help.

postgres=#

To exit out of the postgresql shell type.

\q

Switch back to root user account by exiting the current user mode with the following command:

exit

To create postgresql Role:

createuser --interactive

Output:

[postgres@vps ~]$ createuser --interactive
Enter name of role to add: jones
Shall the new role be a superuser? (y/n) y

PostgreSQL Database

Create a Database by using the following command:

createdb db_name

Enter the Database name

[postgres@vps ~]$ createdb jones

Opening a Postgres Prompt with the New Role

Add new user by using the following command:

sudo adduser jones

To switch over and connect to the database:

sudo -i -u jones

psql

Once you logged in as jones and check your current connection information.

\conninfo

Output:

jones=# \conninfo
You are connected to database "jones" as user "jones" via socket in "/var/run/postgresql" at port "5432".