How to Install PostgreSQL on Debian 9

PostgreSQL or Postgres is a popular relational database management system. PostgreSQL is also an opensource and general-purpose database system which provides an implementation of SQL querying language. It has many advanced features like reliable transactions and concurrency without read locks. In this tutorial, you are going to learn how to install PostgreSQL on Debian 9 with basic database administration.


Before you start to install PostgreSQL on Debian 9. You must have the root user account credentials of your system.

Install PostgreSQL on Debian

To install PostgreSQL on Debian run following command.

As Debian’s official repository contains a package of PostgreSQL, we will use here, apt package manager to install Postgres on Debian.

First update apt package manager index typing following command:

sudo apt update

Now run following command to install PostgreSQL with -contrib package which adds additional features and functionalities:

sudo apt install postgresql postgresql-contrib

To check the Postgres version and confirm installation run following psql command:

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

By using psql utility you can interact with the PostgreSQL database easily.

PostgreSQL Roles and Databases

Both user and group are presented as term role in PostgreSQL for handling database access permissions. Ident, Trust, Password and Peer are different methods for authentication in PostgreSQL. Ident method mainly used on TCP/IP connection while Trust method used to connect without password using given criteria in pg_hba.conf file.

You can log in to PostgreSQL using below command:

sudo su - postgres

To exit from here type following in the terminal:


Create PostgreSQL Database and Role

By using createdb method you can create a database in Postgres and by using createuser method you can create a new role.

To create new database called test_db run following command:

sudo su - postgres -c "createdb test_db"

Now create a new role called test_user run below command:

sudo su - postgres -c "createuser test_user"

Now you should grant permission to the user test_user for the newly created database test_db you should execute a query in PostgreSQL Shell to so run following command:

sudo -u postgres psql

Execute the following query to grant permission:

grant all privileges on database test_db to test_user;

Open PostgreSQL Prompt with New Role

To open the PostgreSQl prompt with new role you should have the same Linux user available as PostgreSQL user and database.

To create new Linux user in Debian named test_user if the matching user is not available by running following command:

sudo adduser test_user

After creating matching user account on the Linux system, you can connect to the database running following command:

sudo -u sammy psql

Enabling Remote Access to PostgreSQL server

Use the following steps to enable remote access to the PostgreSQL server.

Edit /etc/postgresql/10/main/postgresql.conf file and place listen_addresses = '*' in the CONNECTIONS AND AUTHENTICATION SECTION to do so run following command:

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

Update listen_addresses like given below:


# - Connection Settings -

listen_addresses = '*'     

Now restart the PostgreSQL service with systemctl command:

sudo systemctl restart postgresql

Now confirm and verify the changes typing following in terminal:

ss -nlt | grep 5432

The output should be:

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

Finally update pg_hba.conf file to configure server remote connections using below examples:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# The user test_user will be able access all databases from all locations using a md5 password
host    all             test_user                  md5

# The user test_user will be able access only the test_db from all locations using a md5 password
host    test_db          test_user                  md5

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


You have successfully learned how to install PostgreSQL on Debian 9. If you have any queries please don’t forget to comment below.