---
title: How to Install PostgreSQL on Ubuntu 24.04
url: https://docs.vultr.com/how-to-install-postgresql-on-ubuntu-24-04
description: Learn how to install PostgreSQL on Ubuntu 24.04 with our step-by-step guide. Includes setup, configuration, and troubleshooting tips for beginners and advanced users.
publish_date: 2024-06-12T23:27:22.986666Z
last_updated: 2026-09-09T23:28:56.545768Z
author: Bagaskara Guruh T
header_image: https://docs.vultr.com/public/doc-assets/1821/6cf6904b6f5cda1c.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## Introduction

PostgreSQL is an open-source advanced Relational Database Management System (RDBMS) designed to handle a wide range of data management tasks. It supports the Structured Query Language (SQL) to manage data in small and large enterprise applications such as analytical systems, Geographic Information Systems (GIS), healthcare applications, and dynamic web applications.

This article explains how to install PostgreSQL on a Ubuntu 24.04 server. You will enable the PostgreSQL database server and secure it for production use on your server.

## Prerequisites

Before you begin:

* [Deploy a Ubuntu 24.04 server instance](https://www.vultr.com/servers/ubuntu/) on Vultr.
* Access the server [using SSH](https://www.vultr.com/docs/how-to-use-ssh-with-vultr-servers/).
* Create a [non-root user with sudo privileges and switch to the user](https://www.vultr.com/docs/how-to-use-sudo-on-a-vultr-cloud-server/#create-a-sudo-user-on-debian-&-ubuntu).
* [Update the server](https://docs.vultr.com/how-to-update-a-vultr-cloud-server#how-to-update-debian-&-ubuntu).

## Install PostgreSQL

PostgreSQL is available in the default APT repositories on Ubuntu. Follow the steps below to install the PostgreSQL database server packages and enable the application to start at boot time.

1. Install the `postgresql-common` dependency package on your server.

    ```console
    $ sudo apt install -y postgresql-common -y
    ```

1. Run the following command to execute the PostgreSQL APT repository script.

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

    Press :key_enter: when prompted to add the new repository to your server sources.

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

    Press Enter to continue, or Ctrl-C to abort.
    ```

1. Install the `postgresql` database server package.

    ```console
    $ sudo apt install -y postgresql
    ```

1. Start the PostgreSQL database server.

    ```console
    $ sudo systemctl restart postgresql
    ```

1. View the PostgreSQL system service status and verify that it's active.

    ```console
    $ sudo systemctl status postgresql
    ```

    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 runs with the default `postgres` privileged database user account on your server. Follow the steps below to enable password authentication and secure the PostgreSQL database server to grant only authorized users access to databases.

1. Log in to the PostgreSQL database server using the `postgres` user account.

    ```console
    $ sudo -u postgres psql
    ```

1. Modify the default `postgres` user with a new strong password.

    ```sql
    postgres=#  ALTER USER postgres WITH ENCRYPTED PASSWORD 'strong_password';
    ```

1. Create a new user `db_manager` with a new strong password.

    ```sql
    postgres=#  CREATE USER db_manager ENCRYPTED PASSWORD 'strong_password';
    ```

1. Exit the PostgreSQL console.

    ```sql
    postgres=#  \q
    ```

1. 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` to enable password authentication on the server.

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

1. Restart the PostgreSQL server to apply the new configuration changes.

    ```console
    $ sudo systemctl restart postgresql
    ```

## Access the PostgreSQL Database Server

You can access the PostgreSQL database console using the `psql` utility that is pre-installed with the server package. In addition, you can access the database server using compatible graphical tools that create a direct connection to the console. Follow the steps below to access your PostgreSQL database server console and create a new sample database to use with your non-privileged user.

1. Create a new sample PostgreSQL database `hospital` and grant the `db_manager` user ownership privileges to the database.

    ```console
    $ sudo -u postgres createdb hospital -O db_manager
    ```

    When prompted, enter the Postgres user password you created earlier.

1. Log in to the PostgreSQL database as the user `db_manager` to test access to the `hospital` database.

    ```console
    $ sudo -u postgres psql -U db_manager -d hospital
    ```

    Enter the database user password when prompted and press :key_enter: to access the database.

1. Create a new sample `doctors` table.

    ```sql
    psql > CREATE TABLE doctors (
               doctor_id SERIAL PRIMARY KEY,
               first_name VARCHAR(50),
               last_name VARCHAR(50),
               appointment_date DATE
           );
    ```

    The above SQL statement creates a new table in the `hospital` database with the following columns:

    * `doctor_id` is a `PRIMARY KEY` that uniquely identifies each doctor in the `doctors` table.
    * `first_name` and `last_name` store names in the `doctors` table.
    * `appointment_date` stores the doctor's appointment date with a patient in the hospital.
    * `SERIAL` generates a new `doctor_id` for each new record.

1. Insert sample data into the `doctors` table.

    ```sql
    psql > 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');
    ```

1. Query the `doctors` table to view all available records.

    ```sql
    psql > SELECT * FROM doctors;
    ```

    Output:

    ```sql
    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)
    ```

1. Exit the PostgreSQL console.

    ```sql
    psql > \q
    ```

## Conclusion

You have installed PostgreSQL on your Ubuntu 24.04 server and accessed the database server console using the `psql` utility to create sample databases and table records. You can further integrate the PostgreSQL database server with your existing applications to securely enable the creation and management of application database records. For more information and configuration options, visit the official [PostgreSQL](https://www.postgresql.org/docs) documentation.
