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.

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:

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

Ubuntu MATE 26.10 Release Notes

Lo and behold 👐, Ubuntu MATE 26.10 (Stonking Stingray) is here! 🎊 We are back…

1 week ago

How to Deploy Kiwix Server on Ubuntu VPS

This article provides a guide demonstrating how to deploy Kiwix Server on Ubuntu VPS. What…

1 week ago

How to Host a Discord Bot on Ubuntu VPS

This article provides a guide demonstrating how to host a Discord bot on Ubuntu VPS.…

1 week ago

Myna speech-to-text is now available as a technology preview

A few months ago, we introduced Myna, our project to bring private, local speech-to-text dictation…

1 week ago

What’s new in security for Ubuntu 26.10?

Ubuntu 26.10 “Stonking Stingray” arrives at a time when AI tools are accelerating vulnerability discovery.…

2 weeks ago

Devpack for Rust: Zero to Rust in seconds

One of the best things about Rust is how easy it is to get started.…

2 weeks ago