How to Install PostgreSQL on Ubuntu 24.04

PostgreSQL is an advanced, open-source, feature-rich relational database widely used across the globe. PostgreSQL is compatible with almost all major operating systems, including Ubuntu. Ubuntu 24.04 (codenamed Nobal Numbat) is the latest LTS Ubuntu version, released in April 2024. We can install PostgreSQL on Ubuntu 24.04 for a reliable and robust database management system.

In today’s guide, we’ll show you a step-by-step process of installing, using, and uninstalling PostgreSQL from Ubuntu 24.04.

How to Install PostgreSQL(psql) on Ubuntu 24.04?

Go through the following stepwise instructions to install Postgres on Ubuntu 24.04:

  • Update System Packages
  • Install Postgres
  • Start Postgres Service
  • Use Postgres on Ubuntu 24.04

Let’s start the practical implementation of Postgres on Ubuntu 24.04:

Step 1: Update System Packages

Let’s use the below-given command to update the system packages before installing Postgres:

sudo   apt update

img

Step 2: Install Postgres

After Updating the system packages, use the below command to install Postgres and its contrib package on Ubuntu 24.04:

sudo apt install postgresql postgresql-contrib -y

img

Step 3: Start Postgres Service

After installing Postgres, execute the given command to start the Postgres service via the configurations specified in the “postgresql.service” file:

sudo systemctl start postgresql.service

img

Step 4: Use Postgres on Ubuntu 24.04

After starting the Postgres service, we are all set to use it and perform different database operations. But before that, we need to switch to the “postgres” account, for this purpose, use the command:

sudo -u postgres psql

The following output depicts that we have successfully switched to PostgreSQL’s default user “postgres”:

img

Alternatively, we can execute the “sudo -i” command to switch to the “postgres” user and then “psql” command to access the SQL Shell, like this:

sudo -i -u postgres
psql

img

Now we can perform any database operation like creating a new user, switching a user, creating a new database, table, etc. For instance, in the below code snippet, we execute the CREATE USER command to create a new user/role:

CREATE USER cpUser WITH LOGIN PASSWORD 'asdf';

A new user named “cpUser” with the specified login privileges has been created successfully:

img

This way, we can execute any SQL query to accomplish a specific database operation.

How to Uninstall/Remove Postgres From Ubuntu 24.04?

We can use the following apt command with the “autoremove” option to uninstall Postgres along with its related packages from Ubuntu 24.04:

sudo apt autoremove postgresql postgresql-*

img

We can run the following rm command to forcefully remove the complete “/etc/postgresql/” directory along with all its content:

sudo rm -rf /etc/postgresql/

img

This is how we can install or uninstall Postgres to/from Ubuntu 24.04.

Final Thoughts

To install Postgres on Ubuntu 24.04, first update the system packages and then use the “sudo apt install postgresql postgresql-contrib -y” command. This will install Postgres along with the contrib package on Ubuntu 24.04. Once Postgres is installed on your system, start the Postgres service, access the SQL Shell, and then you can execute any SQL query to perform a database operation. This tutorial demonstrated a step-by-step guide on installing, using, and uninstalling PostgreSQL on Ubuntu 24.04.