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

How is Livepatch safeguarded against bad actors?

Canonical Livepatch is a security patching automation tool which supports reboot-less security updates for the…

3 hours ago

Accelerating data science with Apache Spark and GPUs

Apache Spark has always been very well known for distributing computation among multiple nodes using…

3 hours ago

Cut data center energy costs with bare metal automation

Data centers are popping up everywhere. With the rapid growth of AI, cloud services, streaming…

1 day ago

Build the future of *craft: announcing Starcraft Bounties!

Our commitment to building a thriving open source community is stronger than ever. We believe…

1 day ago

NodeJS 18 LTS EOL extended from April 2025 to May 2032 on Ubuntu

The clock was ticking: Node.js 18’s upstream End of Life (EOL) The OpenJS Foundation is…

1 day ago

Native integration now available for Pure Storage and Canonical LXD

June 25th, 2025 – Canonical, the company behind Ubuntu, and Pure Storage, the IT pioneer…

2 days ago