PostgreSQL is an open-source relational database system with object-oriented features, widely used to store and manage data for web and enterprise apps.
This guide covers installing PostgreSQL on Ubuntu 24.04, enabling the service, logging in with psql, creating users and databases, and removing it.
Quick Answer
Run sudo apt install postgresql postgresql-contrib to install PostgreSQL, then sudo systemctl start postgresql.service to start the database service.
How to Install PostgreSQL on Ubuntu 24.04
Step 1: Update System Packages
Open Terminal with CTRL+Alt+T and update the local package index. This ensures you install the latest available version of PostgreSQL from the repository.
sudo apt update && sudo apt upgrade

Step 2: Install PostgreSQL
Install PostgreSQL with the postgresql-contrib package for extra utilities and extensions. The -y flag automatically accepts all installation prompts.
sudo apt install postgresql -y postgresql-contrib

To install PostgreSQL without the additional extensions, run sudo apt install postgresql. This installs only the core database server package.
sudo apt install postgresql
Step 3: Check PostgreSQL Status
After installation, check that the PostgreSQL service is active. A green active (running) status confirms the database is ready to accept connections.
sudo service postgresql status
You can also check the service status using systemctl. Both commands return the same service state information for the PostgreSQL database.
sudo systemctl status postgresql
Step 4: Enable and Start PostgreSQL
If PostgreSQL is not running, enable the service first so it starts automatically on boot, then start it with the commands below.
sudo systemctl enable postgresql.service

sudo systemctl start postgresql.service

How to Log In to PostgreSQL on Ubuntu 24.04
PostgreSQL installation creates a default postgres user that has full access to the database. Use this account to log in and configure the server.
Step 1: Switch to the postgres Account
Switch to the postgres system account with sudo -i -u postgres. Replace postgres with another username to log in as a different database user.
sudo -i -u postgres

Step 2: Open the psql Shell
Run psql to open the PostgreSQL interactive shell. The prompt changes to postgres=# to indicate you are in the database command-line interface.
psql

Alternatively, access the PostgreSQL shell without switching accounts by combining the sudo -u postgres and psql commands in a single step.
sudo -u postgres psql
Step 3: Set a Password for the postgres User
The default postgres user has no password set. Run \password postgres inside the psql shell to set a password. Enter and confirm the new password.
\password postgres

Step 4: Exit PostgreSQL
Run \q to exit the psql shell and return to the postgres user prompt. Run exit to return from the postgres account to the main Terminal session.
\q

exit

How to Create a PostgreSQL User on Ubuntu 24.04
PostgreSQL matches database users to Linux system users by username. Create a Linux user first before creating the PostgreSQL user with that name.
Step 1: Create a Linux User
Create a new Linux user with adduser and the desired username. Set a password when prompted. This username will match the PostgreSQL user you create next.
sudo adduser test


Step 2: Create a PostgreSQL User
Create a PostgreSQL user with the createuser command. The –interactive flag prompts for the username and whether to grant superuser privileges.
sudo -u postgres createuser --interactive

Step 3: List Existing Users
Log in to the psql shell and run \du to display all existing PostgreSQL roles. The output shows each user, their attributes, and assigned group memberships.
\du
Step 4: Log In with the New User
Switch to the new Linux user account with sudo -i -u, then run psql to open the PostgreSQL shell logged in as the matching PostgreSQL user.
sudo -i -u test

How to Create and Delete a PostgreSQL Database
Step 1: Create a Database
Create a database from the psql shell with CREATE DATABASE. Specify the database name after the keywords and end the statement with a semicolon.
CREATE DATABASE example;
Alternatively, create a database from the Ubuntu Terminal without entering the psql shell by using the createdb command with sudo privileges.
sudo -u user createdb userdb
Step 2: List All Databases
Run \l inside the psql shell to list all existing PostgreSQL databases. The output shows each database, its owner, encoding, and access privileges.
\l
Step 3: Connect to a Database
Connect to a specific database with \c followed by the database name. The prompt updates to show the connected database name after switching.
\c example
Step 4: Delete a Database
Drop a database with the DROP DATABASE command inside the psql shell. Only the database owner or a superuser can remove a PostgreSQL database.
DROP DATABASE example;
How to Uninstall PostgreSQL from Ubuntu 24.04
Run the command below to completely remove PostgreSQL and all its packages from Ubuntu. The –purge flag removes configuration files along with the binary.
sudo apt-get --purge remove postgresql postgresql-*
When PostgreSQL Is the Right Tool
PostgreSQL is the right tool when you need a full-featured relational database for web applications, APIs, analytics, or multi-user enterprise systems.
If you only need a lightweight local database for development, SQLite may be simpler. PostgreSQL is best when ACID compliance and scalability matter.