Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if PostgreSQL is installed or not via Linux script?

I want to check in a script if PostgreSQL is installed or not on Linux and print the result. Any suggestions on how to do the check?

like image 622
Nirmal- thInk beYond Avatar asked Apr 27 '11 11:04

Nirmal- thInk beYond


People also ask

How do I find PostgreSQL version in Linux?

Type the following SQL statement within the prompt to check the current version: SELECT version(); The resulting output provides the full version and system information for the PostgreSQL server.

Is postgres installed on Linux?

Most Linux platforms such as Debian, Red Hat / CentOS, SUSE, and Ubuntu have PostgreSQL integrated with their package management. It is recommended that you install PostgreSQL this way since it ensures a proper integration with the operating system including automatic patching and other update management functionality.


1 Answers

What about trying the which command?

If you were to run which psql and Postgres is not installed there appears to be no output. You just get the terminal prompt ready to accept another command:

> which psql > 

But if Postgres is installed you'll get a response with the path to the location of the Postgres install:

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

Looking at man which there also appears to be an option that could help you out:

-s      No output, just return 0 if any of the executables are found, or         1 if none are found. 

So it seems like as long as whatever scripting language you're using can can execute a terminal command you could send which -s psql and use the return value to determine if Postgres is installed. From there you can print that result however you like.

I do have postgres installed on my machine so I run the following

> which -s psql > echo $? 0 

which tells me that the command returned 0, indicating that the Postgres executable was found on my machine.

Here's the information about using echo $?

like image 142
campo Avatar answered Sep 29 '22 03:09

campo