How to Install PostgreSQL on Debian 9

Install PostgreSQL, regularly known really as Postgres, is an open-source wellknown-reason object-relational database management device. PostgreSQL has many superior capabilities. Such as online backups Point in time recovery. Nested transactions. SQL and JSON querying, multi-model concurrency manipulate (MVCC), asynchronous replication and extra.

In this educational, we are able to show you the way to installation PostgreSQL on Debian 9 and discover the basics of simple database management.


Before proceeding with this academic, make sure the consumer you are logged in as has sudo privileges.

Installing PostgreSQL

At the time of writing this text, the ultra-modern version of PostgreSQL available from the Debian repositories is PostgreSQL version 9.6.

To set up PostgreSQL on your Debian server entire the subsequent steps:

01. Begin by using updating the nearby bundle index:

sudo apt update

Install the PostgreSQL server and PostgreSQL contrib package which presents additional capabilities for the PostgreSQL database:

sudo apt install postgresql postgresql-contrib

When the installation is completed, the PostgreSQL service will begin mechanically. To affirm the set up we’ll connect with the PostgreSQL database server the usage of the psql application and print the server version:

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

The output will look like this:


PostgreSQL 9.6.10 on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
(1 row)
Psql is an interactive terminal application that permits you to have interaction with the PostgreSQL server.

PostgreSQL Roles and Authentication Methods

PostgreSQL handles database get entry to permissions the use of the idea of roles. A role can represent a database user or a set of database users.

PostgreSQL supports a number of authentication techniques. The maximum commonly used strategies are:

  • Trust – With this approach, the function can join with out a password, as long as the standards defined within the pg_hba.conf are met.
  • Password – A position can connect by supplying a password. The passwords may be saved as scram-sha-256 md5 and password (clear-text)
  • Ident – This approach is best supported on TCP/IP connections. Works by way of obtaining the purchaser’s operating device user call, with an non-compulsory user name mapping.
  • Peer – Same as Ident however it’s miles simplest. Supported on nearby connections.

PostgreSQL consumer authentication is described in the configuration file named pg_hba.Conf. By default for nearby connections, PostgreSQL is about to apply the peer authentication method.

The postgres user is created automatically. While you set up PostgreSQL. This person is the superuser for the PostgreSQL example and it’s miles equivalent to the MySQL root consumer.

To log in to the PostgreSQL server. Because the postgres character first. You need to change to the postgres client. After which you can get proper of get right of get entry to to to a PostgreSQL set off the usage of the psql software application software program:

sudo su - postgres

From right here you may have interaction with your PostgreSQL instance. To go out out of the PostgreSQL shell kind:


You can use the sudo command to get entry to the PostgreSQL activate with out switching customers:

sudo -u postgres psql

The postgres customer is commonly used only from the close by host and it’s far recommended not to set the password for this person.

Creating PostgreSQL Role and Database

You can create new roles from the command line the use of the createuser command. Only superusers and roles with CREATEROLE privilege can create new roles.

In the subsequent example, we will create a brand new function named jonh a database named johndb and supply privileges at the database.

01. Create a brand new PostgreSQL Role

The following command will create a brand new role named john:

sudo su - postgres -c "createuser john"

02. Create a new PostgreSQL Database

Create a brand new database named johndb the use of the createdb command:

sudo su - postgres -c "createdb johndb"

03. Grant privileges

To furnish permissions to the loo user on the database we created in the previous step, connect with the PostgreSQL shell:

sudo -u postgres psql

And run the following query:

grant all privileges on database johndb to john;

Install PostgreSQL, Enable remote access to PostgreSQL server

By default the PostgreSQL, server listens most effective at the local interface To allow remote get right of entry to on your PostgreSQL server open the configuration document postgresql.conf and add listen_addresses = ‘*’ in the CONNECTIONS AND AUTHENTICATION section.

sudo vim /etc/postgresql/9.6/main/postgresql.conf


# - Connection Settings -

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

Keep the record and restart the PostgreSQL carrier with:

sudo service postgresql restart

Verify the changes with the ss utility:

ss -nlt | grep 5432
LISTEN   0         128          *
LISTEN   0         128                    [::]:5432                [::]:*

As you can see from the output above the PostgreSQL server is listening on all interfaces (0.Zero.0.0).

Install PostgreSQL, The final step is to configure the server to sincerely accept a ways flung connections by using enhancing the pg_hba.Conf report.

Below are a few examples showing different use instances:


# TYPE  DATABASE        USER            ADDRESS                 METHOD

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

# The user jane will be able to access only the janedb from all locations using a md5 password
host    janedb          jane                  md5

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


You have learned how to set up and configure PostgreSQL for your Debian 9 server. For extra records on this subject matter, consult the PostgreSQL Documentation.