PostgreSQL is a free, open-source, object-relational database system designed to scale the most complicated data workloads. Generally, it is used as the primary data store for many web, mobile, geospatial, and analytics applications. PostgreSQL can store structured and unstructured data in a single product. PostgreSQL 18 comes with a variety of features that help users to deploy their data-backed applications.

In this post, we will show you how to install PostgreSQL 18 in Rocky Linux 8.

Step 1 – Add PostgreSQL 18 Repository

By default, the latest version of PostgreSQL is not included in the Rocky Linux 8 default repo.

First, update the repository index.

dnf update -y                 

Next, install the PostgreSQL 18 repository using the following command:

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

Once the PostgreSQL 18 repository is added, you can move to the next step.

Also Read

How To Secure PostgreSQL Server

Step 2 – Install PostgreSQL 18 on Rocky Linux 8

Run the following command to install the PostgreSQL 18:

dnf install postgresql18 postgresql18-server -y

Once the installation is finished, initialized the PostgreSQL database with the following command:

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

Next, start and enable the PostgreSQL service using the following command:

systemctl start postgresql-18
systemctl enable postgresql-18

To check the status of PostgreSQL, run the following command:

systemctl status postgresql-18

You will get the following output:

ā— postgresql-18.service - PostgreSQL 18 database server
     Loaded: loaded (/usr/lib/systemd/system/postgresql-18.service; disabled; preset: disabled)
     Active: active (running) since Thu 2025-10-16 09:13:35 EDT; 8s ago
 Invocation: 2abe7ccca34444f9a0c08ce7fcbe1c8f
       Docs: https://www.postgresql.org/docs/18/static/
    Process: 3871 ExecStartPre=/usr/pgsql-18/bin/postgresql-18-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
   Main PID: 3877 (postgres)
      Tasks: 10 (limit: 24809)
     Memory: 21.6M (peak: 22M)
        CPU: 65ms
     CGroup: /system.slice/postgresql-18.service
             ā”œā”€3877 /usr/pgsql-18/bin/postgres -D /var/lib/pgsql/18/data/
             ā”œā”€3878 "postgres: logger "
             ā”œā”€3879 "postgres: io worker 0"
             ā”œā”€3880 "postgres: io worker 1"
             ā”œā”€3881 "postgres: io worker 2"
             ā”œā”€3882 "postgres: checkpointer "
             ā”œā”€3883 "postgres: background writer "
             ā”œā”€3885 "postgres: walwriter "
             ā”œā”€3886 "postgres: autovacuum launcher "
             └─3887 "postgres: logical replication launcher "

Oct 16 09:13:35 rocky systemd[1]: Starting postgresql-18.service - PostgreSQL 18 database server...
Oct 16 09:13:35 rocky postgres[3877]: 2025-10-16 09:13:35.663 EDT [3877] LOG:  redirecting log output to logging collector process
Oct 16 09:13:35 rocky postgres[3877]: 2025-10-16 09:13:35.663 EDT [3877] HINT:  Future log output will appear in directory "log".
Oct 16 09:13:35 rocky systemd[1]: Started postgresql-18.service - PostgreSQL 18 database server.

By default, PostgreSQL listens on port 5432. You can check it with the following command:

ss -antpl | grep 5432

You will get the following output:

LISTEN 0      128        127.0.0.1:5432       0.0.0.0:*    users:(("postmaster",pid=8428,fd=7))                                                                                           
LISTEN 0      128            [::1]:5432          [::]:*    users:(("postmaster",pid=8428,fd=6))                                                                                           

Step 3 – Connect to PostgreSQL

There are two ways to connect to the local PostgreSQL instance:

Use the following command to connect to the local PostgreSQL instance directly:

sudo -u postgres psql

You will get the PostgreSQL shell in the following output:

psql (18.0)
Type "help" for help.

postgres=# 

You can also use the following method to connect to the local PostgreSQL instance:

First, switch the user to the PostgreSQL user:

su - postgres

Next, run the psql command to connect to the PostgreSQL shell:

psql

You will get the following output:

psql (18.0)
Type "help" for help.

postgres=# 

Now, run the exit command two times to exit from the PostgreSQL.

Also Read

How to Install and Use pgAdmin on Ubuntu 18.04

Step 4 – Working with PostgreSQL

By default, the Postgres user is configured without any password. For security reasons, it is recommended to set a strong password for the Postgres user.

Run the following command to set a password for the Postgres user:

psql -c "alter user postgres with password 'password'"

To create a database named wpdb, run the following command:

psql
CREATE DATABASE wpdb;

To list all PostgreSQL databases, run the following command:

\l

You will get a list of all PostgreSQL databases in the following output:

                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 wpdb      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 

Step 5 – Configure PostgreSQL for Remote Access

By default, PostgreSQL is configured to listen on localhost, so only local users can connect to the PostgreSQL database. If your application is hosted on a different server then you will need to configure PostgreSQL for remote access.

You can configure it by editing the following file:

nano /var/lib/pgsql/18/data/postgresql.conf

Change the following line with your server IP address:

listen_addresses = 'your-server-ip'

Save and close the file, then edit another file:

nano /var/lib/pgsql/18/data/pg_hba.conf

Add the following line:

# Accept from anywhere
host all all 0.0.0.0/0 md5

Save and close the file, then restart the PostgreSQL service to apply the changes:

systemctl restart postgresql-18

You can now connect to the PostgreSQL server from the remote machine using the following command:

psql -U username -h your-server-ip -p 5432 dbname

Conclusion

In this post, we learned how to install PostgreSQL 18 on Rocky Linux 8. We also learned how to connect and interact with PostgreSQL. Try it on dedicated hosting from Atlantic.Net!