How to Check if PostgreSQL Is Installed on Linux

Bilal Shahid Feb 02, 2024
How to Check if PostgreSQL Is Installed on Linux

Are you searching for PostgreSQL on your Linux, wondering if it is installed or not? This article is specially articulated to help you to find whether PostgreSQL was installed on your Linux or not using simple commands.

In addition to checking if PostgreSQL is installed on your laptop or not, you can look for the path of PostgreSQL if it is installed by running some different commands on Ubuntu.

Follow this article to find different ways to check the installation status of PostgreSQL on Linux.

Different Ways to Check if PostgreSQL Is Installed on Linux

Numerous commands are available that you can run on Ubuntu and search for PostgreSQL. In addition, you can use another command that returns its path on your Linux if installed. Let’s discuss the different BASH commands in detail below.

the which Command

The which command is a BASH command that you can run on Ubuntu. It returns nothing if PostgreSQL is not installed on your Linux. In this case, the terminal gets ready to accept another command, so you can quickly figure out that PostgreSQL is not installed. Here is a depiction of this scenario:

> which psql
> 

If PostgreSQL is installed, the which command will return the path where PostgreSQL is located on your Linux. Hence, the which command is quite useful to get the path of PostgreSQL if installed on your Linux. You can expect an output that is similar to the one shown below:

> which psql
/opt/boxen/homebrew/bin/psql

the -s Extension in the which Command

If you search for the man which command on the terminal, you will find many different options and extensions for the which command. It is good to go through the manual and choose the best option that suits your requirement.

For this article, the which command works excellently. Adding the -s extension to the command alters the return type only, which are as follows:

  • If any executables are found, 0 is returned
  • If the PostgreSQL executable is not found, 1 is returned

Here is a demonstration of the command:

> which -s psql
> echo $?
0

The BASH command above returns 0. It highlights that PostgreSQL is installed on your Linux.

The commands work by first executing the which -s command. Then, after executing the first command successfully, the echo $ command is used to get the return value from the first command.

Note: The echo $ command is used to show the return value of the last executed command, which in this case is the which -s command.

the psql Command

An alternative to the which command is the psql command. This BASH command returns the version of the PostgreSQL if used in the following manner:

psql --version

If you use the psql command written above, you will receive an output similar to the one given below:

psql (PostgreSQL) 11.5 (Ubuntu 11.5-1.pgdg18.04+1)

The 11.5 value in the output tells the version of the PostgreSQL that has been installed on your Linux.

the -V Extension in the psql Command

An extension that we can add to the simple psql command is the -V extension (capital “V”). It returns the version of the PostgreSQL that is installed on your Linux. However, it works differently.

To use the psql command with the -V extension, you first have to run the which psql command. After you receive the output from the which psql command, you will run the psql -V command to get the version of the PostgreSQL installed.

Following is a demonstration of the psql -V command:

> which psql
/opt/boxen/homebrew/bin/psql
> psql -V
psql (PostgreSQL) 11.5

The psql -V command is another alternative that you can use to check whether PostgreSQL has been installed or not. In addition, it returns the version of PostgreSQL if it has been installed on your Linux.

Techniques Excluding the BASH Commands

BASH commands can help you locate PostgreSQL if installed on your Linux. However, if you cannot use the BASH commands for any reason, here are some methods you can try to check for PostgreSQL’s installation.

Method 1: Connect to Port 5432

The first method to check for the installation of the PostgreSQL is to connect to the 5432 port and monitor the response you receive from it.

If you receive a PostgreSQL protocol response, we can assume that PostgreSQL is installed and working on your Linux. The reason for monitoring the response on port 5432 is that PostgreSQL uses this port.

By monitoring the response from this port, you can identify if PostgreSQL is installed on your Linux or not.

Method 2: Check Installation Folder

The second method to check for the installation of PostgreSQL is to check the folder with a path similar to /etc/postgresql/.

The path must contain one or more subfolders in it. If you cannot find subfolders in the path or the entire path itself, this indicates that PostgreSQL is not installed on your Linux.

On the other hand, if you find one subfolder in the path, for example, /etc/postgresql/11.5, this explains that PostgreSQL with 11.5 version is installed on your Linux.

If you find multiple subfolders in the path /etc/postgresql/, various versions of PostgreSQL have been installed on your Linux.

So, there are numerous methods to check if PostgreSQL is installed on your Linux or not. The BASH commands are tested for Linux and provide the required results. The other techniques mentioned also work well.

Finally, it’s up to you to follow any method that suits you to check for the installation status of PostgreSQL. Again, alternative approaches are provided to help you if one way fails to work.

Author: Bilal Shahid
Bilal Shahid avatar Bilal Shahid avatar

Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!

GitHub

Related Article - PostgreSQL Installation