🐘 Whether you are a beginner, student, or curious dev, this step-by-step guide will walk you through not only installing but also running PostgreSQL successfully on Ubuntu 24.04
🧠 Why PostgreSQL?
PostgreSQL is one of the most powerful, open-source relational databases on the planet. It's trusted by large-scale apps, startups, and hobby projects
📌 What You’ll Need
- Ubuntu 24.04 LTS (local, cloud, or WSL) or any Ubuntu version above 20.0x
- Terminal access
- An internet connection
🧾 Step 1: Check Your System
Let’s confirm your Ubuntu version first:
lsb_release -a
uname -a
on the above commands, lsb_release -a
is the release Version, and uname -a
is the Kernel Version
🔄 Step 2: Update Your System
Update your system to make sure everything is fresh:
sudo apt update && sudo apt upgrade -y
🐘 Step 3: Install PostgreSQL
Now install PostgreSQL and the helper tools:
sudo apt install postgresql postgresql-contrib -y
from your end you will see alot of words and a progress bar dont be worried is the installation taking place, as for me i have it already installed thus the response
Step 4: Confirm installation Success.
Now we run a version check to confirm that the installation was successful
which psql
this command will show the psql location in the system
. Expected out is /usr/bin/psql
, well, if you managed to get here, then you are a few steps from running SQL commands.
/usr/bin/psql
As the image below
👤 Step 5: Switch to the postgres User
PostgreSQL creates its user named postgres. Switch into it:
sudo -i -u postgres
To note:
sudo
- Run the command with superuser (root) privileges
-i
- Run interactive shell for the user
-u postgres
- Run the command as the postgres user
You’ll now be inside a shell session as the postgres user.
💬 Step 6: Enter the PostgreSQL Shell
Let’s open the PostgreSQL command-line tool:
psql
Try some quick commands:
\conninfo -- See connection info
\l -- List databases
\q -- Quit
🧑💻 Step 7: Create Your Own Database and User
While inside the postgres user:
CREATE USER user_lux WITH PASSWORD 'super_secure_password';
CREATE DATABASE lux_db OWNER user_lux;
🛠️ Step 8: Enable PostgreSQL to Start on Boot
Make sure PostgreSQL is running and will auto-start:
sudo systemctl enable postgresql
sudo systemctl start postgresql
Check the status:
sudo systemctl status postgresql
🌐 (Optional) Step 9: Allow Remote Access
This step is optional and for those connecting remotely (e.g., from DBeaver or DataGrip (as I do)).
sudo nano /etc/postgresql/16/main/pg_hba.conf
sudo nano /etc/postgresql/16/main/postgresql.conf
Set:
-
listen_addresses = '*'
on thepostgresql.conf
file - Add host-based rules
host all all 0.0.0.0/0 md5
topg_hba.conf
file
Then restart:
sudo systemctl restart postgresql
sudo nano /etc/postgresql/16/main/pg_hba.conf
modifiy this variable to this listen_addresses = '*'
and press ctrl+x
and on prompt press y
and enter
to save the file
sudo nano /etc/postgresql/16/main/postgresql.conf
host all all 0.0.0.0/0 md5
add this at the end of the file and press ctrl+x
and on prompt press y
and enter
to save the file
✅ You're Done!
You’ve installed PostgreSQL, created your first user and database, and even set up remote access.
📘 Resources
If this guide helped you, don’t forget to ❤️, Share, and Comment (🗨️ ) below
Top comments (1)
Super helpful