使用 django 检查 postgres 数据库是否存在 - 抛出 OS 错误。
Checking postgres database existense using django - throws OS error .
我在使用 supervisor 的虚拟环境中有一个 django 应用程序 运行ning。 root
的主管是 运行,用户 ubuntu
的应用程序是 运行。
我想检查 postgres 中是否存在数据库。我下面的函数在正常 python shell (Repl) 中运行良好,甚至当我 运行 我的应用程序使用 python migrate.py runserver
甚至在 django shell .
然而,一旦我使用 supervisor 启动应用程序并执行该代码块,我就会收到以下异常 -
Exception Type: OSError
Exception Value:[Errno 2] No such file or directory
这是函数
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)
我不明白它想要的确切目录或文件是什么find.Is它有任何权限问题吗?但即使是正常的 shells 也是 运行 使用 ubuntu
用户所以似乎不是权限错误。如何调试在 supervisor 中 运行 时找不到哪个文件?我在主管中添加了日志,但它只显示 <request url > HTTP/1.0" 500
,所以没有任何线索。
这是管理员配置
[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
您不需要如此复杂的方法来测试数据库是否存在。试试这个:
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
问题出在主管配置上。它试图找到 psql
但没有找到它的路径。原因是 supervisor conf 中的路径变量错误。这是运行良好的正确设置。
更改:删除 PATH
并指定虚拟环境 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
我在使用 supervisor 的虚拟环境中有一个 django 应用程序 运行ning。 root
的主管是 运行,用户 ubuntu
的应用程序是 运行。
我想检查 postgres 中是否存在数据库。我下面的函数在正常 python shell (Repl) 中运行良好,甚至当我 运行 我的应用程序使用 python migrate.py runserver
甚至在 django shell .
然而,一旦我使用 supervisor 启动应用程序并执行该代码块,我就会收到以下异常 -
Exception Type: OSError
Exception Value:[Errno 2] No such file or directory
这是函数
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)
我不明白它想要的确切目录或文件是什么find.Is它有任何权限问题吗?但即使是正常的 shells 也是 运行 使用 ubuntu
用户所以似乎不是权限错误。如何调试在 supervisor 中 运行 时找不到哪个文件?我在主管中添加了日志,但它只显示 <request url > HTTP/1.0" 500
,所以没有任何线索。
这是管理员配置
[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
您不需要如此复杂的方法来测试数据库是否存在。试试这个:
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
问题出在主管配置上。它试图找到 psql
但没有找到它的路径。原因是 supervisor conf 中的路径变量错误。这是运行良好的正确设置。
更改:删除 PATH
并指定虚拟环境 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