How to Install PostgreSQL on Debian 12

how to install postgresql on debian 12

PostgreSQL is an open-source relational database management system used by large companies around the globe. It is a powerful and reliable RDMS that offers an extensive data storage and management feature set. In this article, we will show you how to install PostgreSQL on Debian 12.

PostgreSQL is scalable, enabling users to store and handle vast quantities of data while offering strong performance and dependability. Let’s start with the installation.

Prerequisites

  • A Debian 12 VPS with at least 4GB of RAM
  • SSH access with sudo privileges or root access.

In addition, it is recommended to have at least 2GB of SWAP memory, even if you have enough available RAM.

Step 1. Update the System

First of all, we need to log in to our Debian 12 VPS through SSH:

ssh root@IP_Address -p Port_number

Replace “root” with a user that has sudo privileges or root if necessary. Additionally, replace “IP_Address” and “Port_Number” with your server’s respective IP address and SSH port number. Next, let’s make sure that we’re on Debian 12. You can do that like this:

# lsb_release -a

The command should return an output similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 12 (bookworm)
Release: 12
Codename: bookworm

Step 2. Install PostgreSQL

There are a couple of different methods to install PostgreSQL on Debian 12. One method is using the system’s default repository, another is using PostgreSQL official repository, or you can install it from the source code. In this tutorial, we will show you the two easiest methods to install PostgreSQL on Debian 12.

Method 1

The easiest way to install PostgreSQL on Debian 12 is using the Advanced Package Tool (apt). The installation is easy and straightforward. Execute the command below to install PostgreSQL.

# apt install postgresql

On the Debian 12 system, PostgreSQL will automatically run upon installation. To verify this, we can invoke this command.

# systemctl status postgresql

The command above will return an output similar to this:

root@debian12:~# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; preset: enabled)
     Active: active (exited) since Thu 2023-08-10 18:43:42 CDT; 1min 21s ago
   Main PID: 29458 (code=exited, status=0/SUCCESS)
        CPU: 2ms

Aug 10 18:43:42 debian12.rosehosting.net systemd[1]: Starting postgresql.service - PostgreSQL RDBMS...
Aug 10 18:43:42 debian12.rosehosting.net systemd[1]: Finished postgresql.service - PostgreSQL RDBMS.

We can also check the installed version of PostgreSQL by running this command.

# psql --version

You should see this as the output.

psql (PostgreSQL) 15.3 (Debian 15.3-0+deb12u1)

Method 2

Most Linux distributions ship PostgreSQL in their repository. However, the included PostgreSQL version is quite outdated in some cases. To get the most recent version of PostgreSQL or a specific version of PostgreSQL, we can use the PostgreSQL repository. This repository will integrate with your normal system and patch management and provide automatic updates for all supported versions of PostgreSQL throughout the support lifetime of PostgreSQL. In this method, we will show you how to install PostgreSQL using their official repository.

First of all, we need to install the gnupg package before continuing.

# apt install gnupg2 -y

After installing it, we can add or create a file repository configuration. Let’s execute this command.

# sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Then, import the repository signing key. This will guarantee that the packages we install are from a reliable source. Execute the following command:

# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

Once completed, we need to refresh the package list on our Debian 12 system.

# apt update

Finally, install the latest version of PostgreSQL.

# apt -y install postgresql

If you want a specific version, for example, PostgreSQL 14, use ‘postgresql-14’ instead of ‘postgresql’ in the command above.

To check the installed version, depending on the version, you can run this command:

# /usr/lib/postgresql/14/bin/psql --version

It will return an output like this:

psql (PostgreSQL) 14.9 (Debian 14.9-1.pgdg120+1)

Step 3. Using PostgreSQL

Once PostgreSQL is installed, a default database and user account called ‘postgres’ will be set up during the installation. To access the database, execute the following command in your Debian 12 system to switch to user postgres.

 su - postgres

After switching to the postgres user, we can run this command to get into PostgreSQL shell.

$ psql
postgres@debian12:~$ psql
psql (15.3 (Debian 15.3-0+deb12u1))
Type "help" for help.

postgres=#

Run \l to list the databases.

postgres@debian12:~$ psql
psql (15.3 (Debian 15.3-0+deb12u1))
Type "help" for help.

postgres=# \l
                                                 List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges   
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
(3 rows)

postgres=#

To exit from the PostgreSQL shell, simply type \q, then hit ENTER.

That’s it all! You have successfully installed the PostgreSQL server on your Debian 12 system.

Of course, if you are one of our Debian Hosting customers, you don’t have to install PostgreSQL on Debian 12 yourself – simply ask our admins, sit back, and relax. Our admins will install PostgreSQL on Debian 12 for you immediately without any additional fee, along with many useful optimizations that we can do for you. Managing a PostgreSQL-based website is not just about the installation. We can help you with optimizing your PostgreSQL installation if you have an active VPS with us.

If you liked this post about how to install PostgreSQL on Debian 12, please share it with your friends on social networks or simply leave a comment in the comments section. Thanks.

Leave a Comment