3

I've just started using the psycopg2 module to access a local PostgreSQL server and I'll like a way to progammatically check if the server is already started so my program can handle the error when I try to start the 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 already started
conn = psycopg2.connect('dbname=' + dbname + ' user=' + username)
cur = conn.cursor()

I experimented a little and it seems that this returns a "0" or "3", which would solve my problem, but I didn't find any information on the PostgreSQL/psycopg2 manual that confirms if this is a documented behavior:

server_state = os.system('pg_ctl status -D C:/mydatadir')

What's the best way? Thanks!

2
  • 2
    Use the subprocess module instead of os.system. And of course: get hold of the output of the command...it's easy to parse the result of the output for related strings... Commented Apr 20, 2013 at 5:28
  • 1
    @PrincessOftheUniverse subprocess is certainly the right way to go, but I strongly disagree with the other part of your advice. It is generally not wise to parse for strings in output; ephedyn is taking the correct approach by testing return values. String parsing bites you badly the first time someone whose computer is configured for Russian or Thai or ... tries to run your program and the strings don't match anymore. Or someone makes a typo fix or wording clarification in an updated version. It's a bad practice that should be a last resort. Commented Apr 20, 2013 at 10:05

2 Answers 2

5

From the pg_ctl documentation:

-w

Wait for the startup or shutdown to complete. Waiting is the default option for shutdowns, but not startups. When waiting for startup, pg_ctl repeatedly attempts to connect to the server. When waiting for shutdown, pg_ctl waits for the server to remove its PID file. pg_ctl returns an exit code based on the success of the startup or shutdown.

You will also find pg_ctl status useful:

status mode checks whether a server is running in the specified data directory. If it is, the PID and the command line options that were used to invoke it are displayed. If the server is not running, the process returns an exit status of 3.

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

1 Comment

Exactly what I needed, thanks! Also noticed a couple of other useful tips from your previous comments - upvoted appropriately.
1

Try to use pgrep command.

proc = subprocess.Popen(["pgrep -u postgres -f -- -D"], stdout=subprocess.PIPE, shell=True)
    (out, err) = proc.communicate()
    try:
        if int(out) > 0:
            return True
    except Exception as e:
        return False

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.