How to install PostgreSQL on Ubuntu 21.04

To Install PostgreSQL on Ubuntu 21.04

Introduction

Open-source PostgreSQL is a powerful, object-relational database, known for its reliability and performance. There are a variety of features that make it easier for developers and administrators to deploy data-driven applications.

Installation Procedure:

Step 1: Checking the installed OS version by using the following command

linuxhelp@linuxhelp:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 2: Log in as Root user by using the following command

linuxhelp@linuxhelp:~$ sudo -s
[sudo] password for linuxhelp: 

Step 3: Change to the home directory by using the following command

root@linuxhelp:/home/linuxhelp# cd ~

Step 4: Check the postgresql availability in apt repository

root@linuxhelp:~# apt show postgresql
Package: postgresql
Version: 13+225ubuntu1
Priority: optional
Section: database
Source: postgresql-common (225ubuntu1)
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian PostgreSQL Maintainers <team+postgresql@tracker.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 69.6 kB
Depends: postgresql-13
Suggests: postgresql-doc
Task: postgresql-server
Download-Size: 4,244 B
APT-Sources: http://in.archive.ubuntu.com/ubuntu hirsute/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.

Step 5: Install postgresql the by using the following command

root@linuxhelp:~# apt install postgresql postgresql-contrib
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libpq5 postgresql-13 postgresql-client-13 postgresql-client-common postgresql-common sysstat
Suggested packages:
  postgresql-doc postgresql-doc-13 libjson-perl isag
The following NEW packages will be installed:
  libpq5 postgresql postgresql-13 postgresql-client-13 postgresql-client-common postgresql-common postgresql-contrib
  sysstat
0 upgraded, 8 newly installed, 0 to remove and 263 not upgraded.
Need to get 16.0 MB of archives.
After this operation, 48.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 libpq5 amd64 13.4-0ubuntu0.21.04.1 [116 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu hirsute/main amd64 postgresql-client-common all 225ubuntu1 [28.7 kB]

Setting up sysstat (12.5.2-2) ...

Creating config file /etc/default/sysstat with new version
update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) in auto mode
Created symlink /etc/systemd/system/sysstat.service.wants/sysstat-collect.timer → /lib/systemd/system/sysstat-collect.
timer.
Created symlink /etc/systemd/system/sysstat.service.wants/sysstat-summary.timer → /lib/systemd/system/sysstat-summary.
timer.
Created symlink /etc/systemd/system/multi-user.target.wants/sysstat.service → /lib/systemd/system/sysstat.service.
Setting up postgresql-contrib (13+225ubuntu1) ...
Processing triggers for man-db (2.9.4-2) ...
Processing triggers for libc-bin (2.33-0ubuntu5) ...

Step 6: Logon to postgres user by using the following command

root@linuxhelp:~# su - postgres

Step 7: Open the postgres by using the following command

postgres@linuxhelp:~$ psql
psql (13.4 (Ubuntu 13.4-0ubuntu0.21.04.1))
Type "help" for help.

Step 8: Create a database named “sample_db” by using the following command

postgres=# CREATE DATABASE sample_db;
CREATE DATABASE

Step 9: Create a user named “sample_ user” with password

postgres=# create user sample_user with encrypted password '1234';
CREATE ROLE

Step 10: Grant all privileges for “sample_ user” user to “sample_db” database

postgres=# grant all privileges on database sample_db to sample_user;
GRANT

Step 11: Exit from the Database by using the following command

postgres=# exit 

With this installation of PostgreSQL on ubuntu comes to end.

FAQ
Q
How do I log in as a Postgres user?
A
Log on to Postgres user by using the following command "sudo -u postgres"
Q
What is the default Postgres user?
A
The default Postgres user is "postgres"
Q
Differentiate PostgreSQL and MySQL?
A
Postgres is an object-relational database, while MySQL is a purely relational database.
Q
How to list Postgres users?
A
Use \du or \du+ COMMAND to list Postgres users.
Q
How to delete the Postgres database?
A
Use the DROP DATABASE command to delete a not-needed database.