How to install PostgreSQL on Ubuntu 24.04?
sudo apt update followed by sudo apt install postgresql postgresql-contrib. This installs PostgreSQL 15 (Ubuntu’s default version) in under a minute. For the latest PostgreSQL 17, add the official PostgreSQL APT repository first (see Option 2 below). After installing, start the service with sudo systemctl start postgresql and verify with psql --version.Introduction
If you’re a developer, data analyst, or sysadmin working on Ubuntu 24.04, chances are you’re here to install PostgreSQL – and you’re in the right place. PostgreSQL (or Postgres) is a high-performance, highly extensible, open-source relational database trusted by startups and Fortune 500 companies alike.
This guide covers installing PostgreSQL on Ubuntu the right way – whether you want the stable version from Ubuntu’s default repository or the latest PostgreSQL 17 release. We’ll also show you how to install the psql client, verify your PostgreSQL Ubuntu install, secure it, and configure remote access.
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. If you’re new to the concept of server and database, see our article on Difference between Server and Database.
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.
Note: If you’re using Ubuntu 22.04, see our article on Install PostgreSQL on Ubuntu 22.04.
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
Verify PostgreSQL Installation and Service Status
Once PostgreSQL has been installed on Ubuntu 24.04, it is useful to confirm that the installation completed successfully and the service can be started without any issues.
Check PostgreSQL service status
sudo systemctl status postgresql
Access PostgreSQL shell
sudo -i -u postgres psql
List all databases
\l
Check if PostgreSQL is listening on default port (5432)
sudo netstat -plntu | grep 5432
Secure PostgreSQL
Securing your PostgreSQL installation is crucial. PostgreSQL uses the pg_hba. conf file to manage authentication.
Authentication methods you can use
- peer – Local connections based on OS user accounts
- md5 – Password-based authentication
- scram-sha-256 – More secure password-based authentication
Edit the Configuration File
sudo nano /etc/postgresql/17/main/pg_hba.conf
After making changes, restart PostgreSQL to apply them:
sudo systemctl restart postgresql
Note: Always use strong passwords for PostgreSQL users and restrict remote access where not needed.
After verifying and securing your PostgreSQL setup, you can use it to run applications like SonarQube on Ubuntu 22.04 LTS, which relies on PostgreSQL to store project data.
Resolve Common Installation Issues
Even with a successful installation, you might run into some common errors. Let’s go through how to resolve them:
psql: could not connect to server
- Check service status:
sudo systemctl status postgresql
- Ensure PostgreSQL is listening on the correct port (5432)
Permission denied or Authentication error
- Verify user roles and authentication method in pg_hba.conf
- Reset passwords if needed:
ALTER USER postgres WITH PASSWORD 'newpassword';
Package conflicts or Missing dependencies
- Update apt repository and reinstall:
sudo apt update sudo apt install postgresql postgresql-contrib
These steps will help you to fix the most common issues ubuntu users face while installing PostgreSQL.
(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.
Conclusion
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! If you want to host your applications, Cantech’s Cloud Hosting for Ecommerce offers a secure and scalable backend.
Got questions? Drop them in the comments. And, if you find this guide useful, don’t forget to share it with other developers!
FAQs
What’s the default port for PostgreSQL?
PostgreSQL listens on port 5432 by default on Ubuntu 24.04 (and on all major operating systems). You can confirm this by running sudo netstat -plntu | grep 5432 after installing PostgreSQL.
How do I remove PostgreSQL?
To completely remove PostgreSQL from Ubuntu 24.04, run sudo apt remove --purge postgresql* followed by sudo apt autoremove. This removes PostgreSQL packages and configuration files, whether you installed PostgreSQL 15 (Ubuntu’s default) or PostgreSQL 17 (from the official PostgreSQL repository).
How do I create a new PostgreSQL database?
After installing PostgreSQL on Ubuntu 24.04, create a new database by running sudo -u postgres createdb mydatabase, replacing mydatabase with your desired database name. This works the same way whether you’re running PostgreSQL 15 or PostgreSQL 17.
Can I run multiple PostgreSQL versions on Ubuntu 24.04?
Yes. On Ubuntu 24.04, you can run PostgreSQL 15 (the default repository version) and PostgreSQL 17 (from the official PostgreSQL repository) side-by-side, as long as each version uses a separate port and data directory. Ubuntu’s package manager handles this automatically when both are installed via apt.
What version of PostgreSQL comes with Ubuntu 24.04 by default?
Ubuntu 24.04 LTS ships PostgreSQL 15 in its default repository. To install the newer PostgreSQL 17 instead, add the official PostgreSQL APT repository and run sudo apt install postgresql-17.
How do I enable remote access to PostgreSQL in Ubuntu 24.04?
- Edit
postgresql.confto setlisten_addresses = '*' - Update
pg_hba.confto allow connections from your desired IP ranges - Restart PostgreSQL and allow port 5432 through the firewall
On Ubuntu 24.04, these config files are located at /etc/postgresql/<version>/main/ (e.g., /etc/postgresql/17/main/ for PostgreSQL 17).