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

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


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 $?


We can simply write:

psql --version

output show like:

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


If it is debian based.

aptitude show postgresql | grep State

But I guess you can just try to launch it with some flag like --version, that simply prints some info and exits.

Updated using "service postgres status". Try:

service postgres statusif [ "$?" -gt "0" ]; then  echo "Not installed".else  echo "Intalled"fi