In this guide, we are going to look at how to install PostgreSQL 13 on Kali Linux 2021.x. Let’s first get to know what PostgreSQL is before we start on installation.

What is PostgreSQL?

PostgreSQL is an enterprise-class and open-source relational database, advanced to support both relational queries (SQL) and non-relational (JSON) queries. PostgreSQL supports a higher number of data types that are not found in MySQL and it is also possible for one to create own data types, index types and operators. It is community-driven, having more than 30 years of community support thus making it highly resilient and stable with notable levels of integrity. This helps developers in building fault-tolerant environments as well as protect information integrity. PostgreSQL has formed the primary database for web, mobile, analytics and geospatial applications. Due to its performance optimization features and advanced data types, PostgreSQL competes with the highly valued commercial databases such as OracleDB.

Features of PostgreSQL

Some of the important features of PostgreSQL include:

  • Support for different data types as well as user-defined types
  • Table inheritance
  • Ensures data integrity – foreign key referential intergrity
  • High levels of security
  • Nested transactions
  • Reliability and disaster recovery – supports asynchronous replication and point-in-time recovery
  • Extensibility
  • Sophisticated locking mechanism

Installing PostgreSQL 13 on Kali Linux 2021.x

The below steps will take us through how to install PostgreSQL on Kali Linux and to test how to create a database. Kali Linux is a Debian-based distribution and therefore we are going to be using ‘apt’ package manager.

Step 1: Update system packages

The first recommended thing to do always is to ensure that our systems run the latest packages before the installations. Once updated, reboot the system to enable the new changes to take effect.

sudo apt-get update
sudo apt-get upgrade
sudo reboot

Step 2: Install PostgreSQL 13 on Kali Linux

Postgresql 13 is already included in Kali Linux 2021.x. Check the availability of PostgreSQL in Kali Linux by running the below command:

$ sudo apt show postgresql                           
Package: postgresql
Version: 13+225
Priority: optional
Section: database
Source: postgresql-common (225)
Maintainer: Debian PostgreSQL Maintainers <[email protected]>
Installed-Size: 69.6 kB
Depends: postgresql-13
Suggests: postgresql-doc
Tag: devel::lang:sql, interface::daemon, network::server, network::service,
 role::metapackage, role::program, suite::postgresql, works-with::db
Download-Size: 64.7 kB
APT-Manual-Installed: no
APT-Sources: http://http.kali.org/kali kali-rolling/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.

If you need to install run the command as below:

sudo apt install postgresql -y

You can go ahead to start postgresql in Kali Linux using any of the following commands:

sudo /etc/init.d/postgresql start
or
sudo systemctl start postgresql

Confirm if running

$ sudo systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; disabled; vendo>
     Active: active (exited) since Tue 2021-07-06 12:40:14 EDT; 3min 53s ago
    Process: 42573 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 42573 (code=exited, status=0/SUCCESS)
        CPU: 5ms

Jul 06 12:40:14 kali systemd[1]: Starting PostgreSQL RDBMS...
Jul 06 12:40:14 kali systemd[1]: Finished PstgreSQL RDBMS.

Step 3: Test PostgreSQL Installation on Kali Linux

Check if you can connect to PostgreSQL to create a test database. When PostgreSQL is installed, a default user called ‘postgres’ is created. Change to this user

$ sudo su - postgres

To secure the default postgres user with a strong password, use the following command:

psql -c "alter user postgres with password 'MyStrongPassword'"

To run Postgresql commands, let’s enter PostgreSQL command prompt with the below command:

$ psql
psql (13.2 (Debian 13.2-1))
Type "help" for help.

postgres=# 

To list any available databases, use the command below:

\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)

To create a database, run the below command:

CREATE DATABASE testdb;

Confirm database was created

# \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
 testdb    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)

To create a user and grant them privileges to the database we created earlier, run the following commands:

postgres=# CREATE USER testuser WITH ENCRYPTED PASSWORD 'MyPassword';
CREATE ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE testdb to testuser;
GRANT

To connect to the database you just created, use the command below:

# \c testdb
You are now connected to database "testdb" as user "postgres".
testdb=# 

Step 4: Configure PostgreSQL Remote Access

PostgreSQL has configurations file that you can make changes to when needed. I am going to show you how to edit listening addresses and enable remote access. Open the configuration file as below:

sudo vim /etc/postgresql/13/main/postgresql.conf 

Under ‘Connection and Authentication’, uncomment line 59; #listen_addresses=’localhost’ and edit the same line as below to either allow to listen on any interface or on specific interfaces:

# To listen on all interfaces
listen_addresses = '*'

# To enable only specified private IP address
listen_addresses = '192.168.50.2'

To enable remote connections edit the below file:

sudo vim /etc/postgresql/13/main/pg_hba.conf

Make the below changes:

# To accept connections from anywhere
host all all 0.0.0.0/0 md5

# To accept only from trusted subnet
host all all 10.10.10.0/24 md5

Once you make the above changes, you need to restart postgresql as below

sudo systemctl restart postgresql

To disable remote access for Postgresql database, open the above configuration files and add a ‘#‘ at the beginning of the lines to disable. For example, to disable remote access from any, edit the line to look like below and remember to restart postgresql when done.

#host all all 0.0.0.0/0 md5

This has been a guide on how to install Postgresql database on Kali Linux. As explained earlier, Postgresql is one of the most used databases due to its stability and high performance. It is pretty easy to get Postgresql database up and running on any Linux system and its commands are quite simple as well.

Best On-demand video courses to Learn PostgreSQL Database:

I hope the guide has been helpful in getting you started with Postgresql database on Kali Linux. Check below more interesting guides:

LEAVE A REPLY

Please enter your comment!
Please enter your name here