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.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.