Install and Use PostgreSQL on Rocky Linux 8

In this guide, we want to teach you to Install and Use PostgreSQL on Rocky Linux 8.

PostgreSQL is an advanced, enterprise-class open source relational database that supports both SQL (relational) and JSON (non-relational) querying. It is a highly stable database management system, backed by more than 20 years of community development which has contributed to its high levels of resilience, integrity, and correctness. PostgreSQL is used as the primary data store or data warehouse for many webs, mobile, geospatial, and analytics applications.

How To Install and Use PostgreSQL on Rocky Linux 8

To install PostgreSQL, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Rocky Linux 8.

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

Installing PostgreSQL on Rocky Linux 8

First, you need to add the PostgreSQL repository on Rocky Linux 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 Rocky Linux 8 with the following command:

sudo dnf module list postgresql
Output
Rocky Linux 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.8

Managing PostgreSQL on Rocky Linux 8

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

sudo /usr/pgsql-*/bin/postgresql-*-setup initdb
Output
Initializing database … OK

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 Rocky Linux 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 Tue 2022-08-16 06:03:44 EDT; 5s ago
     Docs: https://www.postgresql.org/docs/13/static/
  Process: 89461 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PG>
 Main PID: 89466 (postmaster)
    Tasks: 8 (limit: 11413)
   Memory: 16.9M
   CGroup: /system.slice/postgresql-13.service
           ├─89466 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
....

How To Use PostgreSQL

At this point, we want to show you the basic usage of PostgreSQL.

You can enter the PostgreSQL shell on Rocky Linux 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.8)
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: reita
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 Rocky Linux 8.

Hope you enjoy it.

Please subscribe to us on Facebook and Twitter.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!