How to Install PostgreSQL on Debian 12

When web and mobile applications are being developed, one important thing that comes to mind is data management. At this time, PostgreSQL comes as a perfect solution as it is a relational database management system and provides easy tools to manage the data of applications. 

Linux distributions are among the operating systems on which PostgreSQL can be installed. In this guide, installation methods of PostgreSQL on Debian 12 have been explained in a step-by-step guide. 

What are the PostgreSQL Installation Methods for Debian 12?

Two different installation methods of PostgreSQL on Debian 12 are:

  1. Using the default repository of Debian 12
  2. Using the PostgreSQL repository 

The next sections contain a detailed explanation of both methods. 

Method 1: Using the Default Repository of Debian 12

The easiest approach to installing packages on Debian 12 is by using its default repository. Follow the steps to install PostgreSQL on Debian 12 Linux distribution. 

Step 1: Update the Packages

Update all Debian’s packages:

$ sudo apt update

Step 2: Install PostgreSQL

Now, install the updated package of PostgreSQL on Debian 12:

$ sudo apt install postgresql postgresql-contrib -y

Step 3: Verify the Installation of PostgreSQL

When the installation is completed, verify it by displaying the status of the PostgreSQL on Debian 12:

$ sudo systemctl status postgresql

It can be seen that the running status is the confirmation of the installation of PostgreSQL on Debian 12.

What is the Uninstallation Method of the PostgreSQL on Debian 12?

To uninstall the PostgreSQL on Debian 12 by removing its associated files and libraries, execute the command:

$ sudo apt purge postgresql postgresql-contrib -y

The package of PostgreSQL has been uninstalled. 

Method 2: Install PostgreSQL Using the PostgreSQL Repository 

Using the official repository of PostgreSQL is another method of installing PostgreSQL on Debian 12. Use the below-mentioned steps for the installation of PostgreSQL. 

Step 1: Add the PostgreSQL Repository

Add the PostgreSQL official repository to the Debian 12 default repository with the following command:

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

The successful execution of the above command verifies the addition of the PostgreSQL repository to Debian 12. 

Step 2: Import the Signing Key of the PostgreSQL Repository

The official PostgreSQL repository’s signature key must then be imported as the following:

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

Step 3: Update the Repository

Now, update the Debian’s 12 repository:

$ sudo apt update

Step 4: Install PostgreSQL

Finally, install the PostgreSQL on Debian 12 using the “install” option of the apt package manager:

$ sudo apt install postgresql -y

How to Use PostgreSQL on Debian 12?

To use PostgreSQL on Debian 12, different commands are used to manage the database. The basic commands for managing the data of PostgreSQL are explained below. 

Setting Up the Password on PostgreSQL Database

To setup the password on the PostgreSQL database for the security of its data, use the command:

$ sudo passwd postgres

The password has been set on the PostgreSQL on Debian 12.

Login to the PostgreSQL Server

To login the PostgreSQL server, use the command:

$ su -- postgres

Access the PostgreSQL Shell

To access the PostgreSQL environment, use the command:

$ psql

Create the Database in PostgreSQL

To create the database in PostgreSQL, run the command:

CREATE DATABASE itslinuxfoss_data;

List the Database in PostgreSQL

To list down the database on PostgreSQL, execute the command:

\l

Delete the Database in PostgreSQL

To delete the database in PostgreSQL, use the “drop” option:

DROP DATABASE itslinuxfoss_data;

That’s all about the installation of PostgreSQL on Debian 12.

Conclusion

Use Debian’s 12 default repository and the command “sudo apt install postgresql postgresql-contrib -y” to install PostgreSQL. Another method of installing PostgreSQL on Debian 12 is by adding the official default repository of PostgreSQL. Both methods have been explained in the step-by-step guide on Debian 12. Also, the basic usage commands of PostgreSQL to manage the database have been explained as well.