DEV Community

Cover image for Ubuntu 24.04 + PostgreSQL: A Friendly Setup Guide for Students & New Devs
Aineah Simiyu
Aineah Simiyu

Posted on

Ubuntu 24.04 + PostgreSQL: A Friendly Setup Guide for Students & New Devs

๐Ÿ˜ 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
Enter fullscreen mode Exit fullscreen mode

on the above commands, lsb_release -a is the release Version, and uname -a is the Kernel Version

System Version

๐Ÿ”„ Step 2: Update Your System

Update your system to make sure everything is fresh:

sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Updating and Upgrading the system

๐Ÿ˜ Step 3: Install PostgreSQL

Now install PostgreSQL and the helper tools:

sudo apt install postgresql postgresql-contrib -y
Enter fullscreen mode Exit fullscreen mode

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

Installed Postgresql

Step 4: Confirm installation Success.

Now we run a version check to confirm that the installation was successful

which psql
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

As the image below

Psql, Postgres version check

๐Ÿ‘ค Step 5: Switch to the postgres User

PostgreSQL creates its user named postgres. Switch into it:

sudo -i -u postgres
Enter fullscreen mode Exit fullscreen mode

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.

Postgres User Shell Session

๐Ÿ’ฌ Step 6: Enter the PostgreSQL Shell

Letโ€™s open the PostgreSQL command-line tool:

psql
Enter fullscreen mode Exit fullscreen mode

Try some quick commands:

\conninfo    -- See connection info  
\l           -- List databases  
\q           -- Quit 
Enter fullscreen mode Exit fullscreen mode

Postgres SQL Shell

List of Databases

Connection info

๐Ÿง‘โ€๐Ÿ’ป 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;
Enter fullscreen mode Exit fullscreen mode

Create User & Create DB

๐Ÿ› ๏ธ 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
Enter fullscreen mode Exit fullscreen mode

Check the status:

sudo systemctl status postgresql
Enter fullscreen mode Exit fullscreen mode

Enable Startup and Checking postgres status

๐ŸŒ (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
Enter fullscreen mode Exit fullscreen mode

Set:

  • listen_addresses = '*' on the postgresql.conf file
  • Add host-based rules host all all 0.0.0.0/0 md5to pg_hba.conf file

Then restart:

sudo systemctl restart postgresql
Enter fullscreen mode Exit fullscreen mode

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

Allowing Inbound Connections

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

Allow Connection replication


โœ… 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)

Collapse
 
aineahsimiyu profile image
Aineah Simiyu

Super helpful