Skip site navigation (1) Skip section navigation (2)

Re: Checking whether postgresql is running

From: Carlos Moreno <moreno(at)mochima(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Checking whether postgresql is running
Date: 2004-08-24 20:10:09
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-general
Ennio-Sr wrote:

> [Possible duplicate: original sent to novice never got through! -;(]
> Hi all!
> Testing a script where I need to make sure that postgresql is running
> before passing a <psql dbasename -c "insert into ..." > instruction I
> faced this curious behaviour:
> This is the relevant content of the script: 
> ------------------
> #!/bin/bash
> /usr/lib/postgresql/bin/pg_ctl status -D /var/lib/postgres/data >/dev/null 2>&1
> rtn=$?
> if [ $rtn -ne 0 ]; then
>    echo "not running"
> else
>    echo "ok ok"
> fi   
> Now, if I run the script as root, I get: 
> ok ok 
> (or, commenting the script if condition:
> pg_ctl: postmaster is running (pid: 18658)
> Command line was:
> /usr/lib/postgresql/bin/postmasteir)
> whereas, if I run the same script as ordinary user, the answer is:
> not running
> (or, commenting the if lines:
> pg_ctl: postmaster or postgres is not running)
> --------------
> Everything is being tested on the same PC [running PG 7.2.1-2Woody5
> under GNU/Linux, k. 2.2.22], root being on /dev/tty1, user on
> /dev/tty2, and postgresql not being stopped while switching from root
> to user :-).
> Could anybody throw some light on this issue?

Though I don't read Perl at all, so I haven't the
slightest idea about what's in the if, I think I
can shed some light on the issue...

When you execute pg_ctl -D /var/lib/postgres/data,
the command fails -- you can not access the directory
/var/lib/postgres/data to go and pickup the file (I think that's the filename) to check
if that PID is running.  So, without understanding
that gibberish inside the if, I bet that it simply
is returning some error code that is causing the
"not running" part of the if to be executed.

You have to be user postgres or superuser to be
able to use pg_ctl to verify if postmaster is

A "loose check" would be executing the command
"ps -C postgres" and see if there's any output.

Or, simply use the PG client library -- you will
get an error message saying that postmaster is
not running on the specified port.


In response to

pgsql-general by date

Next:From: Marc G. FournierDate: 2004-08-24 20:19:55
Subject: Re: Unsupported 3rd-party solutions (Was: Few questions
Previous:From: Richard HuxtonDate: 2004-08-24 20:08:15
Subject: Re: No connection to a PG 8.0 Beta 1 win32 server

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group