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
Terminal running apt update and apt upgrade to update system packages on Ubuntu 24.04

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
Terminal installing PostgreSQL and postgresql-contrib on Ubuntu 24.04 using apt

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
Terminal running systemctl enable postgresql.service to configure PostgreSQL for automatic startup
sudo systemctl start postgresql.service
Terminal running systemctl start postgresql.service to start the PostgreSQL database on Ubuntu 24.04

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
Terminal showing sudo -i -u postgres command to switch to the postgres system user account

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
Terminal showing the psql shell prompt with postgres=# after running psql on Ubuntu 24.04

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
Terminal showing the psql password prompt after running backslash password postgres command

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
Terminal showing the backslash q command to exit the PostgreSQL psql shell
exit
Terminal showing the exit command returning from the postgres account to the main shell session

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
Terminal showing sudo adduser test command creating a new Linux user named test on Ubuntu 24.04
Terminal showing the adduser test setup prompts for full name and other user details

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
Terminal showing createuser --interactive prompting for the new PostgreSQL role name and superuser status

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
Terminal showing sudo -i -u test command switching to the test Linux user account

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.

Related Guides