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.

Prerequisites

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:

output

version                                                  
-----------------------------------------------------------------------------------------------------------
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
psql

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

\q

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 127.0.0.1. 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
      /etc/postgresql/9.6/main/postgresql.conf

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

# - 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                 0.0.0.0:5432             0.0.0.0:*
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:

       /etc/postgresql/9.6/main/pg_hba.conf

# 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            0.0.0.0/0                md5

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

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

Conclusion

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.