Categories: Ubuntu

How to Install and Set up PostgreSQL Database on Ubuntu 22.04

Ubuntu 22.04 users utilize PostgreSQL as a popular database management system, deployed in the software market for about 20 years. This database system is highly reliable, robust, and backed up by a vibrant community that volunteers their precious time to help it grow. As a result, the correction and integrity of the PostgreSQL database are increasing on a daily basis.

This blog will demonstrate the procedure of installing and setting up the PostgreSQL database on Ubuntu 22.04. Let’s get started.

Installing PostgreSQL on Ubuntu 22.04

For the purpose of installing PostgreSQL on Ubuntu 22.04, follow the given instructions.

Sponsored

Step 1: Update system packages
First of all, hit “CTRL+ALT+T” and update the system packages:

$ sudo apt update

Step 2: Install PostgreSQL
Next, execute the provided command for the installation of PostgreSQL on Ubuntu 22.04:

$ sudo apt install postgresql postgresql-contrib

Now move to the next step.

Step 3: Start PostgreSQL service
Write out the provided command for starting the PostgreSQL service:

$ sudo systemctl start postgresql.service

Now, let’s head towards setting up the PostgreSQL database on Ubuntu 22.04.

Setting up PostgreSQL database on Ubuntu 22.04

Follow the below-given instructions for setting up the PostgreSQL database on Ubuntu 22.04.

Step 1: Switch to postgres account
The installation of PostgreSQL automatically created a user account named “postgres”. Switch to this account for accessing the PostgreSQL database:

$ sudo -i -u postgres

Step 2: Create new role
After logging into the “postgres” account, create a new role with the help of the provided command:

$ createuser –interactive

Enter the role name and permit the new role to a super user. For instance, we have specified “linuxhint” as our new role name and entered “y” to mark this as a super user:

Sponsored

Step 3: Create PostgreSQL database
Now execute the “createdb” command and specify the same name for the PostgreSQL database which you have added as role name:

$ createdb linuxhint

Step 4: Create a new user

Utilize the “adduser” command for creating a new user with the same name as the PostgreSQL role and database:

$ sudo adduser linuxhint

Step 5: Switch account
Next, switch to the newly created account by specifying the username in the following command:

$ sudo -i -u linuxhint

Step 6: Connect to PostgreSQL database
Run the “psql” command for establishing a connection with the PostgreSQL database:

$ psql

Step 7: Check connection information
Lastly, verify that you have connected to the PostgreSQL database:

conninfo

The given output indicates that we have successfully connected to the created “linuxhint” PostgreSQL database:

How to uninstall PostgreSQL Database on Ubuntu 22.04

To uninstall PostgreSQL database on Ubuntu 22.04, run the following command:

$ sudo apt remove postgresql postgresql-contrib

We have compiled the easiest method to install, set up, and uninstall PostgreSQL database on Ubuntu 22.04.

Conclusion

To install and set up the PostgreSQL database, firstly update the system packages and execute the “$ sudo apt install postgresql postgresql-contrib” command for PostgreSQL installation. Then, start the PostgreSQL service, switch to the “postgres” account, and create a new role, a database, and a new user. Then switch to the newly created account and run the “$ psql” command for connecting to the PostgreSQL database. This blog demonstrated the procedure of installing, setting up, and uninstalling the PostgreSQL database on Ubuntu 22.04.

Ubuntu Server Admin

Recent Posts

CRA compliance: Things IoT manufacturers can no longer do under the CRA (and what to do instead)

I’ve written about the EU Cyber Resilience Act (CRA) on our Canonical blog a few…

2 days ago

IBM LinuxONE 5 and Ubuntu Server, a great combination from day one

Today, IBM announced the launch of their latest server: the new IBM LinuxONE Emperor 5.…

3 days ago

Ubuntu Weekly Newsletter Issue 890

Welcome to the Ubuntu Weekly Newsletter, Issue 890 for the week of April 27 –…

3 days ago

Ubuntu IoT Day in Singapore – Unlock compliant and scalable innovation in edge AI

Singapore | May 27, 2025 | Full-day event How do you build robust, performant edge…

4 days ago

Kolla Ansible OpenStack Installation (Ubuntu 24.04)

Kolla Ansible provides production-ready containers (here, Docker) and deployment tools for operating OpenStack clouds. This…

7 days ago

Canonical announces first Ubuntu Desktop image for Qualcomm Dragonwing™ Platform with Ubuntu 24.04

This public beta enables the full Ubuntu Desktop experience on the Qualcomm Dragonwing™ QCS6490 and…

1 week ago