In this guide, we want to teach you How To Set up PostgreSQL on Debian 11.
PostgreSQL, also known as Postgres, is a flexible open-source object-relational database management system. PostgreSQL can handle a huge variety of use cases, from single machines to data warehouses to web services with many concurrent users. It uses and extends SQL (hence the name), and is broadly extensible to a range of use cases beyond mere transactional data.
PostgreSQL is strongly supported by its global development group of companies and individual contributors.
How To Set up PostgreSQL on Debian 11
To install PostgreSQL, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Debian 11.
In this guide, you will install PostgreSQL from its official repository.
Follow the steps below to complete this guide.
Install PostgreSQL on Debian 11 using the official repository
To get the latest packages of PostgreSQL, you must add the official repo of PostgreSQL.
First, install the required packages on Debian 11 by using the following command:
sudo apt -y install gnupg2 wget
Then, add an additional repository that provides the packages. Create the file repository configuration 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'
Now import the repository signing key by using the following command:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Update your local package index with the following command:
sudo apt -y update
At this point, use the command below to install PostgreSQL on Debian 11:
sudo apt -y install postgresql
When your installation is completed, verify that is active and running on your server:
sudo systemctl status postgresql
Output ● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor pr> Active: active (exited) since Thu 2022-06-30 07:07:20 EDT; 3min 6s ago Process: 3931 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 3931 (code=exited, status=0/SUCCESS) CPU: 1ms
PostgreSQL can be used as a Postgres user only if you set a password; otherwise, you will not be able to log in as a Postgres user: Use the following command to set up a password:
sudo passwd postgres
Output New password: Retype new password: passwd: password updated successfully
To access your PostgreSQL server, you must log in as a Postgres user. To do this run the following command:
su – postgres
Now you can access your PostgreSQL shell with the following command:
[email protected]:~$ psql
In your output you will see:
Output psql (14.4 (Debian 14.4-1.pgdg110+1)) Type "help" for help. postgres=#
Once you have accessed the command shell of Postgres, you can now use SQL queries to perform several database-related operations.
For example, to create a database using the following command, here we named it orca:
postgres=# createdb orca
To access the database log in as a Postgres user and use the following command to access database “orca”:
[email protected]:~$ psql orca
Note: You can fall back to the Postgres user by using “\q”:
You can now add, delete, and select data from the database by accessing the database on the terminal.
For more information, you can visit the PostgreSQL Documentation page.
At this point, you learn to Set up PostgreSQL on Debian 11.
Hope you enjoy it.