2

I have a django application running in a virtual environment using supervisor . Supervisor is run by root and application is run by user ubuntu .

I want to check whether a database exist in postgres or not. My function below works well in normal python shell (Repl) and even when I run my application using python migrate.py runserver and even in django shell .

However as soon as I start the application using supervisor and that code block executes , I get the following exception -

Exception Type: OSError Exception Value:[Errno 2] No such file or directory

Here's the function which is

def database_exists(database_name):
    try:
         db= "anydbname"
         c1 = "psql -U postgres -lqt"
         c2 = "cut -d | -f 1"
         c3 = "grep -w " + db              

         ps1 = subprocess.Popen(c1.split(),stdout=subprocess.PIPE)
         ps2 = subprocess.Popen(c2.split(),stdin=ps1.stdout, stdout=subprocess.PIPE)
         ps3 = subprocess.Popen(c3.split(),stdin=ps2.stdout , stdout=subprocess.PIPE)
         result = ps3.communicate()[0]  # if empty , db not found 
         result = result.strip()
         if result == "" :
             return False
         else :
             return True 
    except Exception, e:
        raise e
        return False, str(e)

I failed to understand what is the exact directory or file it wants to find.Is it any kind of permission issue ? But even the normal shells are run using ubuntu user so doesn't seem to be permission error . How to debug it which file not found when run in supervisor ? I added the logs in supervisor but it only shows <request url > HTTP/1.0" 500 so no clues about it .

Here's the supervisor config

[program:myprog]
environment=PATH="/home/ubuntu/.virtualenvs/myvirtualenv/bin"
command=python /var/www/myapp/manage.py rungevent 127.0.0.1:8010 250
directory=/var/www/myapp
autostart=true
autorestart=true
redirect_stderr=True
killasgroup=true
stopasgroup=true
user=ubuntu
stdout_logfile = /var/log/supervisor/myapp.log
stderr_logfile = /var/log/supervisor/myapp-err.log
3
  • This could be a case where its not able to find psql command. Try giving complete path where psql is present. Commented May 2, 2016 at 6:11
  • hey redoc ! you nailed it man . Exactly this was the reason. This code was a part of a big system . we did few parts using pyscopg2 as suggested by @Sergey below however you saved it man. Commented May 2, 2016 at 7:30
  • I got the reason of it . We reassigned the PATH variable of system while in supervisor so its not able to find path Commented May 2, 2016 at 7:31

2 Answers 2

1

You don't need so sophisticated way to test database existence. Try this:

import psycopg2

def database_exists(database_name):
    con = None
    res = None
    try:
        con = psycopg2.connect(database="test", user="test", password="abcd", host="127.0.0.1") #It's better to get the parameters from settings
        cur = con.cursor()
        cur.execute("SELECT exists(SELECT 1 from pg_catalog.pg_database where datname = %s)", (database_name,))
        res = cur.fetchone()[0]
    except psycopg2.DatabaseError as e:
        res = False
    finally:
        if con:
            con.close()
    return res
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer . This seems to be a better way to do query rather the psql . 100% agreed . Thanks a lot .
I upvoted the answer however couldn't accept it as the solution to my problem was with supervisor config
Regarding the supervisor, try to use absolute paths to executables - psql, cut and grep.
0

The problem was with supervisor config . It was trying to find psql and not getting path of it . Reason was the wrong path variable in supervisor conf . Here's the correct setting which worked well .

Change : Removed PATH and specified the full executable path of virtual env's python .

[program:myprog]
command=/home/ubuntu/.virtualenvs/myvirtualenv/bin/python /var/www/myapp/manage.py rungevent 127.0.0.1:8010 250
directory=/var/www/myapp
autostart=true
autorestart=true
redirect_stderr=True
killasgroup=true
stopasgroup=true
user=ubuntu
stdout_logfile = /var/log/supervisor/myapp.log
stderr_logfile = /var/log/supervisor/myapp-err.log

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.