Thread: How to check postgres running or not ?
Hello Sir, I want to know how to check whether postgres database is running or not ? when I give command like ./postmaster -i &, whether all databases in that postgres will run or any one [default] ? If any one, then how to detect that database ? Thanks and regards, Sandeep. --------------------------------------------------------- Disclaimer: The contents of this message are confidential and intended to the addressee at the specified e-mail address only. Its contentsmay not be copied or disclosed to anyone other than the intended recipient. If this e-mail is received in error,please contact Vertex Software Pvt. Ltd immediately on +91 20 4041500 with details of the sender and addressee anddelete the e-mail. Vertex Software Pvt. Ltd accepts no responsibility in the event that the onward transmission, openingor use of this message and/or any attachments adversely affects the recipient's systems or data. It is the recipient'sresponsibility to carry out such virus and other checks as the recipient considers appropriate. ---------------------------------------------------------
In an attempt to throw the authorities off his trail, sgaikwad@vertex.co.in ("Sandeep Gaikwad") transmitted: > Hello Sir, > I want to know how to check whether postgres database > is running or not ? when I give command like ./postmaster -i &, > whether all databases in that postgres will run or any one [default] ? > If any one, then how to detect that database ? Well, the way I usually check on what databases are running is thus: cbbrowne@wolfe:/tmp/mm5/doc> netstat -an | grep PG Saturday 13:18:30 unix 2 [ ACC ] STREAM LISTENING 2793 /var/run/postgresql/.s.PGSQL.5432 One could presumably script things further to get more out of that; it doesn't normally seem worthwhile to do so... -- output = ("cbbrowne" "@" "ntlug.org") http://www.ntlug.org/~cbbrowne/postgresql.html "As long as war is regarded as wicked, it will always haveits fascination. When it is looked upon as vulgar,it will ceaseto be popular." --Oscar Wilde
Sandeep Gaikwad wrote: > > Hello Sir, > I want to know how to check whether postgres database is > running or not ? when I give command like ./postmaster -i &, whether > all databases in that postgres will run or any one [default] ? If any > one, then how to detect that database ? Well, in the $DATADIR you habe postmaster.pid that contains the supposed pid... Regards Gaetano Mendola
Assuming it is unix.... The command ps xau|grep post If it is running it will produce output like... worik@stolberg:~$ ps xau|grep post postgres 880 0.0 0.2 8580 740 ? S Sep07 0:31 /usr/lib/postgresql/bin/postmaster postgres 887 0.0 0.2 9536 612 ? S Sep07 0:02 postgres: stats buffer process postgres 889 0.0 0.2 8624 632 ? S Sep07 0:05 postgres: stats collector process worik 25927 0.0 0.2 1612 540 pts/10 S 13:54 0:00 grep post worik@stolberg:~$ If it is not running it will produce output like... worik@stolberg:~$ ps xau|grep post worik 26094 0.0 0.2 1608 528 pts/10 S 13:55 0:00 grep post Sandeep Gaikwad wrote: > > Hello Sir, > I want to know how to check whether postgres database is > running or not ? when I give command like ./postmaster -i &, whether > all databases in that postgres will run or any one [default] ? If any > one, then how to detect that database ? > > Thanks and regards, > Sandeep. > > > ------------------------------------------------------------------------ > > --------------------------------------------------------- > > Disclaimer: > > The contents of this message are confidential and intended to the addressee at the specified e-mail address only. Its contentsmay not be copied or disclosed to anyone other than the intended recipient. If this e-mail is received in error,please contact Vertex Software Pvt. Ltd immediately on +91 20 4041500 with details of the sender and addressee anddelete the e-mail. Vertex Software Pvt. Ltd accepts no responsibility in the event that the onward transmission, openingor use of this message and/or any attachments adversely affects the recipient's systems or data. It is the recipient'sresponsibility to carry out such virus and other checks as the recipient considers appropriate. > > --------------------------------------------------------- > > > ------------------------------------------------------------------------ > > > ---------------------------(end of broadcast)--------------------------- > TIP 7: don't forget to increase your free space map settings
Worik wrote: > Assuming it is unix.... The command > > ps xau|grep post You might want to change that to: ps aux|grep postgres As your suggestion will pick up extraneous data if one is running postfix on the same box. -- Until later, Geoffrey Registered Linux User #108567 AT&T Certified UNIX System Programmer- 1995
--- Christopher Browne <cbbrowne@acm.org> wrote: > In an attempt to throw the authorities off his > trail, sgaikwad@vertex.co.in ("Sandeep Gaikwad") > transmitted: > > Hello Sir, > > I want to know how to check > whether postgres database > > is running or not ? when I give command like > ./postmaster -i &, > > whether all databases in that postgres will run or > any one [default] ? > > If any one, then how to detect that database ? The "standard" way would be "pg_ctl status". "man pg_ctl" is recommended reading for anyone administering a PostgreSQL setup. > > Well, the way I usually check on what databases are > running is thus: > > cbbrowne@wolfe:/tmp/mm5/doc> netstat -an | grep PG > > Saturday 13:18:30 > unix 2 [ ACC ] STREAM LISTENING > 2793 /var/run/postgresql/.s.PGSQL.5432 > > One could presumably script things further to get > more out of that; it > doesn't normally seem worthwhile to do so... > -- > output = ("cbbrowne" "@" "ntlug.org") > http://www.ntlug.org/~cbbrowne/postgresql.html > "As long as war is regarded as wicked, it will > always have > its fascination. When it is looked upon as vulgar, > it will cease to be popular." > --Oscar Wilde > > ---------------------------(end of > broadcast)--------------------------- > TIP 2: you can get off all lists at once with the > unregister command > (send "unregister YourEmailAddressHere" to > majordomo@postgresql.org) > _______________________________ Do you Yahoo!? Declare Yourself - Register online to vote today! http://vote.yahoo.com
Geoffrey <esoteric@3times25.net> writes: > Worik wrote: >> Assuming it is unix.... The command >> ps xau|grep post > You might want to change that to: > ps aux|grep postgres > As your suggestion will pick up extraneous data if one is running > postfix on the same box. Actually I'd recommend grepping for "postmaster". If your PG user is named "postgres" then the above command will find any program the PG user is running --- which might only be a shell, for instance. If your PG user is not named "postgres" then the above might find nothing at all, even though the postmaster is alive (since depending on the details of your local ps command, it might report all the server processes as "postmaster"). There is even another gotcha, which is that the "grep postmaster" command could easily find itself in the ps output. So what really works isps aux | grep postmaster | grep -v grep (or use "ps -ef" if using a SysV-ish ps). Obviously none of this matters if you are just going to eyeball the output, but if you want something suitable for a test in a script, you'd better use something like the last one. regards, tom lane
On Sun, Sep 19, 2004 at 12:25:00PM -0400, Tom Lane wrote: > ps aux | grep postmaster | grep -v grep > (or use "ps -ef" if using a SysV-ish ps). Except that on Solaris, ps -ef _always_ shows "postmaster", even for the individual back ends. A -- Andrew Sullivan | ajs@crankycanuck.ca In the future this spectacle of the middle classes shocking the avant- garde will probably become the textbook definition of Postmodernism. --Brad Holland
Andrew Sullivan <ajs@crankycanuck.ca> writes: > On Sun, Sep 19, 2004 at 12:25:00PM -0400, Tom Lane wrote: >> ps aux | grep postmaster | grep -v grep >> (or use "ps -ef" if using a SysV-ish ps). > Except that on Solaris, ps -ef _always_ shows "postmaster", even for > the individual back ends. Right, but if you see a backend then you can figure the system is up. If you are concerned about the case where the postmaster has crashed and yet there are still backends laying about, then the whole "ps" approach is probably wrong anyway. It would make more sense to check whether the postmaster is answering the doorbell --- ie, send a connection request and see what happens. At one time there was discussion of writing a "pg_ping" utility program to do exactly this, but it still hasn't got done. You can fake it to some extent by just running "psql -l >/dev/null" and checking the exit code, but this does require supplying a valid username and possibly a password (because psql's exit code doesn't distinguish "could not connect" from authentication errors). BTW, "pg_ctl status" doesn't answer this need because it only looks for a postmaster.pid file, it doesn't attempt to verify that the postmaster is really alive. regards, tom lane
Jeff Eckermann wrote: > --- Christopher Browne <cbbrowne@acm.org> wrote: > > >>In an attempt to throw the authorities off his >>trail, sgaikwad@vertex.co.in ("Sandeep Gaikwad") >>transmitted: >> >>>Hello Sir, >>> I want to know how to check >> >>whether postgres database >> >>>is running or not ? when I give command like >> >>./postmaster -i &, >> >>>whether all databases in that postgres will run or >> >>any one [default] ? >> >>>If any one, then how to detect that database ? > > > The "standard" way would be "pg_ctl status". "man > pg_ctl" is recommended reading for anyone > administering a PostgreSQL setup. Is not enough because it check only for the postmaster.pid and not if the engine is really up. Regards Gaetano Mendola
Tom Lane wrote: > Geoffrey <esoteric@3times25.net> writes: > >>Worik wrote: >> >>>Assuming it is unix.... The command >>>ps xau|grep post > > >>You might want to change that to: >>ps aux|grep postgres >>As your suggestion will pick up extraneous data if one is running >>postfix on the same box. > > > Actually I'd recommend grepping for "postmaster". If your PG user is > named "postgres" then the above command will find any program the PG > user is running --- which might only be a shell, for instance. If your > PG user is not named "postgres" then the above might find nothing at > all, even though the postmaster is alive (since depending on the details > of your local ps command, it might report all the server processes as > "postmaster"). > > There is even another gotcha, which is that the "grep postmaster" > command could easily find itself in the ps output. So what really > works is > ps aux | grep postmaster | grep -v grep > (or use "ps -ef" if using a SysV-ish ps). Just to enforce the test is better looking for the entire executable path: ps aux | grep /usr/bin/postmaster | grep -v grep Regards Gaetano Mendola
On Sun, Sep 19, 2004 at 01:12:07PM -0400, Tom Lane wrote: > > Except that on Solaris, ps -ef _always_ shows "postmaster", even for > > the individual back ends. > > Right, but if you see a backend then you can figure the system is up. Oops, good point. (And in any case, on Solaris you also have the ucb ps, so it makes no difference.) A -- Andrew Sullivan | ajs@crankycanuck.ca The fact that technology doesn't work is no bar to success in the marketplace. --Philip Greenspun
[snip] > > > Just to enforce the test is better looking for the entire executable path: > > ps aux | grep /usr/bin/postmaster | grep -v grep > Does not work for me! worik@stolberg:~$ ps aux | grep /usr/bin/postmaster | grep -v grep worik@stolberg:~$ ps aux | grep postmaster | grep -v grep postgres 670 0.1 0.6 8544 1688 pts/1 S 12:33 0:00 /usr/lib/postgresql/bin/postmaster worik@stolberg:~$ So... " ps aux | grep postmaster | grep -v grep " is more reliable(?) cheers Worik > > > Regards > Gaetano Mendola > > > > > > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster > > >
Perhaps I have a bug in pg_ctrl? This is what I get... worik@stolberg:~$ ps aux | grep postmaster | grep -v grep ;/usr/lib/postgresql/bin/pg_ctl status postgres 670 0.0 0.6 8544 1688 pts/1 S 12:33 0:00 /usr/lib/postgresql/bin/postmaster pg_ctl: postmaster or postgres is not running worik@stolberg:~$ cheers Worik Jeff Eckermann wrote: > --- Christopher Browne <cbbrowne@acm.org> wrote: > > >>In an attempt to throw the authorities off his >>trail, sgaikwad@vertex.co.in ("Sandeep Gaikwad") >>transmitted: >> >>>Hello Sir, >>> I want to know how to check >> >>whether postgres database >> >>>is running or not ? when I give command like >> >>./postmaster -i &, >> >>>whether all databases in that postgres will run or >> >>any one [default] ? >> >>>If any one, then how to detect that database ? > > > The "standard" way would be "pg_ctl status". "man > pg_ctl" is recommended reading for anyone > administering a PostgreSQL setup. > > >>Well, the way I usually check on what databases are >>running is thus: >> >>cbbrowne@wolfe:/tmp/mm5/doc> netstat -an | grep PG >> >> Saturday 13:18:30 >>unix 2 [ ACC ] STREAM LISTENING >>2793 /var/run/postgresql/.s.PGSQL.5432 >> >>One could presumably script things further to get >>more out of that; it >>doesn't normally seem worthwhile to do so... >>-- >>output = ("cbbrowne" "@" "ntlug.org") >>http://www.ntlug.org/~cbbrowne/postgresql.html >>"As long as war is regarded as wicked, it will >>always have >> its fascination. When it is looked upon as vulgar, >> it will cease to be popular." >> --Oscar Wilde >> >>---------------------------(end of >>broadcast)--------------------------- >>TIP 2: you can get off all lists at once with the >>unregister command >> (send "unregister YourEmailAddressHere" to >>majordomo@postgresql.org) >> > > > > > > _______________________________ > Do you Yahoo!? > Declare Yourself - Register online to vote today! > http://vote.yahoo.com > > ---------------------------(end of broadcast)--------------------------- > TIP 9: the planner will ignore your desire to choose an index scan if your > joining column's datatypes do not match > > >
Worik <worik@noggon.com> writes: > Perhaps I have a bug in pg_ctrl? More likely you have the wrong value of PGDATA in your environment (where "wrong" means "not what that postmaster is using"). regards, tom lane
How about a simple " telnet localhost 5432 " ? assuming postmaster is listening on that host and port On Fri, 17 Sep 2004, Sandeep Gaikwad wrote: > > Hello Sir, > I want to know how to check whether postgres database is > running or not ? when I give command like ./postmaster -i &, whether all > databases in that postgres will run or any one [default] ? If any one, then > how to detect that database ? > > Thanks and regards, > Sandeep. >
Clinging to sanity, worik@noggon.com (Worik) mumbled into her beard: > [snip] >> Just to enforce the test is better looking for the entire executable >> path: >> ps aux | grep /usr/bin/postmaster | grep -v grep >> > > Does not work for me! > > worik@stolberg:~$ ps aux | grep /usr/bin/postmaster | grep -v grep > worik@stolberg:~$ ps aux | grep postmaster | grep -v grep > postgres 670 0.1 0.6 8544 1688 pts/1 S 12:33 0:00 > /usr/lib/postgresql/bin/postmaster > worik@stolberg:~$ > > So... > " ps aux | grep postmaster | grep -v grep " > is more reliable(?) There's a very slightly clever modification that gets rid of the extra grep... Try.. [appropriate ps command for your platform] | egrep '[p]ostmaster' That egrep _won't_ match its own command line. -- let name="cbbrowne" and tld="ntlug.org" in String.concat "@" [name;tld];; http://www3.sympatico.ca/cbbrowne/multiplexor.html He doesn't have much of a reputation, or so I've heard.
I want to use pgsql to send the command: Copy sometable from 'sometable.csv'; But only if 'sometable.csv' exists; If 'sometable.csv' does not exist as an input table I want to continue the next command. My full procedure is as follows and any help is greatly appreciated. Thanks Richard CREATE OR REPLACE FUNCTION restore_database(text) RETURNS text AS $BODY$ declare tblname record; cnt record; tname varchar :=''; tquery varchar :=''; filename varchar :=''; begin tname := ''; for tblname in select tablename from pg_tables WHERE not(tablename like 'pg_%') and not(tablename like 't_%') and not(tablename like '%_list') order by tablename loop raise notice '%',tblname.tablename; tquery := 'delete from '|| tblname.tablename ; execute tquery; filename := '/'||$1||'/'|| lower(tblname.tablename)||'.csv'; tquery := 'copy '|| tblname.tablename || ' from ' || quote_literal(filename); execute tquery; end loop; return tquery; end; $BODY$ LANGUAGE 'plpgsql' VOLATILE; --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004
Hello! Our check procedure on TRU64 UNIX is: checkmasterdaemon if [ $? -eq 1 ]; then # Try to connect to postgres by selecting tables TABELLEN=`/pg/postgresql-7.4.3/bin/psql-h postgres -c 'select datname from pg_database' postgres postgres 2>/dev/null` if [[ "X$TABELLEN" = "X" ]];then echo "check failed for postmaster" exit 1 else # echo"postmaster is running" fi else echo "check failed for postmaster" exit 1 fi where : checkmasterdaemon () { MASTER_PID=$(getmasterpid) if [ "X$MASTER_PID" = "X" ]; then #echo "Postmaster is not running" ret=0 else PS_OUT=`ps -o comm,pid-p "${MASTER_PID}"` PID=`echo $PS_OUT | awk '/postgres/ {print $4}'` #echo $PID if [ "X$MASTER_PID"!= "X$PID" ] then #echo "Postmaster (${MASTER_PID}) does not exist (any more)" ret=0 else ret=1 fi fi return $ret } getmasterpid () { if [[ -r ${PIDFILE} ]] then MASTER_PID=`head -n 1 ${PIDFILE}` echo $MASTER_PID else echo "" fi } -- Mit freundlichen Gruessen / With best regards Reiner Dassing
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Worik wrote: | | | [snip] | |> |> |> Just to enforce the test is better looking for the entire executable |> path: |> |> ps aux | grep /usr/bin/postmaster | grep -v grep |> | | Does not work for me! | | worik@stolberg:~$ ps aux | grep /usr/bin/postmaster | grep -v grep | worik@stolberg:~$ ps aux | grep postmaster | grep -v grep | postgres 670 0.1 0.6 8544 1688 pts/1 S 12:33 0:00 | /usr/lib/postgresql/bin/postmaster | worik@stolberg:~$ | | So... | " ps aux | grep postmaster | grep -v grep " | is more reliable(?) It only depends on your distribution, in your case: ps aux | grep /usr/lib/postgresql/bin/postmaster | grep -v grep consider also the if you run different postmaster version in different location this is the only way I believe Regards Gaetano Mendola -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (MingW32) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFBTquC7UpzwH2SGd4RAiouAKCpvuLSspsTVXCjSLgDZ2ZKQ3gfywCg7rzl gXkPlq9UanBNjIWpBvPXNwo= =ZNAY -----END PGP SIGNATURE-----
Am Montag, 20. September 2004 12:05 schrieb Gaetano Mendola: > It only depends on your distribution, in your case: > > ps aux | grep /usr/lib/postgresql/bin/postmaster | grep -v grep > > consider also the if you run different postmaster version in different > location this is the only way I believe Or maybe simply: $ ps -C postmaster -F UID PID PPID C SZ RSS PSR STIME TTY TIME CMD postgres 1172 1 0 4339 2160 0 09:14 ? 00:00:00 /usr/lib/postgresql/bin/postmaster -D /var/lib/postgres/data postgres 1177 1172 0 2039 2776 0 09:14 ? 00:00:00 postgres: stats buffer process postgres 1178 1177 0 1826 2048 0 09:14 ? 00:00:00 postgres: stats collector process postgres 9935 1172 0 4443 2848 0 13:46 ? 00:00:00 postgres: pei template1 [local] idle -- Peter Eisentraut http://developer.postgresql.org/~petere/
Tom Lane wrote: > At one time there was discussion of writing a "pg_ping" utility program > to do exactly this, but it still hasn't got done. You can fake it to > some extent by just running "psql -l >/dev/null" and checking the exit > code, but this does require supplying a valid username and possibly a > password (because psql's exit code doesn't distinguish "could not > connect" from authentication errors). > > BTW, "pg_ctl status" doesn't answer this need because it only looks for > a postmaster.pid file, it doesn't attempt to verify that the postmaster > is really alive. Is improving this a TODO item for pg_ctl? -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001+ If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania19073
Funny... I set up a little program on my development machine that queries for the rendezvous name. This way I know my development machine is running before I try to connect. I think zeroconf could be used for this quite easily... JMHO Ted --- Arne Stoelck <stolck@web.de> wrote: > > How about a simple > > " telnet localhost 5432 " ? > > assuming postmaster is listening on that host and > port > > > On Fri, 17 Sep 2004, Sandeep Gaikwad wrote: > > > > > Hello Sir, > > I want to know how to check whether > postgres database is > > running or not ? when I give command like > ./postmaster -i &, whether all > > databases in that postgres will run or any one > [default] ? If any one, then > > how to detect that database ? > > > > Thanks and regards, > > Sandeep. > > > > ---------------------------(end of > broadcast)--------------------------- > TIP 9: the planner will ignore your desire to choose > an index scan if your > joining column's datatypes do not match > __________________________________ Do you Yahoo!? Yahoo! Mail is new and improved - Check it out! http://promotions.yahoo.com/new_mail
Richard, > But only if 'sometable.csv' exists; > > If 'sometable.csv' does not exist as an input table I want to continue > the next command. Sorry, can't be done in plpgsql. Regular procedural languages are deliberately kept ignorant of the host filesystem in order to prevent database users from being able to compromise it. It's a security thing. You could easily write a function in an "untrusted" language, like PL/PerlU or PL/PythonU, which would test for file existence and return a true/false value. -- Josh Berkus Aglio Database Solutions San Francisco