How to install PostgreSQL on Ubuntu 24.04

How to install PostgreSQL on Ubuntu 24.04

PostgreSQL is a free, open-source object-relational database that focuses on reliability and performance, offering robust features for users of all skill levels, from beginners to professionals. It’s a popular and widely used database system for various use cases, characterized by high scalability and a strong focus on security features. It works on most Linux Operating systems, and in this tutorial, we will show you how you can install PostgreSQL on Ubuntu 24.04 Operating System.

Prerequisites

  • A server with Ubuntu 24.04 as OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

You can first update the system packages to the latest versions available.

# apt-get update -y && sudo apt-get upgrade -y

Step 2. Install PostgreSQL

To start the installation, you will first need to install the postgresql-common package that contains the dependencies for installing PostgreSQL by running the following command:

# apt install postgresql-common

Then you can run this command to automatically install the PostgreSQL repository for apt.

# /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh

You should receive this output:

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

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

If you want to manually install the APT repository, you can follow these steps, or if you installed the repository automatically with the command:

# /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh

You can skip to the command below: apt install postgresql -y

To manually install the APT repository, you will first need to install the needed packages and import the repository signing key:

# apt install curl ca-certificates
# install -d /usr/share/postgresql-common/pgdg
# curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc

Then you can create the repository configuration file

# . /etc/os-release
# sudo sh -c "echo 'deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $VERSION_CODENAME-pgdg main' > /etc/apt/sources.list.d/pgdg.list"

If you have installed the repository automatically, you can skip to this step, and now update the packages with:

# apt update

You can click Enter, and finally install PostgreSQL with the command:

# apt install postgresql -y

Now, the PostgreSQL server should be installed, and you can check if PostgreSQL is running:

# systemctl status postgresql

● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
     Active: active (exited) since Fri 2025-11-07 10:09:46 EST; 42s ago
 Invocation: 3094ffa465f342689eec38258d8ffec3
    Process: 2826 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 2826 (code=exited, status=0/SUCCESS)
   Mem peak: 1.8M
        CPU: 7ms

Nov 07 10:09:46 rosehosting systemd[1]: Starting postgresql.service - PostgreSQL RDBMS...
Nov 07 10:09:46 rosehosting systemd[1]: Finished postgresql.service - PostgreSQL RDBMS.

And to check the PostgreSQL version, you can run:

# psql --version
psql (PostgreSQL) 18.0 (Debian 18.0-1.pgdg13+3)

Step 3. Using PostgreSQL

Now you can log in to PostgreSQL by switching to the postgres user from the command line with:

# su – postgres

Then you can run the command psql and get this output, signaling you are logged in the PostgreSQL console, or by running only one command:

# sudo -u postgres psql
psql (18.0 (Debian 18.0-1.pgdg13+3))
Type "help" for help.

postgres=#

If you want to check the available databases in PostgreSQL, you can do so by typing \l in the PostgreSQL console.
Here are also some commands you may find useful for PostgreSQL

  • \c dbname: Connects to the specified database.
  • \dt: Lists all tables in the current database.
  • \d table_name: Describes the structure of a specified table.
  • \du: Lists all database users (roles).
  • \q: Exit the psql client.

Conclusion

If you have followed the instructions, you should have PostgreSQL installed on Ubuntu 24.04 and be ready to use it. You can still configure certain parameters to secure your database system or optimize performance for specific server resources. If you need help on how to install PostgreSQL on Ubuntu 24.04, or how to secure and configure PostgreSQL for the best performance, you can check our PostgreSQL hosting plans and get started immediately.

Leave a Comment