我现在已经苦苦挣扎了很长时间,仍然无法使这行代码正常工作:
os.system('su - postgres -c "psql -c \'ALTER USER postgres WITH ENCRYPTED PASSWORD \\\"{0}\\\";\'"'.format(self.password))
并记录结果:
2013-11-12 19:58:42 ICT ERROR: syntax error at or near ""password"" at character 45
2013-11-12 19:58:42 ICT STATEMENT: ALTER USER postgres WITH ENCRYPTED PASSWORD "password";
有什么想法吗?
答案 0 :(得分:3)
Urgh。如果可能的话,请使用psycopg2
,PostgreSQL的本机Python客户端驱动程序。
如果您必须转发至psql
,请使用subprocess
模块的check_call
功能,这样可以更轻松,更安全地为您进行报价。
import subprocess
subprocess.check_call([
'psql', '-c',
'ALTER USER postgres WITH ENCRYPTED PASSWORD "{0}";'.format(self.password)
])
os.system
应该是IMO的最后手段。
答案 1 :(得分:0)
密码应该是单引号 -
postgres=# create user usr with password "password";
ERROR: syntax error at or near ""password""
LINE 1: create user usr with password "password";
^
postgres=# create user usr with password 'password';
CREATE ROLE