In this tutorial, we will explain in detail how to install PostgreSQL 15 on Debian 11 OS.
PostgreSQL or Postgres is a free and open-source relational database management system used worldwide in thousands of applications by thousands of database administrators and developers. PostgreSQL database management system is written in C and C++ and can handle multiple tasks at the same time. There is a number of database types offered by Postgres such as integer, big integer, date, interval and etc. In this blog post, except for the installation, we will explain logging into Postgres and some useful information about managing the PostgreSQL service.
Installing PostgreSQL 15 on Debian 11 is a straightforward process and may take up to 10 minutes. Let’s get started!
Table of Contents
Prerequisites
- A server with Debian 11 as OS
- User privileges: root or non-root user with sudo privileges
Step 1. Update the System
Before we start with the installation of PostgreSQL, we will update the system packages to the latest versions available.
sudo apt-get update -y && sudo apt-get upgrade -y
Step 2. Install Dependencies
To install the PostgreSQL required dependencies execute the following command:
sudo apt-get install gnupg2 wget curl -y
Step 3. Add PostgreSQL repo and Key
In the default Debian repository, the available version of PostgreSQL is version 13. Since we need the latest PostgreSQL 15 we need first to add the repository:
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Once the repo is added next is to add the GPG Key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
You should receive the following output:
root@host:~# sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' root@host:~# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - OK
Step 4. Install PostgreSQL 15
Everything is ready for installation, but first, we need to update the packages after the repo and key insertion:
sudo apt-get update -y
To install the PostgreSQL 15 version, execute the following command:
sudo apt-get install postgresql-15 -y
Step 5. Managing the PostgreSQL service
After installation, you need to start and enable the PostgreSQL service for an automatic start after the system reboot.
sudo systemctl start postgresql && sudo systemctl enable postgresql
To check the status of the service, execute the following command:
sudo systemctl status postgresql
You should receive the following output:
root@vps:~# sudo systemctl status postgresql ● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled) Active: active (exited) since Sat 2022-12-10 05:20:50 CST; 8min ago Main PID: 4191 (code=exited, status=0/SUCCESS) Tasks: 0 (limit: 4678) Memory: 0B CPU: 0 CGroup: /system.slice/postgresql.service Dec 10 05:20:50 host.test.vps systemd[1]: Starting PostgreSQL RDBMS... Dec 10 05:20:50 host.test.vps systemd[1]: Finished PostgreSQL RDBMS.
Respectively for restarting and stopping the service, you can use the following commands:
sudo systemctl restart postgresql sudo systemctl stop postgresql
Good to know is if the service is stopped and you try to restart it, it will start anyway. The restart of the service will act as a start when the service is stopped.
Useful PostgreSQL information
PostgreSQL is listening on port 5432, and you can check this with the following command:
sudo netstat -tunlp | grep 5432
You will receive output similar to this:
root@host:~# sudo netstat -tunlp | grep 5432 tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 5696/postgres tcp6 0 0 ::1:5432 :::* LISTEN 5696/postgres
The command for PostgreSQL login is the following one:
sudo -u postgres psql
Once logged in, you will see the Postgres command line:
root@host:~# sudo -u postgres psql could not change directory to "/root": Permission denied psql (15.1 (Debian 15.1-1.pgdg110+1)) Type "help" for help. postgres=#
That’s it. You successfully installed PostgreSQL 15 on Debian 11 OS. Of course, if you do not know how to install and configure it, you can always contact our technical support, and they will do the rest. You just need to sign up for one of our NVMe VPS hosting plans and submit a support ticket. We are available 24/7.
PS. If you liked this post on how to install PostgreSQL 15 on Debian 11, please share it with your friends on social networks using the buttons on the left or simply leave a reply below. Thanks.