Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
What do you mean by "PostgreSQL is installed"? Pgsql client? Client libraries? Pgsql server? – GreyCat Apr 27 '11 at 20:05
@GrayCat: its server – Nirmal- thInk beYond Apr 28 '11 at 4:26

4 Answers

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 status
if [ "$?" -gt "0" ]; then
  echo "Not installed".
else
  echo "Intalled"
fi
share|improve this answer
It's noteworthy that this command would only work on the Debian based family of Linux distros. – nkr1pt Apr 27 '11 at 11:34
didnt worked ,no command found, i know command service posrgres status, but how to check result – Nirmal- thInk beYond Apr 27 '11 at 11:35
I don't have it installed, so it printed out "posrgres: unrecognized service" – Draco Ater Apr 27 '11 at 11:37
@Draco Ater: it shows installed, though it is not – Nirmal- thInk beYond Apr 27 '11 at 11:43
I updated. Now try checking the exit status of service. – Draco Ater Apr 27 '11 at 11:56
show 2 more comments

There is no straightforward way to do this. All you can do is check with the package manager (rpm, dpkg) or probe some likely locations for the files you want. Or you could try to connect to a likely port (5432) and see if you get a PostgreSQL protocol response. But none of this is going to be very robust. You might want to review your requirements.

share|improve this answer
1  
Absolutely right. I have installed my PostgreSQL server from sources, under teapot user name and changed binary / service names to TeaPotSQL. No other users have permissions to read directory where it is installed. Default port is also changed. Go find it :-) – Vlad Lazarenko Apr 27 '11 at 13:35

Go to bin directory of postgres db such as /opt/postgresql/bin & run below command

[...bin]# ./psql --version

psql (PostgreSQL) 9.0.4

Here you go.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.