How to Install PostgreSQL on Ubuntu 22.04

Install Postgresql On Ubuntu 22.04

In this guide, we will see how can we install the PostgreSQL database server with the client on the ubuntu operating system version 22.04. Thanks to the tools such as APT and snap, you can easily install Postgresql and the other required software programs with ease and in a couple of seconds. So without wasting the time, let’s get started!

Prerequisites

This tutorial is specially made for ubuntu users of version 22.04. This might work for ubuntu version 20 as well. You should possess a basic knowledge about the basic commands such as apt. Apart from that, you need to meet the following prerequisites-

  • A computer running Ubuntu 22.04 or a later version of Ubuntu.
  • A user account with Sudo privileges.
  • A stable internet connection to download packages and dependencies.

Install PostgreSQL On Ubuntu

That’s it. Let’s now see how can we install PostgreSQL on ubuntu.

Update the packages

The first thing is to make sure that all the system packages are up to date. To do this, you need to use the update and upgrade commands-

sudo apt update && sudo apt upgrade
Update And Upgrade Packages
Update And Upgrade Packages

The command “sudo apt update” will update the packages and does not install any updates. To install the updated packages, we use “sudo apt upgrade” command.

This will ensure that all the dependencies that the PostgreSQL needs are up to date.

Sometimes your system needs to reboot for changes to take place. So, simply reboot the system.

Now we are ready to install the PostgreSQL database server on our ubuntu 22.04 version.

Install PostgreSQL Server & Client

We will now install the PostgreSQL database server without configuring anything. Along with the PostgreSQL database server, we will need the PostgreSQL client so that we can connect to the server, write queries and perform operations on the database.

To install the required packages, write the following command in the terminal-

sudo apt install postgresql postgresql-client
Install Postgresql And Client
Install Postgresql And Client

The above command will first install the PostgreSQL database server and then the PostgreSQL client.

Note that, PostgreSQL Client will not be installed as a graphical user interface program. Instead, you can use the terminal as a client to connect to the PostgreSQL database server.

Now that the server, as well as the client, are installed, check the status of the PostgreSQL service.

sudo systemctl status postgresql
Check Postgresql Servicestatus
Check Postgresql Service status

As you can see, the PostgreSQL service is running now.

Login to PostgreSQL Client

There are multiple methods to login into the PostgreSQL client. However, the simplest is to log in using the peer connection method.

Enter the following command in the terminal and hit enter.

sudo -u postgres psql
Login To Postgresql Client
Login To Postgresql Client

Here, -u specifies the shorthand for the username, ‘postgres’ is the username in the postgresql database server whereas psql is our program.

Here, you don’t need to enter the password because the Postgres program uses the peer connection authentication method.

In PostgreSQL, a “peer” connection is a type of connection that is used to authenticate users who have the same name as the operating system user connecting to the database.

The user of name “postgres” is created automatically when you install the PostgreSQL server.

When a user connects to the database, PostgreSQL checks the operating system user name of the connecting process and compares it to the names of the database users. If there is a match, the connection is authenticated using the “peer” authentication method.

After entering the above command, you will enter the postgres client. Now you can write queries and perform operations on the database.

To list all the databases, you can use the \l command.

List All Databases
List All Databases

As you can see, the database list is shown in front of us. To connect to the database, use the command \c followed by the database name.

Select Database
Select Database

As you can see here, we get the message that “you are now connected to the database”.

Conclusion

In this tutorial, we have seen how we can install the PostgreSQL database server and the client on ubuntu 22.0.4 using the apt command. It is a straightforward process that needs no expertise in anything and even a beginner can follow it without any problem.