October 21, 2021
How to install PostgreSQL 13 on Ubuntu 20.04 LTS

How To Install PostgreSQL 13 on Ubuntu 20.04 LTS

On this tutorial we will learn how to install PosrgreSQL 13.4 (the last stable version at the time this article was written) on Ubuntu 20.04 LTS operating system.

Introduction

PostgreSQL database has been widely used in various critical applications, ranging from e-commerce, finance, energy, education and many more. PostgreSQL is a powerful, 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. On this tutorial we will learn how to install PostgreSQL version 13.4 on Ubuntu 20.04 LTS operating system.



PostgreSQL 13 Installation On Ubuntu 20.04 LTS

The source of PostgreSQL is located on PostgreSQL official website. We will use the source from this site.

Prerequisites

Before we are starting PostgreSQL 13 installation, there are will be prerequisites to be fulfill, as mention below :

  • Ubuntu 20.04 LTS System with sufficient disk space
  • root or non-root user with administrative privileges
  • firewall configured with firewalld

The PostgreSQL installaton will be consist of several steps :

  1. Adding PostgreSQL Repository To Ubuntu 20.04
  2. Install PostgreSQL 13 on Ubuntu 20.04
  3. Set PostgreSQL admin user’s password
  4. Enabling Remote Access

1. Adding PostgreSQL Repository To Ubuntu 20.04

The PostgreSQL repository will be integrated with our normal systems and patch management and provide automatic updates for all supported versions of PostgreSQL throughout the support lifetime of PostgreSQL. On this step, we will create the file repository configuration, by typing :

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, by typing :

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

Then update the package lists, by typing :

sudo apt-get update
mpik@app01:~$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
mpik@app01:~$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
OK
mpik@app01:~$ sudo apt-get update
Get:1 http://apt.postgresql.org/pub/repos/apt focal-pgdg InRelease [86.7 kB]                                                                  
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]                                                                     
Hit:3 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease                                                                              
Hit:5 http://us.archive.ubuntu.com/ubuntu focal InRelease                                                                                     
Hit:6 http://ppa.launchpad.net/uroni/urbackup/ubuntu focal InRelease                                                                          
Get:7 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]              
...                                     
Get:20 http://us.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [944 B]                                             
Get:21 http://us.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [10.4 kB]                                           
Fetched 3,978 kB in 28s (143 kB/s)                                                                                                            
Reading package lists... Done

2. Install PostgreSQL 13.4

After all are set, the we will install the latest PostgreSQL (PostgreSQL version 13.4), by typing :

sudo apt-get -y install postgresql
mpik@app01:~$ sudo apt-get -y install postgresql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libevent-core-2.1-7 libevent-pthreads-2.1-7 libmecab2 libzip5 linux-headers-5.4.0-26 linux-headers-5.4.0-26-generic
  linux-image-5.4.0-26-generic linux-modules-5.4.0-26-generic linux-modules-extra-5.4.0-26-generic mecab-ipadic mecab-ipadic-utf8 mecab-utils
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libcommon-sense-perl libjson-perl libjson-xs-perl libpq5 libtypes-serialiser-perl pgdg-keyring postgresql-13 postgresql-client-13
  postgresql-client-common postgresql-common sysstat
Suggested packages:
  postgresql-doc postgresql-doc-13 isag
The following NEW packages will be installed:
  libcommon-sense-perl libjson-perl libjson-xs-perl libpq5 libtypes-serialiser-perl pgdg-keyring postgresql postgresql-13
  postgresql-client-13 postgresql-client-common postgresql-common sysstat
0 upgraded, 12 newly installed, 0 to remove and 402 not upgraded.

By default, after installation the PostgreSQL service has been enabled and running. Then we will verify it by typing command line :

sudo systemctl status postgresql
mpik@app01:~$ sudo systemctl status postgresql
[sudo] password for mpik: 
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Fri 2021-09-17 21:41:34 PDT; 6min ago
   Main PID: 6430 (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 9436)
     Memory: 0B
     CGroup: /system.slice/postgresql.service

Sep 17 21:41:34 app01.otodiginet.com systemd[1]: Starting PostgreSQL RDBMS...
Sep 17 21:41:34 app01.otodiginet.com systemd[1]: Finished PostgreSQL RDBMS.
PostgreSQL 13 service
PostgreSQL 13 service

Verifying PostgreSQL Version

We can find out the PostgreSQL version installed on our system in various ways:

PostgreSQL client version

Submitting command line from shell console : psql -V

mpik@app01:/usr/lib/postgresql/13/bin$ psql -V
psql (PostgreSQL) 13.4 (Ubuntu 13.4-1.pgdg20.04+1)



PostgreSQL server version

We will use the query by entering sql script, after logging into the database.

mpik@app01:~$ sudo su postgres
postgres@app01:/home/mpik$ psql
psql (13.4 (Ubuntu 13.4-1.pgdg20.04+1))
Type "help" for help.

postgres=# SELECT version();
                                                             version                                                              
----------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 13.4 (Ubuntu 13.4-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit
(1 row)

postgres=# SHOW server_version;
          server_version          
----------------------------------
 13.4 (Ubuntu 13.4-1.pgdg20.04+1)
(1 row)

3. Set PostgreSQL admin user’s password

For administrative easiness, we have to set the admin user’s password. For this purpose, we have to login to database and setting the password. On this section, we will list the database and existing user on the PostgreSQL database.

mpik@app01:~$ sudo su postgres
[sudo] password for mpik: 
postgres@app01:/home/mpik$ psql
psql (13.4 (Ubuntu 13.4-1.pgdg20.04+1))
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# alter user postgres with password 'Otodiginet2021';
ALTER ROLE
PostgreSQL 13 setting postgres password
PostgreSQL 13 setting postgres password

Conclusion

At the end, we have shwon you, how to install PostgreSQL 13.14 on Ubuntu 20.04 LTS operating system.. I hope this article will be useful for anyone who is finding PostgreSQL installation guidance.

Share this article via :

One thought on “How To Install PostgreSQL 13 on Ubuntu 20.04 LTS

Leave a Reply

Your email address will not be published. Required fields are marked *