How To Install PostgreSQL 15 on Ubuntu 22.04 | Easy Setup
This guide aims to walk you through How To Install PostgreSQL 15 on Ubuntu 22.04. The open-source PostgreSQL 15 relational database made its debut recently, packed with a series of advancements designed to boost performance and streamline data management. It’s a significant upgrade for anyone using PostgreSQL.
Users who update to PostgreSQL 15 will find a host of improvements, including innovative compression capabilities that optimize data storage and backup processes, enhanced data sorting for faster lookups, and expanded SQL and logging functionalities. These enhancements collectively contribute to a more efficient and robust database experience.
Let’s dive into the steps on how to How To Install PostgreSQL 15 on Ubuntu 22.04.
To follow this guide, ensure you are logged into your Ubuntu 22.04 server as a non-root user with sudo privileges. If you need help setting this up, refer to our guide on Initial Server Setup with Ubuntu 22.04.
Set up PostgreSQL 15 on Ubuntu 22.04
First, refresh your local package index using the following command:
sudo apt update
Install Required Packages
Next, install the necessary packages using the following command:
sudo apt install dirmngr ca-certificates software-properties-common gnupg gnupg2 apt-transport-https curl -y
Import PostgreSQL GPG Key
Now, import the PostgreSQL GPG key on Ubuntu 22.04 to verify the integrity of the packages. Execute 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
Import the PostgreSQL 15 repository to your system. You can choose between the stable or testing repository based on your requirements.
To import the recommended PostgreSQL 15 stable build, use this 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
For importing the PostgreSQL 15 testing build (use with caution), 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-testing main | sudo tee /etc/apt/sources.list.d/postgresql-testing.list
Note: The testing repository is intended for testing purposes and may contain unstable or outdated packages, making it unsuitable for production environments.
Install PostgreSQL 15 on Ubuntu 22.04
Update your local package index again:
sudo apt update
Then, install PostgreSQL 15 using the command below:
sudo apt install postgresql-client-15 postgresql-15 -y
If you opted for the testing repository, use the following command. This will prioritize future versions like PostgreSQL 16:
sudo apt install postgresql-client postgresql -y
PostgreSQL should automatically start during installation. To verify, use the following command:
sudo systemctl status postgresql

