我有需要调用psql的Django应用程序.我在芹菜线程中执行以下操作:
@task()
def insert_sqldump_threaded(username, database, file):
host = database.server.db_address
work = subprocess.Popen([settings.Psql,
"-f%s" % file,
"-d%s" % database.db_name,
"-h%s" % host,
"-U%s" % settings.DB_ADMIN_USER
], env = {'PGPASSFILE': settings.DB_PASSFILE}
)
work.wait()
return work.returncode
在我的开发服务器上,PGPASSFILE如下所示:
localhost:5432:*:postgres:postgres
应该没问题
psql: Could not translate host name "localhost" to address: UnkNown server error
现在,它真的变得很奇怪,但是当我不提交“ env”变量时,psql似乎可以识别主机.至少然后它要求输入密码.
关于如何解决这个问题的任何想法?
解决方法:
我认为postgresql需要通过环境时清除的其他环境变量.您可以简单地更改os.environ或预先复制它,如以下代码所示:
import os
@task()
def insert_sqldump_threaded(username, database, file):
d = dict(os.environ)
d['PGPASSFILE'] = settings.DB_PASSFILE
host = database.server.db_address
work = subprocess.Popen([settings.Psql,
"-f%s" % file,
"-d%s" % database.db_name,
"-h%s" % host,
"-U%s" % settings.DB_ADMIN_USER
], env = d
)
work.wait()
return work.returncode