Python/PostgreSQL - check if server is running -


i've started using psycopg2 module access local postgresql server , i'll way progammatically check if server started program can handle error when try start server:

psqldir = 'c:/program files/postgresql/9.2/bin'    # windows username = os.environ.get('username') dbname = 'mydb' os.chdir(psqldir) os.system('pg_ctl start -d c:/mydatadir')   # error here if server started conn = psycopg2.connect('dbname=' + dbname + ' user=' + username) cur = conn.cursor() 

i experimented little , seems returns "0" or "3", solve problem, didn't find information on postgresql/psycopg2 manual confirms if documented behavior:

server_state = os.system('pg_ctl status -d c:/mydatadir') 

what's best way? thanks!

from the pg_ctl documentation:

-w

wait startup or shutdown complete. waiting default option shutdowns, not startups. when waiting startup, pg_ctl repeatedly attempts connect server. when waiting shutdown, pg_ctl waits server remove pid file. pg_ctl returns exit code based on success of startup or shutdown.

you find pg_ctl status useful:

status mode checks whether server running in specified data directory. if is, pid , command line options used invoke displayed. if server not running, process returns exit status of 3.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -