Cantech Knowledge Base

Your Go-To Hosting Resource

How to Install PostgreSQL on Ubuntu 24.04?

If you are a developer, data analyst, or database geek using Ubuntu 24.04, you have probably considered installing PostgreSQL. PostgreSQL (or Postgres) is an awesome, high-performance, highly extensible, and one of the most trusted open-source relational database systems today.

This easy step-by-step guide will show you how to install PostgreSQL on Ubuntu 24.04. You will learn how to install the stable version of PostgreSQL using the official Ubuntu repository or PostgreSQL repository.

What is PostgreSQL?

A powerful, open-source object-relational database package. It offers querying via both SQL (relational) and JSON (non-relational), which means it can be used for any type of application. PostgreSQL is used worldwide by startups and Fortune 500 companies.

Prerequisites

Before you begin the installation, make sure:

  • You have a system running Ubuntu 24.04 LTS.
  • You have sudo privileges or access to a user with root permissions.
  • Your system is connected to the internet to fetch packages.

Option 1: Install PostgreSQL from Ubuntu’s Default Repository

It is the quick and easy method to install PostgreSQL on Ubuntu 24.04. It might not get you the latest version, but it’s stable and tested for your OS.

Steps:

1. Update the system packages:

sudo apt update

2. Install PostgreSQL:

sudo apt install postgresql postgresql-contrib

3. Check the PostgreSQL version installed:

psql --version

4. Start and enable the PostgreSQL service:

sudo systemctl start postgresql
sudo systemctl enable postgresql

5. Switch to the default postgres user and open psql shell:

sudo -u postgres psql

6. Set a secure password for the postgres user:

ALTER USER postgres PASSWORD 'your_secure_password';

7. Exit the PostgreSQL prompt:

\q

That’s it! PostgreSQL is now installed and ready to use.

Option 2: Install PostgreSQL 17 from the Official PostgreSQL Repository

Want the latest and greatest? PostgreSQL 17 is available via the official PostgreSQL Apt repository.
Steps:

1. Install prerequisites:

sudo apt install curl ca-certificates

2. Import the PostgreSQL GPG key:

curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg

3. Add the PostgreSQL repository:

echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list

4. Update the package list again:

sudo apt update

5. Install PostgreSQL 17:

sudo apt install postgresql-17

6. Start and enable PostgreSQL:

sudo systemctl start postgresql
sudo systemctl enable postgresql

7. Verify installation:

psql --version

8. Access and configure PostgreSQL:

sudo -u postgres psql
ALTER USER postgres PASSWORD 'your_secure_password';
\q

(Optional) Configure PostgreSQL for Remote Access

By default, PostgreSQL only listens to localhost. So, if you are building a multi-user system or a remote database, you must allow external users to connect.

Steps:

1. Edit the PostgreSQL configuration file:

sudo nano /etc/postgresql/17/main/postgresql.conf

Uncomment and change:

listen_addresses = '*'

2. Allow remote access in the pg_hba.conf file:

sudo nano /etc/postgresql/17/main/pg_hba.conf

Add this line:

host    all     all     0.0.0.0/0     md5

3. Restart PostgreSQL to apply changes:

sudo systemctl restart postgresql

4. Open the PostgreSQL port in your firewall (default is 5432):

sudo ufw allow 5432/tcp

Bonus Tips

  • Use pgAdmin for a GUI-based PostgreSQL interface.
  • Enable SSL in production environments for secure access.
  • Always use strong passwords and role-based access control.

Wrapping Up

Now, you know exactly how to install PostgreSQL on Ubuntu 24.04 — the easy and advanced ways. Whether you’re setting up a development machine, configuring a staging server, or launching into production, PostgreSQL’s performance and reliability make it a top choice!

Got questions? Drop them in the comments. And, if you find this guide useful, don’t forget to share it with other developers!

FAQs

Q1: What’s the default port for PostgreSQL?

PostgreSQL is listening on port 5432 by default.

Q2: How do I remove PostgreSQL?

Run:

sudo apt remove --purge postgresql*
sudo apt autoremove

Q3: How do I create a new PostgreSQL database?

sudo -u postgres createdb mydatabase

Q4: Can I run multiple PostgreSQL versions on Ubuntu 24.04?

Yes, with proper port and data directory configuration, you can run multiple versions side-by-side.

Q5: What version of PostgreSQL comes with Ubuntu 24.04 by default?

As of the Ubuntu 24.04 release, the default repository has PostgreSQL 15 included by default, but you can install PostgreSQL 17 from the official PostgreSQL Apt repository.

Q6: How do I enable remote access to PostgreSQL in Ubuntu 24.04?

  • Edit the postgresql.conf file to set listen_addresses = ‘*’
  • Update pg_hba.conf to allow connections from desired IP ranges
  • Restart the PostgreSQL service and allow port 5432 in the firewall.
May 22, 2025