How to Install and Set Up PostgreSQL Database on Ubuntu 22.04

PostgreSQL is a free, open-source, and powerful object-relational database. It supports Structure Query Language and provides modern features such as updatable views, transactional integrity, multi-version concurrency management, applied keys, and queries. PostgreSQL is a more executable, reliable, and secure database management system.

The following post will describe how to install, use, and uninstall PostgreSQL in Ubuntu 22.04.

PostgreSQL Database installation on Ubuntu 22.04

On Ubuntu 22.04, to install the PostgreSQL database, follow the steps given below.

Step 1: Update system packages

Open up the terminal using “CTRL+ALT+T” and update and upgrade the system packages:

$ sudo apt update && sudo apt upgrade -y

Step 2: Install Dependencies

Install additional dependencies on Ubuntu 22.04:

$ sudo apt install software-properties-common apt-transport-https wget -y

Move toward the next step.

Step 3: Import GPG key

After that, import the GPG key of PostgreSQL by utilizing the below command:

$ sudo wget -O- https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/postgresql.gpg

Step 4: Add PostgreSQL repository

In the next step, add the PostgreSQL repository into the system:

$ echo deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt/ jammy-pgdg main | sudo tee /etc/apt/sources.list.d/postgresql.list

Step 5: Update system packages

Again, update the system packages:

$ sudo apt update

Step 6: Install PostgreSQL

Install PostgreSQL on Ubuntu 22.04 with the provided command:

$ sudo apt install postgresql-client postgresql -y

Step 7: Check PostgreSQL status

Check the service status of PostgreSQL on Ubuntu 22.04:

$ systemctl status postgresql

Let’s move ahead to start the PostgreSQL service.

Step 8: Start PostgreSQL service

In order to start PostgreSQL services, use the mentioned command:

$ sudo systemctl start postgresql

We have successfully installed the PostgreSQL database on Ubuntu 22.04. Let’s move ahead to use it.

PostgreSQL Database set up on Ubuntu 22.04

On Ubuntu 22.04, utilize the provided instructions to set up the PostgreSQL database.

Step 1: Login to PostgreSQL

Run the following command to log in to PostgreSQL on Ubuntu 22.04:

$ sudo -i -u postgres

Execute “psql” to get the interactive interface of PostgreSQL:

$ psql

Alternatively, you can run the below command for the same purpose:

$ sudo -u postgres psql

Step 2: Create new Database

Make a new database and specify its name. We have created “itslinuxfoss” PostgreSQL database:

# create database itslinuxfoss;

Step 3: Create new User

Now, create a new user, and specify the username and password in the below-mentioned command:

# create user itslinux with encrypted password 'root';

Step 4: Grant privileges

In the next step, grant privileges of database to the new user:

# grant all privileges on database itslinuxfoss to itslinux;

Then, type “exit” to quit from the PostgreSQL shell:

# exit;

Want to remove the PostgreSQL database from Ubuntu 22.04? Have a look at the following section!

PostgreSQL Database removal from Ubuntu 22.04

To remove the PostgreSQL database from Ubuntu 22.04, execute this command:

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

Meanwhile, the “Package Configuration” wizard will prompt you to confirm the removal of the PostgreSQL database. Hit the “Yes” option:

Below displayed output indicates that PostgreSQL is removed successfully from Ubuntu 22.04:

Utilize the following command to remove the directory of PostgreSQL:

sudo rm -rf /etc/postgresql/

We have demonstrated the procedure to install, use, and remove PostgreSQL database on Ubuntu 22.04.

Conclusion:

To install PostgreSQL on Ubuntu 22.04, import the GPG key, and add the repository of PostgreSQL database into the system. Then, install PostgreSQL by utilizing the “$ sudo apt install postgresql-client postgresql -y” command. To use the PostgreSQL database, provide the default login credential “$ sudo -u postgres psql”. In this guide, we have demonstrated the step-by-step instructions to install, use, and remove the PostgreSQL database on Ubuntu 22.04.