使用PGPASSWORD运行psql命令行

时间:2016-10-11 02:50:09

标签: python psql

我想使用Popen或类似的东西从Python运行以下命令:

from subprocess import Popen, PIPE

cmds = ['PGPASSWORD=xxxx', 'psql', '-h', 'postgres_host', '-p', '6666', '-d', 'prod', '-U', 'user111', '-c', 'SELECT * FROM users LIMIT 10']
process=Popen(cmd_list, stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)

我得到的是:

Traceback (most recent call last):
  File "a.py", line 4, in <module>
    process=Popen(cmd_list, stdout=PIPE, stderr=PIPE)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

任何有关如何解决此问题的见解将不胜感激!或者如何以不同的方式将密码传递给psql也很好!

0 个答案:

没有答案