How To Install PostgreSQL on Ubuntu 22.04

This guide intends to teach you How To Install PostgreSQL on Ubuntu 22.04.

PostgreSQL is a powerful, open-source object-relational database system. It has more than 15 years of the active development phase and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.

Steps To Install PostgreSQL on Ubuntu 22.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Ubuntu 22.04.

Install PostgreSQL 14 on Ubuntu 22.04

By default, PostgreSQL 14 packages are available in the default Ubuntu 22.04 repository.

First, you need to update your local package index with the following command:

sudo apt update

Then, install PostgreSQL and its dependencies with the following command:

sudo apt install postgresql postgresql-contrib

When your installation is completed, let’s see how to use PostgreSQL.

How to Use PostgreSQL on Ubuntu 22.04

PostgreSQL uses a concept called roles to handle authentication and authorization.

During the PostgreSQL installation, Postgres is set up to use ident authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account.

If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.

One way is to switch over to the Postgres account on your server by running the following command:

sudo -i -u postgres
Output
postgres@sam:~$

You can access the Postgres shell on Ubuntu 22.04 by running the following command:

postgres@sam:~$ psql

With this command, you will see that your shell prompt changes to:

Output
psql (14.5 (Ubuntu 14.5-0ubuntu0.22.04.1))
Type "help" for help.

postgres=#

To exit from the PostgreSQL shell, run the following command:

postgres=# \q

With this command, you will be back to the Postgres Linux command prompt.

postgres@sam:~$

You can type exit to return to the regular system user.

postgres@sam:~$ exit

Also, you can connect to your Postgres shell on Ubuntu 22.04 in another way by typing the following command:

sudo -u postgres psql

In this way, you will directly connect to your PostgreSQL shell.

Output
psql (14.5 (Ubuntu 14.5-0ubuntu0.22.04.1))
Type "help" for help.

postgres=#

Create a new PostgreSQL Role

At this point, you can create a new Postgres role in two ways.

First, if you are logged in as a Postgres account, you can use the following command:

postgres@sam:~$ createuser --interactive

Second, you can use sudo for each command without switching from your normal account on Ubuntu 22.04:

sudo -u postgres createuser --interactive

Both ways will be asked to enter your role name and whether the new role is a superuser or not.

Output
Enter name of role to add: orca
Shall the new role be a superuser? (y/n) y

Create a new PostgreSQL Database

The Postgres authentication system for any role used to log in, the role should have a database with the same name which it can access.

If you logged in as the Postgres account, run the following command to create the database with the name of the role you have created in the previous step:

postgres@sam:~$ createdb orca

Or you can use sudo to create the database on Ubuntu 22.04:

sudo -u postgres createdb orca

Open the Postgres Shell with the new Role

At this point, you need to create a Linux user with the same name as your Postgres role and database. To do this, run the following command:

sudo adduser orca

Now you can connect to the Postgres database with the following commands:

# sudo -i -u orca
# orca@sam:~$ psql

Or you can use the following command instead:

sudo -u orca psql

When you have logged in to your database, you can check your current connection information by running the following command:

orca=# \conninfo

In your output you will see:

Output
You are connected to database "orca" as user "orca" via socket in "/var/run/postgres

For more information, you can visit the PostgreSQL Documentation page.

Conclusion

At this point, you have learned to Install PostgreSQL on Ubuntu 22.04 and basic usage of it.

Hope you enjoy it.

How To Install and Use Iptables on Ubuntu 22.04

Set Up Time Synchronization on Ubuntu 22.04

Enable and Configure SSH on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!