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. 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 is listening on port 5432 by default.
How do I remove PostgreSQL?
Run:
sudo apt remove --purge postgresql* sudo apt autoremove
How do I create a new PostgreSQL database?
sudo -u postgres createdb mydatabase
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.
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.
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.