Atlantic.Net Blog

How to Install and Secure PostgreSQL Server on Oracle Linux 8

Hitesh Jethva
by Atlantic.Net (429 posts) under Dedicated Server Hosting, Tutorials
0 Comments

PostgreSQL is an open-source, powerful, and high-performance relational database management system. It is gaining popularity due to its robustness, flexibility, and performance. It is used as a database backend for several web, mobile, geospatial, and analytics applications. At the time of writing this article, PostgreSQL 14 is the latest version. This version comes with significant improvements to the indexing and lookup system that benefit large databases.

In this post, we will show you how to install and secure PostgreSQL on Oracle Linux 8.

Prerequisites

  • A server running Oracle Linux 8 on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Oracle Linux 8 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your server, run the following command to update your base system with the latest available packages.

dnf update -y

Step 2 – Add PostgreSQL 14 Repository

By default, the latest version of PostgreSQL is not included in the Oracle Linux default repository. You can check all available versions in the AppStream repository using the following command:

dnf module list postgresql

You should see all PostgreSQL versions in the following output:

Last metadata expiration check: 0:19:12 ago on Wednesday 22 June 2022 11:49:00 AM EDT.
Oracle Linux 8 Application Stream (x86_64)
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

In order to install the latest PostgreSQL version, you will need to install the PostgreSQL repo to your system.

You can install it 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 repo is created, you can proceed to the next step.

Step 3 – Install PostgreSQL 14 on Oracle Linux 8

First, update your repository using the following command:

dnf update -y

Next, disable the default PostgreSQL repo using the following command:

dnf -qy module disable postgresql

Next, install the latest version of PostgreSQL by running the following command:

dnf install postgresql14 postgresql14-server

Once PostgreSQL 14 is installed, you will get the following output:

Last metadata expiration check: 0:00:53 ago on Wednesday 22 June 2022 12:09:29 PM EDT.
Dependencies resolved.
==============================================================================================================================================
 Package                               Architecture             Version                             Repository                           Size
==============================================================================================================================================
Installing:
 postgresql14                          x86_64                   14.4-1PGDG.rhel8                    pgdg14                              1.5 M
 postgresql14-server                   x86_64                   14.4-1PGDG.rhel8                    pgdg14                              5.7 M
Installing dependencies:
 lz4                                   x86_64                   1.8.3-3.el8_4                       ol8_baseos_latest                   103 k
 postgresql14-libs                     x86_64                   14.4-1PGDG.rhel8                    pgdg14                              276 k

Transaction Summary
==============================================================================================================================================
Install  4 Packages

Total download size: 7.6 M
Installed size: 32 M
Is this ok [y/N]: y

Next, initialize the PostgreSQL database with the following command:

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

Sample output:

Initializing database ... OK

Next, start the PostgreSQL service and enable it to start at system reboot with the following command:

systemctl start postgresql-14
systemctl enable postgresql-14

You can check the status of PostgreSQL with the following command:

systemctl status postgresql-14

You should get the following output:

● postgresql-14.service - PostgreSQL 14 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-14.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2022-06-22 12:11:16 EDT; 7s ago
     Docs: https://www.postgresql.org/docs/14/static/
  Process: 49700 ExecStartPre=/usr/pgsql-14/bin/postgresql-14-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 49705 (postmaster)
    Tasks: 8 (limit: 23694)
   Memory: 16.7M
   CGroup: /system.slice/postgresql-14.service
           ├─49705 /usr/pgsql-14/bin/postmaster -D /var/lib/pgsql/14/data/
           ├─49707 postgres: logger 
           ├─49709 postgres: checkpointer 
           ├─49710 postgres: background writer 
           ├─49711 postgres: walwriter 
           ├─49712 postgres: autovacuum launcher 
           ├─49713 postgres: stats collector 
           └─49714 postgres: logical replication launcher 

Jun 22 12:11:16 oraclelinux8 systemd[1]: Starting PostgreSQL 14 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=49705,fd=7))
LISTEN 0      128            [::1]:5432         [::]:*    users:(("postmaster",pid=49705,fd=6))

Step 4 – Set a Password for Postgres User

By default, the password of the Postgres user is not set, so it is recommended to set a password for security reasons.

To set a password, log in to PostgreSQL with the following command:

su - postgres

Next, set a secure password with the following command:

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

Next, exit from the PostgreSQL shell using the following command:

exit

Step 5 – Change PostgreSQL Authentication Method

By default, PostgreSQL is configured to use the peer method to connect to PostgreSQL locally, but this method is not recommended for the production environment. It is recommended to change the authentication method from peer to scram-sha-256.

You can change it by editing the PostgreSQL main configuration file:

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

Find the following line:

local   all             all                                     peer

And replace it with the following line:

local   all             all                                     scram-sha-256

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

systemctl restart postgresql-14

Step 6 – Create a Database and User in PostgreSQL

First, log in to the PostgreSQL shell with the following command:

sudo -u postgres psql

You will get the following output:

could not change directory to "/root": Permission denied
psql (14.4)
Type "help" for help.

postgres=# 

Next, create a new PostgreSQL user named testuser using the following command:

CREATE USER testuser WITH CREATEDB CREATEROLE PASSWORD 'passoword';

To verify the PostgreSQL users, run:

\du

You will get the following output:

                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 testuser  | Create role, Create DB                                     | {}
                               

To create a new PostgreSQL database named testdb, run:

CREATE DATABASE testdb OWNER testuser;

To verify the PostgreSQL databases, run:

\l

You will get 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
 testdb    | testuser | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)

Conclusion

In this post, we explained how to install and use PostgreSQL on Oracle Linux 8. You can now use PostgreSQL as the primary data store for your web application. For security reasons, it is always recommended to install the latest version of PostgreSQL in the production environment. Try it on dedicated hosting from Atlantic.Net!

Get A Free To Use Cloud VPS

Free Tier Includes:
G3.2GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

How to Install Sails.js Framework with Nginx on Oracle Linux 8
How to Install OTRS on Oracle Linux 8
How to Install and Configure Caddy Web Server with PHP on Oracle Linux 8
How to Install and Use PIP Python Package Manager on Oracle Linux 8
How to Install FTP Server with ProFTPD on Oracle Linux 8

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2008 Lookout Dr,

Dallas, Texas 75044

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources