How to Install PostgreSQL on Debian 11 / Debian 10

PostgreSQL (often abbreviated as Postgres) is an object-relational database management system (ORDBMS) emphasizing extensibility and standards compliance.

It has more than 30 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and performance.

PostgreSQL is released under the PostgreSQL License and is available for Linux, Microsoft Windows, FreeBSD, OpenBSD, and macOS.

In this post, we will see how to install PostgreSQL on Debian 11 / Debian 10.

Add PostgreSQL repository

PostgreSQL publishes packages for Debian operating system through their dedicated repository, and the packages in their repository are fresher than those available in the OS repository.

First, update the repository index using the apt command

sudo apt update

Then, install the HTTPS support for apt to get packages from the internet.

sudo apt install -y curl apt-transport-https

Now, import the PostgreSQL signing key.

# Debian 11

curl -fsSL | sudo gpg --dearmor -o /usr/share/keyrings/postgresql-keyring.gpg

# Debian 10

curl -fsSL | sudo apt-key add -

Finally, add the PostgreSQL repository information to the system with the below command.

# Debian 11

echo "deb [signed-by=/usr/share/keyrings/postgresql-keyring.gpg] bullseye-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql.list

# Debian 10

echo "deb buster-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql.list

Install PostgreSQL on Debian

Once you have added the PostgreSQL repository, run the following command to update the repository index.

sudo apt update

Then, install PostgreSQL using the below command based on the PostgreSQL version you want to install.

# PostgreSQL 13

sudo apt install -y postgresql-13

# PostgreSQL 12

sudo apt install -y postgresql-12

By now, the PostgreSQL service will be up and running. You can check the status of the service with the below command.

sudo systemctl status postgresql


 postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: active (exited) since Wed 2021-09-15 14:19:30 CDT; 42s ago
 Main PID: 3759 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 2343)
   Memory: 0B
   CGroup: /system.slice/postgresql.service

Sep 15 14:19:30 debian.itzgeek.local systemd[1]: Starting PostgreSQL RDBMS...
Sep 15 14:19:30 debian.itzgeek.local systemd[1]: Started PostgreSQL RDBMS.

Configure PostgreSQL Server

By default, PostgreSQL listens on the localhost (, which you may not always want as external applications may need to connect to the database. So, we will configure PostgreSQL to listen to the system IP address.

# PostgreSQL 13

sudo nano /etc/postgresql/13/main/postgresql.conf

# PostgreSQL 12

sudo nano /etc/postgresql/12/main/postgresql.conf

Set the listen_addresses to * or <IPAddress>.

listen_addresses = ''

Restart PostgreSQL service.

sudo systemctl restart postgresql

You can now use the netstat command to confirm if PostgreSQL is listening on port 5432 or not.

sudo netstat -antup | grep 5432


tcp        0      0*               LISTEN      5265/postgres

READ: netstat command not found on Debian – Quick Fix

Access PostgreSQL

To manage the PostgreSQL database, you will need to log in as a postgres (Linux user) and then have to access the database shell using the psql command.

sudo -u postgres psql


postgres@server:~$ psql
psql (13.4 (Debian 13.4-1.pgdg100+1))
Type "help" for help.


On psql shell, run the below command to change the postgres user (Database admin password.

postgres=# \password


postgres=# \password postgres

Create PostgreSQL Database and User

First, create a database user with the below command.


Next, create a database with the below command.


Then, you can grant privileges to mydb_user on the newly created database.


Now, check whether the user has been created or not.

postgres=# \du


postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
 mydb_user |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}


Also, you can list databases to see if your database mydb has been created.

postgres=# \l


postgres=# \l
                                   List of databases
   Name    |   Owner   | Encoding |   Collate   |    Ctype    |    Access privileges
 mydb      | mydb_user | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/mydb_user          +
           |           |          |             |             | mydb_user=CTc/mydb_user
 postgres  | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres            +
           |           |          |             |             | postgres=CTc/postgres
 template1 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres            +
           |           |          |             |             | postgres=CTc/postgres
(4 rows)


Finally, log in to the mydb with the database user mydb_user.

psql -h localhost -d mydb -U mydb_user


Password for user mydb_user:
psql (13.4 (Debian 13.4-1.pgdg110+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.



That’s All. I hope you have learned how to install PostgreSQL on Debian 11 / Debian 10.

databasedebian 10debian 11postgresql
Comments (0)
Add Comment