How to Install and Configure Postgres 13 Ubuntu 20.04

In this guide we are going to install Postgresql 13 in Ubuntu 20.04

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.

Also Check:

# Prerequisites

To follow along, ensure you have the following:

  1. Ubuntu 20.04 server
  2. Root access to the server or user with root access
  3. Internet access from the server
  4. Knowledge of Linux terminal

# Table of Content

  1. Ensure your system is up to date
  2. Installing Postgres 13
  3. PostgreSQL Roles and Databases Authentication Methods
  4. Connecting to postgres database
  5. Configuring postgres instance for remote access
  6. User management
  7. Connecting to the instance from remote host


  1. Ensure your system is up to date {.wp-block-heading}

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

# 2. Installing Postgres 13

Postgres is provided in the default Ubuntu 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 13, we will need to add an additional repository that provide the packages.

Create the file repository configuration using this command:

sudo sh -c 'echo "deb $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Then import the repository signing key:

wget --quiet -O - | sudo apt-key add -

Now let’s update the package lists:

sudo apt-get update

Then we install the specific version of postgres we want. Use postgresql-13 instead of postgresql:

sudo apt-get -y install postgresql-13

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

Use this command to check the service status:

# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Fri 2021-08-06 17:59:37 UTC; 1min 25s ago
   Main PID: 25741 (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 9257)
     CGroup: /system.slice/postgresql.service

Aug 06 17:59:37 frhb64566ds systemd[1]: Starting PostgreSQL RDBMS...
Aug 06 17:59:37 frhb64566ds systemd[1]: Finished PostgreSQL RDBMS.

Next, let’s verify the installation by connecting to the PostgreSQL database server and checking its version. Use this command:

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


 PostgreSQL 13.3 (Ubuntu 13.3-1.pgdg20.04+1) on aarch64-unknown-linux-gnu, compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 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:

sudo -i -u postgres

You can now access a Postgres prompt immediately by typing:

$ psql
psql (13.3)
Type "help" for help.


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

# 5. Configuring postgres instance for remote access

By default postgres is only set up to be accessed locally. This is not ideal if we want to access our server from another host. In this section we are going to configure postgres to allow access from remote hosts.

Postgres 13 configuration file can be found in this path/etc/postgresql/13/main/pg_hba.conf

# Change peer identification to trust

Use this command to change the

<meta charset="utf-8">peer to <meta charset="utf-8">trust

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

# Change ident identification to md5

To allow password login, change <meta charset="utf-8">ident to

md5 for each host config

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

# Add a block to allow access from everywhere

Add this content to the file /etc/postgresql/13/main/pg_hba.conf to allow all hosts password access:

host    all             all                   md5

# Ensure PostgreSQL is listening on *

Add this line to the config here /etc/postgresql/13/main/postgresql.conf to allow postgres to listen on all hosts


To apply the configurations, we need to restart the postgres 13 service.

Enable and restart postgresql server to reload the configs:

sudo systemctl restart postgresql
sudo systemctl enable postgresql

# 6. User management

# Create Super user:

It would be better if we created a super user to administer the postgres service. This is that one user that has permissions to manage other users and databases.

Connect to the DB as postres role

$ sudo -u postgres psql
psql (13.3 (Ubuntu 13.3-1.pgdg20.04+1))
Type "help" for help.


Create super user with name root using this command:


Check the user if its been created and granted necessary privileges:

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                          | {} ```

# 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.

# 7. Connecting to the instance from remote host

Use this command to test that you can connect:

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

# like
psql 'postgres://root:passwordhere@'

# Conclusion

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

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