如何在用户更改后继续执行python脚本

时间:2013-09-13 13:03:57

标签: python postgresql python-2.7 subprocess sudo

我想使用python脚本按顺序执行以下命令:

sudo su - postgres   #login as postgres user
psql

我尝试使用:

import os

cmd = 'sudo su - postgres'
os.system(cmd)

cmd1='psql'
os.system(cmd1)

这个问题是第二个命令只有在我从postgres用户注销后才会执行,但是我想把它作为postgres用户运行。如何在用户更改后继续执行python脚本?

由于

1 个答案:

答案 0 :(得分:1)

您可以使用:

sudo su - postgres --command='cat … | psql …'

但你不应该。您应该配置数据库服务器以允许运行python脚本的用户无需密码即可访问您的数据库。或者至少在此用户主目录中使用.pg_pass文件来提供此数据库的用户名和密码。

如果您使用PGPASSWORD,正如您在评论中指出的那样,系统中的任何其他用户都可以使用ps auxwww显示它。