๐ 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