• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Install PostgreSQL on Debian 11

Written by Admin, Updated On January 19, 2023
database, debian, postgresql
How to Install PostgreSQL on Debian 11

PostgreSQL or Postgres is an open source relational database management system. It is a popular and has many advanced features like reliable transactions and concurrency without read locks. In this tutorial, you will learn how to install PostgreSQL on Debian 11 Bullseye.

How Install PostgreSQL on Debian#

Follow the below given steps to install PostgreSQL on Debian 11:

Step 1 – Prerequisites#

Before you start installation on your system, you should have non-root user account with sudo privileges.

We will install PostgreSQL from the official Debian repositories.

Step 2 – Update Package Index#

At first you need to update the package index list by typing:

sudo apt update -y
sudo apt upgrade

Step 3 – Install PostgreSQL#

After that, install PostgreSQL with PostgreSQL contrib package for several additional features using below command:

sudo apt install postgresql postgresql-contrib

Step 4 – Verify Installation#

Once the installation is completed, PostgreSQL service will start automatically. You can verify installation using below command:

sudo -u postgres psql -c "SELECT version();"

It will show output as given below:

PostgreSQL 15.1 on x86_64-pc-linux-gnu, compiled by gcc (Debian 11.0.0-18+deb9u1) 11.0.0 20170516, 64-bit

PostgreSQL Roles and Authentication Methods#

A role can represent a database user or a group of database users. There are multiple authentication methods in PostgreSQL and commonly used are Trust, Ident, Password and Peer.

  • Trust – This method used to connect without password using given criteria in pg_hba.conf file.
  • Ident – This method is mainly used on TCP/IP connection. It is obtaining client’s operating system user name, etc. details.
  • Password – A role can connect by providing a password.
  • Peer – It’s same as Ident but it is only supported on local connections.

You can login to PostgreSQL but as postgres user first you need to switch to the postgres user and then you can access a PostgreSQL prompt using the psql utility:

sudo su - postgres
psql

Now here you can interact with your PostgreSQL instance. To get exit of PostgreSQL prompt type:

\q

Create PostgreSQL Role and Database#

You can use createuser command to create new roles from the command line. You can only create if you are superuser or have roles with CREATEROLE privileges. By using createdb method you can create a database in Postgres.

Use the below command to create a new role:

sudo su - postgres -c "createuser demouser"

To create a new database use the following command:

sudo su - postgres -c "createdb demodb"

Now, you should grant permission to user demouser for newly created demodb database. So run below command to connect PostgreSQL shell:

sudo -u postgres psql

Next execute following command which will grant permissions:

grant all privileges on database demodb to demouser;

Enabling Remote Access to PostgreSQL server#

By default, the PostgreSQL server listens only on the local interface 127.0.0.1. You should edit configuration file /etc/postgresql/10/main/postgresql.conf and add listen_addresses = '*' in the CONNECTIONS AND AUTHENTICATION section to enable remote access to your PostgreSQL server.

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

Update listen_addresses like given below:

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*'     # what IP address(es) to listen on;

Save the file and restart the PostgreSQL service using systemctl command:

sudo service postgresql restart

Now confirm and verify the changes typing following in terminal:

ss -nlt | grep 5432

It will show output as below:

LISTEN   0         128                 0.0.0.0:5432             0.0.0.0:*
LISTEN   0         128                    [::]:5432                [::]:*

At last, update pg_hba.conf file to configure the server to accept remote connections:

LISTEN   0         128                 0.0.0.0:5432             0.0.0.0:*
LISTEN   0         128                    [::]:5432                [::]:*

At last, update pg_hba.conf file to configure the server to accept remote connections:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# The user demouser will be able access all databases from all locations using a md5 password
host    all             demouser            0.0.0.0/0                md5

# The user demouser will be able access only the demodb from all locations using a md5 password
host    demodb          demouser            0.0.0.0/0                md5

# The user demouser will be able access all databases from a trusted location (192.168.43.92) without a password
host    all             demouser            192.168.43.92            trust

Conclusion#

You learned successfully how to install and configure PostgreSQL on your Debian 11 machine. If you would like to get more details you can visit PostgreSQL 15.1 Documentation.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install Python on Ubuntu 22.04
Next Article   How to Install PHP 8.2 on Debian 11 Linux

Related Posts

  • How to Install WordPress with Nginx on Debian 11

    How to Install WordPress with Nginx on Debian 11

    March 22, 2023
  • How to Install Mongodb on Ubuntu 22.04

    How to Install MongoDB on Ubuntu 22.04

    March 12, 2023
  • How to Install and Use Docker on Debian 11

    How to Install and Use Docker on Debian 11

    March 10, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook