How to Install and Configure Postgres 14 on Debian 11

In this guide we are going to install Postgresql 14 in Debian 11.

Postgresql is an open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance. Postgres, is a free and open-source relational database management system emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley. PostgreSQL is used as the primary data store or data warehouse for many web, mobile, geospatial, and analytics applications. PostgreSQL can store structured and unstructured data in a single product.

Check these also:

# Prerequisites

To follow along, ensure you have the following:

  1. Debian 11 Server
  2. Root access to the server or user with root access
  3. Internet access from the server
  4. Basic knowledge of Linux terminal

# Table of contents

  1. Ensure the server is up to date
  2. Installing and starting Postgres Server
  3. PostgreSQL Roles and Databases Authentication Methods
  4. Connecting to postgres database
  5. Configuring postgres 14 instance for remote access
  6. User management

# 1. Ensure that your system packages are up to date

Letā€™s refresh your serverā€™s local package index using this command:

sudo apt update

Then we upgrade the packages in our system with this:

sudo apt -y upgrade

Let us also install necessary utility packages that we will use

sudo apt -y install gnupg2 wget

# 2. Installing Postgres 14

Postgres is provided in the default Debian repositories. To check the version provides, use this command:

sudo apt-cache search postgresql | grep postgresql

The default packages provided by the default repositories are postgres 12. If you are interested in Postgres 12 you can install with the following commands. TheĀ -contribĀ package that adds some additional utilities and functionality:

sudo apt install postgresql postgresql-contrib

Since we are looking to install Postgres 14, we will need to add an additional repository that provide the packages. Create the file repository configuration

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Then import the repository signing key:

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Now letā€™s update the package lists for the new repos to reflect:

sudo apt -y update

Then finally we install the latest version of Postgres:

sudo apt -y install postgresql

Once the installation is successful, Postgres 14 will be started.

Use this command to check the service status:

$ sudo systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Mon 2021-10-18 04:33:26 UTC; 33s ago
    Process: 4788 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 4788 (code=exited, status=0/SUCCESS)
        CPU: 877us

Oct 18 04:33:26 ip-10-2-40-246 systemd[1]: Starting PostgreSQL RDBMS...
Oct 18 04:33:26 ip-10-2-40-246 systemd[1]: Finished PostgreSQL RDBMS.

Next, letā€™s erify the installation by connecting to the PostgreSQL database server and checking its version. Use this command:

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

Output

$ sudo -u postgres psql -c "SELECT version();"
                                                           version
-----------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 14.0 (Debian 14.0-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
(1 row)

# 3. PostgreSQL Roles and Databases Authentication Methods

PostgreSQL uses a concept calledĀ rolesĀ to handle client authentication and authorization. By default, Postgres is set up to useĀ ident authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account. If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.

The installation procedure created a user account calledĀ postgres that is associated with the defaultĀ postgresĀ role. In order to use PostgreSQL, you can log in to that account.

PostgreSQL supports multiple authentication methods . The most commonly-used methods are:

  • TrustĀ ā€“ A role can connect without a password, as long as the conditions defined in theĀ pg_hba.confĀ are met.
  • PasswordĀ ā€“ A role can connect by providing a password. The passwords can be stored asĀ scram-sha-256,Ā md5, andĀ passwordĀ (clear-text).
  • IdentĀ ā€“ Only supported on TCP/IP connections. It works by obtaining the clientā€™s operating system user name, with an optional user name mapping.
  • PeerĀ ā€“ Same as Ident, but it is supported on local connections only.

# 4. Connecting to postgres database

  1. By Switching toĀ postresĀ user

Switch over to the postgres account on your server by typing this in the terminal;:

sudo -i -u postgres

You can now access a Postgres prompt immediately by typing:

$ psql
psql (14.0 (Debian 14.0-1.pgdg110+1))
Type "help" for help.

postgres=#

This will log you into the PostgreSQL prompt, and from here you are free to interact with the database management system right away.

  1. By running the command asĀ postgresĀ user

Use this to run the command directly as theĀ postgresĀ user using sudo

sudo -u postgres psql

Output:

$ sudo -u postgres psql
psql (14.0 (Debian 14.0-1.pgdg110+1))
Type "help" for help.

postgres=#

# 5. Configuring postgres 14 instance for remote access

To achieve this, we will modify postgres configuration files. We need to open the files and adjust the configs are required. The main configuration file for Postgresql 14 can be found in this pathĀ /etc/postgresql/14/main/pg_hba.conf

Letā€™s change peer identification to trust:

sed -i '/^local/s/peer/trust/' /etc/postgresql/14/main/pg_hba.conf

Change ident identification to md5 to allow password login.

sed -i '/^host/s/ident/md5/' /etc/postgresql/14/main/pg_hba.conf

Add a block to allow access from everywhere:

Add this content to the fileĀ /etc/postgresql/14/main/pg_hba.conf

host    all             all             0.0.0.0/0                md5

Ensure PostgreSQL is listening on *

Add this line to the config hereĀ /etc/postgresql/14/main/postgresql.conf

listen_addresses='*'

Enable and restart postgresql server to reload the configs

sudo systemctl restart postgresql
sudo systemctl enable postgresql

# 6. User management

# Creating Superuser

Now that everything is set up, let us create a super user.
Connect to the DB as postres role:

# sudo -u postgres psql
psql (14.0 (Debian 14.0-1.pgdg110+1))
Type "help" for help.

postgres=#

Create super user with nameĀ root:

CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'passwordhere';

Output:

# CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'passwordhere';
CREATE ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 root      | Superuser, Create role, Create DB                          | {}

postgres=#

# Managing Application Users

Use this to create a database, create a user and grant that user all accesss to that database:

create database app_db_name;
create user app_user with encrypted password 'dbpassword';
grant all privileges on database app_db_name to app_user;

Checkout this comprehensive guide on user and permission management in postgresĀ here.

# Connecting to the instance from remote host

Use this command to connect to the postgres instance from local machine:

psql 'postgres://<username>:<password>@<host>:<port>/<db>?sslmode=disable'

# like
psql 'postgres://root:passwordhere@192.160.1.20:5432/postgres?sslmode=disable'

# Conclusion

Up to this point we have managed to install Postgresql 14 on an Debian 11 server, do some basic configurations then do basic user management.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy