PostgreSQL is an open-source object-relational database that has relational capability along with an object-oriented design. PostgreSQL helps the programmers to communicate with the remote server using the coding objects. PostgreSQL stores and manages data for big companies to streamline their operations. PostgreSQL does not come preinstalled on Ubuntu Linux, however, it can be installed manually. This article will demonstrate a complete detailed guide to installing PostgreSQL on Ubuntu 24.04.

How to Install PostgreSQL (psql) on Ubuntu 24.04

As we learned, PostgreSQL does not come preinstalled on Ubuntu. However, it is readily available in the official repositories of Ubuntu, and we can install it using the apt package manager. Check the following steps to install the PostgreSQL on Ubuntu 24.04.

Step 1: Update System Packages

Before installing PostgreSQL on Ubuntu, ensure that system repositories are updated to their latest version. To do so, press CTRL + Alt + T shortcut keys to open the Terminal, and run the given command:

sudo apt update && sudo apt upgrade

Step 2: Install PostgreSQL

Once the local packages are updated, run this given command to install PostgreSQL on Ubuntu:

sudo apt install postgresql -y postgresql-contrib

Note: The additional command in the above script postgresql-contrib will install some extra useful utilities and extensions that will help in the smooth running of the PostgreSQL on Ubuntu:

Note: To install PostgreSQL without any additional utilities and features, you can run this command:

sudo apt install postgresql

Step 3: Verify PostgreSQL Installation

Once you have installed PostgreSQL on Ubuntu, you can verify its installation by executing this command:

sudo service postgresql status

This command will check the PostgreSQL service running status:

Alternatively, you can also check the PostgreSQL status by running this command:

sudo systemctl status postgresql

Note: In case the PostgreSQL service is not running, it means that it is disabled. To start it normally, you first need to enable it and then start it. To enable the PostgreSQL service, run this command:

sudo systemctl enable postgresql.service

Once the service is enabled, run this command to start the PostgreSQL service:

sudo systemctl start postgresql.service

Login to PostgreSQL on Ubuntu 24.04

As of now, PostgreSQL is installed and running, and now it’s time to access the PostgreSQL database. Whenever you install PostgreSQL on Ubuntu, it automatically creates the postgres user account in the database and this user can access the database too. To login to the PostgreSQL database, check the following steps.

Step 1: Log in to PostgreSQL

To access the database, you need to login to the database by running this command:

sudo -i -u postgres

Note: In case if you want to log in to the database with another user, then replace the Postgres with your user ID:

Step 2: Access PostgreSQL Terminal

Once logged in run the psql command to access the PostgreSQL command line utility, which will help you access and manage the database:


Alternatively, you can also access the database without switching accounts with one single command, skipping Step 1 and Step 2. You can do this by using the sudo command, providing the postgres user, and then specifying the psql command, as shown below:

sudo -u postgres psql

Step 3: Set PostgreSQL Password

By default, the user postgres has no password, however, you can set a password for this account by running this command, when logged in to the PostgreSQL database:

\password postgres

Running the above command will prompt you to enter the password and then re-enter the password to confirm:

Step 4: Logout of PostgreSql

Once you are done using PostgreSQL with a specific user, you can log out by running the given command:


Note: The \q command will return you to the PostgreSQL user prompt. But to return to the main Terminal, run the exit command:


Create a PostgreSQL User on Ubuntu 24.04 (Optional)

Besides the PostgreSQL default user postgres, you can also create additional users for assigning different roles and permissions. To create a new PostgreSQL user on Ubuntu, check the following steps.

Note: To create a PostgreSQL user, you must first have the Linux user with the same username as you wish to create the PostgreSQL user.

Step 1: Create a Ubuntu Linux User

To add a new Linux user, use the adduser command with sudo privileges along with the desired username, run this command:

sudo adduser test

To learn how to add users on Ubuntu in detail, read this article: Create a New User on Ubuntu

Step 2: Create a New PostgreSQL User

Once you created the user with the name you want to create the postgresql user, then, run this command to create a PostgreSQL user:

sudo -u postgres createuser --interactive

The –interactive flag in the above command will prompt you to enter the username and will ask you to make the new user a superuser or not:

Step 3: Check the List of Existing Users

After creating the PostgreSQL user on Ubuntu, verify the user creation by checking the list of existing users in PostgreSQL by running this command:


Step 4: Login to the Database with New User

After creating the Ubuntu user and PostgreSQL user with the same username, you can log in to that PostgreSQL user account by running this account:

sudo -i -u test

Remember: Replace the above test username with your PostgreSQL username:

Step 5: Verify the New PostgreSQL User

After logging into the PostgreSQL user account, let’s verify whether the new account was created or not by checking its connection info. To do so, first, access the PostgreSQL prompt by running this command:


Now, run this command to check the connection information and verify whether you are logged in as a new user:


Create a PostgreSQL Database on Ubuntu 24.04

While installing PostgreSQL on Ubuntu, it automatically creates a username and a database with the same name postgres. In PostgreSQL, to access the database, you must have a similar username to the database. For instance, if you have created a user test, then PostgreSQL will grant access to the test database to the test user only. To create a PostgreSQL database, check the following steps.

Step 1: Create a Database

To create a PostgreSQL database, use the CREATE DATABASE command and specify the database name in the Terminal, as shown below:


Alternatively, you can also use the createdb command to create a PostgreSQL database, as shown below:

createdb example

You can use your desired database name instead of the test:

Step 2: List Down All Databases

Once you created the database, let’s verify the PostgreSQL database creation by running the below command:


The \l command lists down all the existing PostgreSQL databases on Ubuntu:

Step 3: Connect to the Database

After logging in, check the connection info for the newly created PostgreSQL database, by running this command:

\c example

Step 4: Delete the Database

You can delete any PostgreSQL database by providing the database name to the DROP DATABASE command, as shown below:


Alternatively, you can create a database without switching to the PostgreSQL prompt. To create a PostgreSQL database directly from the Ubuntu Terminal using the sudo privileges run this command:

sudo -u user createdb userdb

How to Uninstall PostgreSQL from Ubuntu 24.04

If you are done with using PostgreSQL and want to remove it from Ubuntu. Then, execute the given command, to completely remove PostgreSQL from Ubuntu, run this command:

sudo apt-get --purge remove postgresql postgresql-*


To install PostgreSQL on Ubuntu, open Terminal, and run the apt install postgresql command with sudo privileges. To install the additional features with PostgreSQL to manage it then, run the apt install postgresql postgresql-contrib command. By default, PostgreSQL creates a user postgres, so, to log in to the database with a postgres user, run the sudo -i -u postgres command. To create the PostgreSQL database user, first, create a Linux user with the same name and then, run the createuser –interactive command and answer some questions to create the user. To create a PostgreSQL database, run the createdb {database-name} command.