105

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?

2
  • What do you mean by "PostgreSQL is installed"? Pgsql client? Client libraries? Pgsql server? Commented Apr 27, 2011 at 20:05
  • See also these generic BASH commands: stackoverflow.com/a/677212/1736679 Commented Jun 5, 2018 at 1:23

15 Answers 15

144

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

Sign up to request clarification or add additional context in comments.

5 Comments

The only option which has for me is -a (print all matching pathnames of each argument), resulting in an illegal option if I use -s.
Hey @Dennis I'm not sure what your motivations are, but I think in the spirit of my original response, you could probably do something using the -a flag. If there is no output then you can assume it is not installed, and if there is output you know that it is (and where) it's installed because you get the path back. Let me know if that's helpful or if you have any other thoughts.
The problem with which is that it varies a lot over different operating systems. But you're right that the -a flag can be used, you can even use no flag at all: if which foo >/dev/null; then echo exists; fi. Another problem is that this will only search your PATH.
which psql will not tell you if the Postgres server is installed. It's possible to install the client package (psql) without the server
mmmmmm, manwich
43

We can simply write:

psql --version

output show like:

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

1 Comment

Which will only tell you if psql is installed, not if the server is installed (and it reports the psql version, not the version of a potentially installed server)
10

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.

You can check if the service is available. Try

service postgresql status
if [ "$?" -gt "0" ]; then
  echo "Not installed".
else
  echo "Intalled"
fi

or in a broader range of Linux distros systemctl status postgresql can be used.

4 Comments

It's noteworthy that this command would only work on the Debian based family of Linux distros.
didnt worked ,no command found, i know command service posrgres status, but how to check result
I don't have it installed, so it printed out "posrgres: unrecognized service"
some systems don't have the service command. In Ubuntu or Debian, I'd check for /etc/init.d/postgres status or something like that.
6

There is no single simple way to do it, because PostgreSQL might be installed and set up in many different ways:

  • Installed from source in a user home directory
  • Installed from source into /opt or /usr/local, manually started or started by an init script
  • Installed from distributor rpm / deb packages and started via init script
  • Installed from 3rd party rpm / deb packages and started via init script
  • Installed from packages but not set to start
  • Client installed, connecting to a server on a different computer
  • Installed and running but not on the default PATH or default port

You can't rely on psql being on the PATH. You can't rely on there being only one psql on the system (multiple versions might be installed in different ways). You can't do it based on port, as there's no guarantee it's on port 5432, or that there aren't multiple versions.

Prompt the user and ask them.

Comments

5

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.

1 Comment

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 :-)
4

And if everything else fails from these great choice of answers, you can always use "find" like this. Or you may need to use sudo

If you are root, just type $$> find / -name 'postgres'

If you are a user, you will need sudo priv's to run it through all the directories

I run it this way, from the / base to find the whole path that the element is found in. This will return any files or directories with the "postgres" in it.

You could do the same thing looking for the pg_hba.conf or postgresql.conf files also.

Comments

4

If you are running Debian Linux (or derivative) and if you have a postive return with > which psql, then simply type psql -V (capital "V") and you will get a return like: psql (PostgreSQL) 9.4.8

1 Comment

I presume this only indicates that the psql client exists rather than the server as OP clarified in his comment. Also the psql client version may mismatch the server version.
3

For many years I used the command:

ps aux | grep postgres

On one hand it is useful (for any process) and gives useful info (but from process POV). But on the other hand it is for checking if the server you know, you already installed is running.

At some point I found this tutorial, where the usage of the locate command is shown. It looks like this command is much more to the point for this case.

Comments

2

aptitude show postgresql | grep Version worked for me

Comments

2

dpkg -l | grep postgres

RUN the above command and if you get the output as shown in the image then it does not exist on your system

Output of the command

Source:-Click here for more info

enter image description here

Comments

1

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 . .

1 Comment

We can simply write: psql --version output show like: psql (PostgreSQL) 11.5 (Ubuntu 11.5-1.pgdg18.04+1)
0

You may also check in /opt mount in following path /opt/PostgresPlus/9.5AS/bin/

Comments

0

Well, all answersabove are good but not in all cases.

Basically check the folder /etc/postgresql/

in most cases there will be one subfolder eg. /etc/postgresql/11/ (or /etc/postgresql/12) which means that you have installed 11 (or 12) version, however in many cases you may have many of such subfolders, so having them all means that all those versions had been ever installed and could be in use ... so be aware of this important trace.

ps using Ubuntu 18.04

Comments

0

Go window services ad search for PostGreSQL. here I found enter image description here

Comments

0

WARNING: this is only checking if postgres CLIENT is installed (which can be installed WITHOUT installing the server)

psql --version; 

AI suggested this:

pg_isready 
/var/run/postgresql:5432 - accepting connections

possibly a ubuntu only way is to use absolute path to the binary:

/usr/lib/postgresql/14/bin/postgres --version
postgres (PostgreSQL) 14.13 (Ubuntu 14.13-1.pgdg22.04+1)

/usr/lib/postgresql/12/bin/postgres --version
postgres (PostgreSQL) 12.20 (Ubuntu 12.20-1.pgdg22.04+1)

but it is very bewildering that this is so tricky and that this use case was not documented by postgres developers?

during install a soft link should be put from under /usr/lib/postgresql/14/bin/postgres to /usr/bin

that points to the (lastest?) postgres binary installed, so a simple postgres --version would work and show if postgres SERVER is installed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.