
PostgreSQL is one of the most powerful, stable, and feature-rich open-source database systems available today. It’s widely used by startups, agencies, and enterprise-level companies for building reliable and scalable applications. If your server or application stack runs on Ubuntu, installing PostgreSQL is straightforward — especially with the latest Ubuntu 24.04 release.
In this guide, we’ll walk you through how to install PostgreSQL on Ubuntu 24.04, how to configure it, create users, manage roles, and connect to your database securely. Whether you're hosting databases for WordPress, Laravel apps, or custom software — this tutorial will help you get PostgreSQL up and running quickly.
Why Choose PostgreSQL?
Before diving into the installation steps, here are a few reasons why PostgreSQL is a favorite among developers:
- Open Source & Free to Use
- Stable & Well-Maintained
- ACID-Compliant Transactions
- Supports Complex Queries & Advanced Data Types
- Extensible with custom functions, plugins and extensions like PostGIS
- Better performance for large datasets compared to MySQL in many cases
If reliability and scalability are important to your project, PostgreSQL is an excellent choice.
Prerequisites
Before starting, ensure you have:
- A system running Ubuntu 24.04
- A non-root user with sudo privileges
- Access to the terminal or SSH
If you're using a cloud server or VPS, you can connect using:
ssh username@your_server_ip
Step 1: Update Your Server Packages
It’s always a good idea to update your system packages before installing any new software:
sudo apt update && sudo apt upgrade -y
This ensures that your PostgreSQL installation uses the latest stable dependencies.
Step 2: Install PostgreSQL
Ubuntu 24.04 comes with PostgreSQL available directly in its default repositories, which makes installation simple:
sudo apt install postgresql postgresql-contrib -y
postgresqlinstalls the core PostgreSQL database server.postgresql-contribinstalls useful additional tools and extensions.
Once installed, PostgreSQL should automatically start. You can verify this by running:
sudo systemctl status postgresql
You should see output showing that PostgreSQL is active (running).
If, for any reason, it’s not running, start it manually:
sudo systemctl start postgresql
Step 3: Using the PostgreSQL Command Line
PostgreSQL creates a user and group called postgres automatically. This is the default database superuser.
Switch to the postgres user:
sudo -i -u postgres
Now open the PostgreSQL command prompt:
psql
You should see a prompt like:
postgres=#
This means you’re inside the PostgreSQL shell.
To exit:
\q
And return to your normal user:
exit
Step 4: Create a New PostgreSQL Database and User
For security reasons, it’s recommended not to use the default postgres user for application access. Let’s create our own database and user.
Switch to the PostgreSQL user:
sudo -i -u postgres
Open the PostgreSQL shell:
psql
Create a new user (replace myuser with your username):
CREATE USER myuser WITH PASSWORD 'mypassword';
Now create a new database (replace mydb with your database name):
CREATE DATABASE mydb OWNER myuser;
Give your user necessary privileges:
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Exit the PostgreSQL shell:
\q
Exit back to normal user:
exit
Step 5: Allow Remote Access (Optional)
If your application is on another server or you need remote access:
Open PostgreSQL configuration file:
sudo nano /etc/postgresql/*/main/postgresql.conf
Find this line and change:
#listen_addresses = 'localhost'
To:
listen_addresses = '*'
Now edit the authentication file:
sudo nano /etc/postgresql/*/main/pg_hba.conf
Add this line at the bottom (replace 0.0.0.0/0 with your allowed IP, if needed):
host all all 0.0.0.0/0 md5
Restart PostgreSQL:
sudo systemctl restart postgresql
Step 6: Connecting to PostgreSQL
To connect to your new database:
psql -U myuser -d mydb
If connecting remotely:
psql -h your_server_ip -U myuser -d mydb
Step 7: Enable PostgreSQL to Start at Boot
sudo systemctl enable postgresql
This ensures PostgreSQL starts automatically when the server restarts.
Step 8: Secure Your Installation
- Use strong passwords.
- Limit remote access only to trusted IP addresses.
- Regularly update PostgreSQL:
sudo apt update && sudo apt upgrade
Conclusion
You’ve successfully installed and configured PostgreSQL on Ubuntu 24.04!
Whether you're building a new application or migrating from a different database system, PostgreSQL provides the stability, speed, and flexibility needed for modern data-driven applications.
If you're hosting your applications on Hostomy, our optimized VPS Hosting Service are fully compatible with PostgreSQL — giving you faster database performance, low latency, and 24/7 support.
