当我使用subprocess.Popen为postgresql调用createuser时,如何才能看到stdout?

时间:2014-10-16 17:45:03

标签: python python-2.7

我正在尝试使用python(2.7)脚本在postgreSQL中创建用户。我正在使用subprocess.Popen

当我调用该函数时,我看到stdout代码OS.system,但我没有得到subprocess.Popen的任何类型的输出,所以我不知道为什么它不起作用。我所知道的是没有创建用户。我也试过print而没有运气。

这是一个我正在尝试模仿的工作bash命令。输入此命令后,我会提示两次输入密码。

createuser -PSdRU postgres -w csdashboard

我的python函数:

def setup_postgresql():
    print('\n'*10)
    print('Configuring PostgreSQL')
    time.sleep(3)

    #initialize the db
    os.system('/etc/init.d/postgresql initdb')

    #boot at start
    os.system('chkconfig postgresql --add')
    os.system('chkconfig postgresql on')

    #copy the configs
    shutil.copyfile('configs/pg_hba.conf', '/var/lib/pgsql/data/pg_hba.conf')
    shutil.copyfile('configs/postgresql.conf', '/var/lib/pgsql/data/postgresql.conf')

    #start postgresql
    os.system('/etc/init.d/postgresql start')
    os.system('createdb -U postgres csdashboard')

    #add my user    
    pguseradd_csdashboard_user = subprocess.Popen(['createuser', ' -PSdRU', 'postgres', '-w',
                                                   'csdashboard'],
                                                  shell=False,
                                                  stdout=subprocess.PIPE,
                                                  stdin=subprocess.PIPE,
                                                  stderr=subprocess.PIPE)

    pguseradd_csdashboard_user.stdin.write(csdashboard_password + '\n')
    pguseradd_csdashboard_user.stdin.write(csdashboard_password + '\n')
    pguseradd_csdashboard_user.stdin.flush()



    os.system('/etc/init.d/postgresql restart')
    exit()

0 个答案:

没有答案