How To Install PostgreSQL 15 on Ubuntu 22.04

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

The open-source PostgreSQL 15 relational database became generally available on Thursday, with a series of enhancements designed to accelerate performance and data management.

Users of the PostgreSQL 15 update get a number of improvements, including new compression capabilities that help with data storage and backup, data sorting enhancement for faster lookups, and new logging and SQL capabilities.

Steps To Install PostgreSQL 15 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 on Initial Server Setup with Ubuntu 22.04.

Set up PostgreSQL 15 on Ubuntu 22.04

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

sudo apt update

Install Required Packages

Then, you need to install the required packages by running the command below:

sudo apt install dirmngr ca-certificates software-properties-common gnupg gnupg2 apt-transport-https curl -y

Import PostgreSQL GPG Key

At this point, you need to impost the PostgreSQL GPG key on Ubuntu 22.04. To do this, run the following command:

curl -fSsL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/postgresql.gpg > /dev/null

Import PostgreSQL 15 Repository on Ubuntu 22.04

Next, you can import the stable or testing repository, depending on your desired installation. 

To import PostgreSQL 15 stable build that is recommended, run the following command:

echo deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt/ jammy-pgdg main | sudo tee -a /etc/apt/sources.list.d/postgresql.list

Import PostgreSQL 15 testing build (Caution), you can use the following command:

echo deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt/ jammy-pgdg-testing main | sudo tee /etc/apt/sources.list.d/postgresql-testing.list

Note: The testing is for testing, as sometimes it can be behind or ahead, which is unsuitable for production.

Install PostgreSQL 15 on Ubuntu 22.04

Update your local package index with the command below:

sudo apt update

At this point, use the command below to install PostgreSQL 15:

sudo apt install postgresql-client-15 postgresql-15 -y

If you are using the test repository, you can use the command below instead. With this, future versions will always be prioritized, such as PostgreSQL 16, etc.

sudo apt install postgresql-client postgresql -y

By default, PostgreSQL will be activated during the installation. To confirm this, run the following command:

sudo systemctl status postgresql
Output
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor pr>
     Active: active (exited) since Sat 2022-10-29 08:36:52 UTC; 56s ago
   Main PID: 4849 (code=exited, status=0/SUCCESS)
        CPU: 2ms
...

If your PostgreSQL service is not activated on Ubuntu 22.04, run the command below to enable it:

sudo systemctl enable postgresql --now

Manage PostgreSQL Service

As you know, the PostgreSQL database server runs as a service under the name “PostgreSQL,” which can be managed with the systemd with the following command examples.

To start the PostgreSQL server, you can use the command below:

sudo systemctl start postgresql

You can stop the PostgreSQL server by running the command below:

sudo systemctl stop postgresql

To restart the PostgreSQL server, you can use the following command:

sudo systemctl restart postgresql

Reload the PostgreSQL server by running the command below:

sudo systemctl reload postgresql

Also, you can check your PostgreSQL status on Ubuntu 22.04 by using the following command:

systemctl status postgresql

Configure PostgreSQL 15 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@olivia:~$

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

postgres@olivia:~$ psql

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

Output
psql (15.0 (Ubuntu 15.0-1.pgdg22.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@olivia:~$

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

postgres@olivia:~$ exit

Also, you can connect to your Postgres 15 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 15 shell.

Output
psql (15.0 (Ubuntu 15.0-1.pgdg22.04+1))
Type "help" for help.

postgres=#

Create a new PostgreSQL 15 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@olivia:~$ 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 15 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@olivia:~$ createdb orca

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

sudo -u postgres createdb orca

Open the Postgres 15 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@olivia:~$ 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 and Configure PostgreSQL 15 on Ubuntu 22.04.

Hope you enjoy it.

You may be like these articles:

How To Install PostgreSQL 14 on Rocky Linux 8

How To Set up PostgreSQL on Debian 11

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!