Tutorials  /  Databases

Install and Secure PostgreSQL on Ubuntu 22.04

Ccentron Redaktion · October 2025 ·5 min read ·Databases, Tutorial

PostgreSQL is a powerful open-source relational database management system (RDBMS) designed to handle a broad range of data operations. It supports SQL and is suitable for small projects as well as large-scale applications such as analytics, GIS, healthcare solutions, and dynamic web platforms.

This guide explains how to install PostgreSQL on an Ubuntu 22.04 server and secure it for production use.

Prerequisites

Before starting, ensure the following:

  • An Ubuntu 22.04 server is available.
  • You can access the server via SSH as a non-root user with sudo privileges.
  • Your server is updated with the latest packages.
DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

Install PostgreSQL

PostgreSQL is included in Ubuntu’s default APT repositories. Follow these steps to install PostgreSQL and configure it to start automatically at boot.

Update the Server Package Index

Console
$ sudo apt update

Install the postgresql-common Dependency

Console
$ sudo apt install -y postgresql-common -y

Run the PostgreSQL APT Repository Script

Console
$ sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh

When prompted, press Enter to add the new repository to your server sources.

This script enables the PostgreSQL APT repository (apt.postgresql.org) on your system. The distribution codename used will be noble-pgdg.

Press Enter to continue, or use Ctrl-C to cancel.

Install the PostgreSQL Database Server

Console
$ sudo apt install -y postgresql

Start the PostgreSQL Service

Console
$ sudo systemctl start postgresql

Verify the PostgreSQL Service Status

Console
$ sudo systemctl status postgresql

Example output:

● postgresql.service - PostgreSQL RDBMS
    Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
    Active: active (exited) since Mon 2024-05-27 16:09:21 UTC; 35s ago
    Process: 5601 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 5601 (code=exited, status=0/SUCCESS)
        CPU: 3ms

Secure the PostgreSQL Database Server

PostgreSQL uses the default postgres user account. Follow these steps to enable password authentication and secure the server by restricting access to authorized users only.

Check the Installed PostgreSQL Version

Console
$ psql --version

Example output:

psql (PostgreSQL) 17.4 (Ubuntu 17.4-1.pgdg22.04+2)

Log In as the postgres User

Console
$ sudo -u postgres psql

Set a Strong Password for the postgres User

Code
postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'strong_password';

Create a New User with a Secure Password

Code
postgres=# CREATE USER db_manager ENCRYPTED PASSWORD 'strong_password';

Exit the PostgreSQL Console

Code
postgres=# quit;

Enable Password Authentication

Run the following command to change the default peer value in the scram-sha-256 field in the main PostgreSQL configuration file pg_hba.conf. This will enable password authentication on the server.

Console
$ sudo sed -i '/^local/s/peer/scram-sha-256/' /etc/postgresql/17/main/pg_hba.conf

Replace 17 with your installed PostgreSQL version number if it differs.

Restart PostgreSQL to Apply Changes

Console
$ sudo systemctl restart postgresql

Access the PostgreSQL Database Server

You can access the PostgreSQL console using the built-in psql utility or graphical tools for direct connections. The steps below show how to create a new sample database and assign it to a non-privileged user.

Create a Sample Database

Create a new PostgreSQL database named hospital and grant ownership to the db_manager user.

Console
$ sudo -u postgres createdb hospital -O db_manager

When prompted, provide the password for the Postgres user you configured earlier.

Log In to the Database as db_manager

Use the following command to test access to the hospital database as the db_manager user:

Console
$ sudo -u postgres psql -U db_manager -d hospital

Enter the password for the database user when prompted and press Enter to gain access.

Create a doctors Table

Inside the database, create a table named doctors:

JavaScript
hospital=> CREATE TABLE doctors (
           doctor_id SERIAL PRIMARY KEY,
           first_name VARCHAR(50),
           last_name VARCHAR(50),
           appointment_date DATE
       );

This SQL statement creates a table with the following columns:

  • doctor_id as a primary key to uniquely identify each doctor.
  • first_name and last_name for storing names.
  • appointment_date for storing appointment dates.
  • SERIAL automatically generates a unique ID for each record.

Insert Sample Data

Insert a few sample records into the doctors table:

JavaScript
hospital=> INSERT INTO doctors
       ( first_name, last_name, appointment_date)
       VALUES
       ( 'Ben', 'Joe', '2024-11-15'),
       ( 'Carson', 'Smith', '2024-02-28'),
       ( 'Donald', 'James', '2024-04-10');

Query the doctors Table

Retrieve all records from the table:

JavaScript
hospital=> SELECT * FROM doctors;

Example output:

doctor_id | first_name | last_name | appointment_date
-----------+------------+-----------+------------------
       1 | Ben        | Joe       | 2024-11-15
       2 | Carson     | Smith     | 2024-02-28
       3 | Donald     | James     | 2024-04-10
(3 rows)

Exit the PostgreSQL Console

JavaScript
hospital=> \q

Conclusion

In this article, you installed PostgreSQL on Ubuntu 22.04, configured it for security, and used the psql utility to create a database and tables. PostgreSQL is now ready to be integrated with your applications for secure and efficient database management. For further details, refer to the official PostgreSQL documentation.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren