January 27, 2022
how to install PostgreSQL

How To Install PostgreSQL On Linux Ubuntu 18.04 LTS

After installing PostgreSQL on CentOS 7 at the previous article, this time we will discuss how to install PostgreSQL on Linux Ubuntu 18.04 TLS. As we know so far, the PostgreSQL has been available in all Ubuntu versions by default. But, like many other development tools, it could not be the PostgreSQL latest version.

The current PostgreSQL version which was running on the system can be querying by submitting command, below :

ramans@ubuntu:~$ apt show postgresql
Package: postgresql
Version: 10+190ubuntu0.1
Priority: optional
Section: database
Source: postgresql-common (190ubuntu0.1)
Origin: Ubuntu
how to install postgresql on ubunttu 10.04 tls | version

If the version included in our Ubuntu version is different with our needs, we can use the PostgreSQL Apt Repository. This repository will integrate with our normal systems and patch management, and provide automatic updates for all supported versions of PostgreSQL throughout the support lifetime of PostgreSQL.

PostgreSQL Installation On Ubuntu 18.04 LTS

There are two sources of PostgreSQL engine, namely : Ubuntu repository and PostgreSQL repository. At this article we will use the PortgreSQL repository. The installation steps will be follow as :


The first thing we have to prepare before we begin the PostgreSQL installation, we should have a regular, non-root user with sudo privileges configured on our server. And also our system has sufficient disk size and good internet connection on our system, this is important if the installation need the newer update.

Download PostgreSQL

The next step is downloading the PostgreSQL source from PostreSQL official website, https://www.postgresql.org/download/linux/ubuntu/ and follow the all steps inside on it to define the right distribution. Our system is Ubuntu 18.04 TLS (Bionic 18.04) version. The follow instruction is as below.

ramans@ubuntu:~$ sudo vi /etc/apt/sources.list.d/pgdg.list ramans@ubuntu:~$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Install PostgreSQL on Ubuntu

Before installation begin, we have to update our Ubuntu system first by submitting command below:

ramans@ubuntu:~$ sudo apt-get update

Then submit the command below :

ramans@ubuntu:~$ sudo apt-get install postgresql-11:
Output :
Reading package lists… Done
 Building dependency tree       
 Reading state information… Done
 The following additional packages will be installed:
   libpq5 pgdg-keyring postgresql-client-11 postgresql-client-common postgresql-common
 Suggested packages:
   postgresql-doc-11 libjson-perl
 The following NEW packages will be installed:
   pgdg-keyring postgresql-11 postgresql-client-11
 The following packages will be upgraded:
   libpq5 postgresql-client-common postgresql-common
 3 upgraded, 3 newly installed, 0 to remove and 4 not upgraded.
 Need to get 15.9 MB of archives.
 After this operation, 51.9 MB of additional disk space will be used.
 Do you want to continue? [Y/n] y
 Get:1 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 libpq5 amd64 12.1-1.pgdg18.04+1 [173 kB]
 Get:2 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 pgdg-keyring all 2018.2 [10.7 kB]
 Get:3 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 postgresql-common all 210.pgdg18.04+1 [235 kB]
 Get:4 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 postgresql-client-common all 210.pgdg18.04+1 [86.4 kB]                                                                                
 Get:5 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 postgresql-client-11 amd64 11.6-1.pgdg18.04+1 [1,388 kB]                                                                              
 Get:6 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 postgresql-11 amd64 11.6-1.pgdg18.04+1 [14.0 MB]                                                                                      
 Fetched 15.9 MB in 7min 58s (33.3 kB/s)                                                                                                                                                                    
 Preconfiguring packages …
 (Reading database … 168321 files and directories currently installed.)
 Preparing to unpack …/0-libpq5_12.1-1.pgdg18.04+1_amd64.deb …
 Unpacking libpq5:amd64 (12.1-1.pgdg18.04+1) over (10.10-0ubuntu0.18.04.1) …
 Selecting previously unselected package pgdg-keyring.
 Preparing to unpack …/1-pgdg-keyring_2018.2_all.deb …
 Unpacking pgdg-keyring (2018.2) …
 Preparing to unpack …/2-postgresql-common_210.pgdg18.04+1_all.deb …
 Leaving 'diversion of /usr/bin/pg_config to /usr/bin/pg_config.libpq-dev by postgresql-common'
 Unpacking postgresql-common (210.pgdg18.04+1) over (190ubuntu0.1) …
 Preparing to unpack …/3-postgresql-client-common_210.pgdg18.04+1_all.deb …
 Unpacking postgresql-client-common (210.pgdg18.04+1) over (190ubuntu0.1) …
 Selecting previously unselected package postgresql-client-11.
 Preparing to unpack …/4-postgresql-client-11_11.6-1.pgdg18.04+1_amd64.deb …
 Unpacking postgresql-client-11 (11.6-1.pgdg18.04+1) …
 Selecting previously unselected package postgresql-11.
 Preparing to unpack …/5-postgresql-11_11.6-1.pgdg18.04+1_amd64.deb …
 Unpacking postgresql-11 (11.6-1.pgdg18.04+1) …
 Setting up libpq5:amd64 (12.1-1.pgdg18.04+1) …
 Setting up pgdg-keyring (2018.2) …
 Removing apt.postgresql.org key from trusted.gpg: OK
 Setting up postgresql-client-common (210.pgdg18.04+1) …
 Installing new version of config file /etc/postgresql-common/supported_versions …
 Setting up postgresql-common (210.pgdg18.04+1) …
 Installing new version of config file /etc/apt/apt.conf.d/01autoremove-postgresql …
 Setting up postgresql-client-11 (11.6-1.pgdg18.04+1) …
 update-alternatives: using /usr/share/postgresql/11/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode
 Setting up postgresql-11 (11.6-1.pgdg18.04+1) …
 Creating new PostgreSQL cluster 11/main …
 /usr/lib/postgresql/11/bin/initdb -D /var/lib/postgresql/11/main --auth-local peer --auth-host md5
 The files belonging to this database system will be owned by user "postgres".
 This user must also own the server process.
 The database cluster will be initialized with locale "en_US.UTF-8".
 The default database encoding has accordingly been set to "UTF8".
 The default text search configuration will be set to "english".
 Data page checksums are disabled.
 fixing permissions on existing directory /var/lib/postgresql/11/main … ok
 creating subdirectories … ok
 selecting default max_connections … 100
 selecting default shared_buffers … 128MB
 selecting default timezone … America/Los_Angeles
 selecting dynamic shared memory implementation … posix
 creating configuration files … ok
 running bootstrap script … ok
 performing post-bootstrap initialization … ok
 syncing data to disk … ok
 Success. You can now start the database server using:
 pg_ctlcluster 11 main start Ver Cluster Port Status Owner Data directory Log file 11 main 5433 down postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log update-alternatives: using /usr/share/postgresql/11/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode Processing triggers for libc-bin (2.27-3ubuntu1) … Processing triggers for systemd (237-3ubuntu10.33) … Processing triggers for man-db (2.8.3-2ubuntu0.1) … Processing triggers for ureadahead (0.100.0-21) …

Ensure the installation was completed done, then we verrify the service to check if PostgreSQL is running by executing command :

Then verify the version of newest PostgreSQL installation, by submitting command :

ramans@ubuntu:~$ apt show postgresql

Now the PostgreSQL version has been changed as follow:

Package: postgresql
 Version: 12+210.pgdg18.04+1
 Priority: optional
 Section: database
 Source: postgresql-common (210.pgdg18.04+1)
 Maintainer: Debian PostgreSQL Maintainers 
 Installed-Size: 67.6 kB
 Depends: postgresql-12
 Suggests: postgresql-doc
 Download-Size: 62.5 kB
 APT-Sources: http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 Packages
 Description: object-relational SQL database (supported version)
  This metapackage always depends on the currently supported PostgreSQL
  database server version.
  PostgreSQL is a fully featured object-relational database management
  system.  It supports a large part of the SQL standard and is designed
  to be extensible by users in many aspects.  Some of the features are:
  ACID transactions, foreign keys, views, sequences, subqueries,
  triggers, user-defined types and functions, outer joins, multiversion
  concurrency control.  Graphical user interfaces and bindings for many
  programming languages are available as well.

Configuring PostgreSQL

Now, we have checked the PostgreSQL on our Ubuntu. Trying to login to its console and explore what was inside.

ramans@ubuntu:~$ sudo su postgres
 [sudo] password for ramans:
 postgres@ubuntu:/home/ramans$ psql
 psql (11.6 (Ubuntu 11.6-1.pgdg18.04+1), server 10.10 (Ubuntu 10.10-0ubuntu0.18.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 | {}


We have install PostgreSQL on Ubuntu 18.04 system successfully. Also we have logged in to the installed database.

Share this article via :

Leave a Reply

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