How To Install and Use PostgreSQL on AlmaLinux 8

In this article, we want to teach you How To Install and Use PostgreSQL on AlmaLinux 8.

PostgreSQL is an open-source database that has a strong reputation for its reliability, flexibility, and support of open technical standards. Unlike other RDMBS (Relational Database Management Systems), PostgreSQL supports both non-relational and relational data types. This makes it one of the most compliant, stable, and mature relational databases available today.

How To Install and Use PostgreSQL on AlmaLinux 8

Before you start to install PostgreSQL on AlmaLinux 8, you need to log to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with AlmaLinux 8.

Now you can follow the steps below to complete this guide.

Installing PostgreSQL on AlmaLinux 8

First, you need to add the PostgreSQL repository on AlmaLinux 8 with the following command:

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

Then, you need to update your local package index with the following command:

sudo dnf update -y

Check the PostgreSQL module list on AlmaLinux 8 with the following command:

sudo dnf module list postgresql
Output
AlmaLinux 8 - AppStream
Name Stream Profiles Summary
postgresql 9.6 client, server [d] PostgreSQL server and client module
postgresql 10 [d] client, server [d] PostgreSQL server and client module
postgresql 12 client, server [d] PostgreSQL server and client module
postgresql 13 client, server [d] PostgreSQL server and client module

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

As you can see the latest available version of PostgreSQL is 13.

Now you need to disable the default module with the command below:

sudo dnf -qy module disable postgresql

At this point, you can install the PostgreSQL client and server with the following command:

sudo dnf install postgresql13 postgresql13-server

Verify your PostgreSQL installation by checking its version:

psql -V
Output
psql (PostgreSQL) 13.6

Manage PostgreSQL on AlmaLinux 8

First, you need to initialize the database with the command below:

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

Then, you can enable your PostgreSQL service to start on boot with the command below:

sudo systemctl enable postgresql-13

Next, start the PostgreSQL service on AlmaLinux 8 with the command below:

sudo systemctl start postgresql-13

Verify your service is active and running on your server with the command below:

sudo systemctl status postgresql-13
Output
postgresql-13.service - PostgreSQL 13 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; enabled; vend>
Active: active (running) since Sat 2022-03-12 03:40:48 EST; 54s ago
Docs: https://www.postgresql.org/docs/13/static/
Process: 89511 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PG>
Main PID: 89517 (postmaster)
Tasks: 8 (limit: 11409)
Memory: 16.9M
CGroup: /system.slice/postgresql-13.service
....

Using PostgreSQL

You can enter the PostgreSQL shell on AlmaLinux 8 by switching to the Postgres user:

sudo su - postgres

You should see that your command prompt changes to Potgress.

[postgres@ ~]$

Now you can access the database prompt with the following command:

psql
Output
psql (13.6)
Type "help" for help.

postgres=#

You can check your current connection information with the command below:

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

To exit out of the PostgreSQL shell type:

\q

Change to your original account by using the following command:

exit

You can create a PostgreSQL role with the command below:

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

Also, you can create a database with the command below:

[postgres@ ~]$ createdb orca

For more information, you can visit the PostgreSQL Documentation Page.

Conclusion

At this point, you learn to Install and Use PostgreSQL on AlmaLinux 8.

Hope you enjoy it.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Stay informed and not overwhelmed, subscribe now!