If PostgreSQL isn’t running, enable and start it with the following command:
sudo systemctl enable postgresql --now
Manage PostgreSQL Service
The PostgreSQL database server runs as a service named “PostgreSQL,” which can be managed using systemd commands.
To start the PostgreSQL server:
sudo systemctl start postgresql
To stop the PostgreSQL server:
sudo systemctl stop postgresql
To restart the PostgreSQL server:
sudo systemctl restart postgresql
To reload the PostgreSQL server:
sudo systemctl reload postgresql
To check the status of PostgreSQL on Ubuntu 22.04:
systemctl status postgresql
Configure PostgreSQL 15 on Ubuntu 22.04
PostgreSQL uses roles for authentication and authorization.
By default, PostgreSQL uses ident
authentication, linking Postgres roles to corresponding Unix/Linux system accounts. If a role exists in Postgres, a Unix/Linux username with the same name can log in as that role.
Switch to the Postgres account on your server:
sudo -i -u postgres
Output
postgres@olivia:~$
Access the Postgres shell on Ubuntu 22.04:
postgres@olivia:~$ psql
The shell prompt changes to:
Exit the PostgreSQL shell:
postgres=# q
Return to the Postgres Linux command prompt:
postgres@olivia:~$
Type exit
to return to the regular system user.
postgres@olivia:~$ exit
Alternatively, connect to your Postgres 15 shell on Ubuntu 22.04:
sudo -u postgres psql
This directly connects you to the 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
You can create a new Postgres role in two ways.
Logged in as a Postgres account:
postgres@olivia:~$ createuser --interactive
Or, using sudo from your normal account on Ubuntu 22.04:
sudo -u postgres createuser --interactive
Both methods prompt you for the role name and whether the new role is a superuser.
Output
Enter name of role to add: orca
Shall the new role be a superuser? (y/n) y
Create a new PostgreSQL 15 Database
For any role used to log in, the role should have a database with the same name which it can access.
Logged in as the Postgres account:
postgres@olivia:~$ createdb orca
Or, using sudo to create the database on Ubuntu 22.04:
sudo -u postgres createdb orca
Open the Postgres 15 Shell with the new Role
Create a Linux user with the same name as your Postgres role and database:
sudo adduser orca
Connect to the Postgres database:
# sudo -i -u orca
# orca@olivia:~$ psql
Or:
sudo -u orca psql
Check your current connection information:
orca=# conninfo
Output
You are connected to database "orca" as user "orca" via socket in "/var/run/postgres
For more information, consult the PostgreSQL Documentation page.
Conclusion
You’ve successfully learned how to How To Install PostgreSQL 15 on Ubuntu 22.04. PostgreSQL 15 on Ubuntu 22.04 delivers better performance, enhanced query optimization, improved security, and new features like the MERGE SQL command. It’s perfect for handling large databases, ensuring reliability, and supporting modern applications. How To Install PostgreSQL 15 on Ubuntu 22.04 is a valuable skill for any developer or system administrator.
Hope you enjoy it. You may also like these articles:
How To Install PostgreSQL 14 on Rocky Linux 8
How To Set up PostgreSQL on Debian 11
PostgreSQL Setup on Ubuntu 24.04
Install a Specific Version of PostgreSQL on Ubuntu 20.04
Install PostgreSQL on Debian 12 Bookworm
Alternative Installation Methods
While the above method using the PostgreSQL APT repository is standard and reliable, here are two alternative approaches to install PostgreSQL 15 on Ubuntu 22.04:
1. Using Docker
Docker provides a containerized environment, ensuring consistency and portability. This method is especially useful for development and testing environments where you want to isolate PostgreSQL from the host system.
Explanation:
Docker allows you to run PostgreSQL 15 inside a container. The container includes all the necessary dependencies and configurations, ensuring that PostgreSQL runs the same way regardless of the host environment. This simplifies deployment and eliminates potential conflicts with other software on your system.
Steps:
-
Install Docker: If you don’t have Docker installed, follow the official Docker documentation for Ubuntu: https://docs.docker.com/engine/install/ubuntu/
-
Pull the PostgreSQL 15 Docker image:
docker pull postgres:15
-
Run the PostgreSQL 15 Docker container:
docker run --name postgres15 -e POSTGRES_PASSWORD=your_password -p 5432:5432 -d postgres:15
--name postgres15
: Assigns the name "postgres15" to the container.-e POSTGRES_PASSWORD=your_password
: Sets the PostgreSQL superuser password. Replaceyour_password
with a strong password.-p 5432:5432
: Maps port 5432 on the host to port 5432 in the container.-d
: Runs the container in detached mode (in the background).
-
Access PostgreSQL: You can now connect to PostgreSQL using a client like
psql
or a GUI tool like pgAdmin. The host will belocalhost
and the port will be 5432.psql -h localhost -p 5432 -U postgres -W
Enter the password you set in the
POSTGRES_PASSWORD
environment variable.
Code Example (Docker Compose – Optional):
For more complex setups, you can use Docker Compose to define the PostgreSQL service. Create a docker-compose.yml
file:
version: "3.8"
services:
db:
image: postgres:15
restart: always
environment:
POSTGRES_PASSWORD: your_password
ports:
- "5432:5432"
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
Then, run:
docker-compose up -d
This will create and start the PostgreSQL container using the defined configuration.
2. Compiling from Source
Another alternative is to compile PostgreSQL 15 directly from source. This method provides the most control over the build process and allows for customization, but it’s also the most complex.
Explanation:
Compiling from source involves downloading the PostgreSQL source code, configuring the build environment, and then compiling and installing the software. This gives you fine-grained control over the installation process and allows you to optimize PostgreSQL for your specific hardware and software environment.
Steps:
-
Download the Source Code: Obtain the source code from the PostgreSQL website: https://www.postgresql.org/ftp/source/. Choose the
*.tar.gz
file for version 15. -
Extract the Archive:
tar -xvzf postgresql-15.x.tar.gz # Replace 15.x with the actual version number cd postgresql-15.x
-
Install Build Dependencies:
sudo apt-get update sudo apt-get install build-essential libreadline-dev zlib1g-dev flex bison
-
Configure the Build:
./configure --prefix=/usr/local/pgsql15
--prefix
: Specifies the installation directory.
-
Compile and Install:
make sudo make install
-
Set Up Environment Variables:
export PATH=/usr/local/pgsql15/bin:$PATH export PGDATA=/usr/local/pgsql15/data
You might want to add these lines to your
~/.bashrc
or~/.zshrc
file for persistence. -
Initialize the Database Cluster:
initdb -D $PGDATA
-
Start the PostgreSQL Server:
pg_ctl -D $PGDATA -l logfile start
Code Example (Starting the Server):
To start the PostgreSQL server in the background and direct output to a log file:
pg_ctl -D /usr/local/pgsql15/data -l /usr/local/pgsql15/logfile start
These alternative methods offer different advantages depending on your needs and environment. Docker provides portability and isolation, while compiling from source gives you maximum control over the installation.