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.6)
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: Adam
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 my_db

Opening a Postgres Prompt with the New Role

We will add a new user for managing the roles accordingly. For this, switch back to the root user account by exiting the current user mode with the following command:

exit

Then, run the adduser command to add the new user on the system.

adduser username

Examples:

[root@vps ~]# adduser Adam

Now, switch over to the newly added user and enter into the Database.

sudo -i -u username

psql -d database_name

Examples

[root@vps ~]# sudo -i -u Adam
[Adam@vps ~]$ psql -d my_db
psql (14.6)
Type "help" for help.

Once you logged in as "Adam" user and into the "my_db" database, check your current connection information.

\conninfo

Output:

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

This concludes the topic of installing PostgreSQL on AlmaLinux 9.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS