0

I'm trying to create a user in postgreSQL using a python(2.7) script. I'm using subprocess.Popen.

When I call the function I see stdout for the OS.system code, but I don't get any type of output for subprocess.Popen so I have not idea why it's not working. What I do know is the user is not being created. I've also tried print with no luck.

Here is a working bash command that I'm trying to mimic. When this command is entered I'm prompted twice for the password.

createuser -PSdRU postgres -w csdashboard

My python function:

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()
insecure-IT
  • 2,068
  • 4
  • 18
  • 26
  • 3
    possible duplicate of [Python - capture Popen stdout AND display on console?](http://stackoverflow.com/questions/1283061/python-capture-popen-stdout-and-display-on-console) – aruisdante Oct 16 '14 at 17:46
  • 1
    Password prompts don't always work when using Popen. Check out the pexpect module, which is built to handle them. – tdelaney Oct 16 '14 at 18:01
  • Issues: 1. You've redirected stdout/stderr (PIPE) but you don't read from the streams, try: `print(pguseradd.....user.communicate(("\n".join([passwd]*2)))` (if you want to discard the output then redirect to os.devnull instead) 2. Password can be asked from the terminal directly outside of stdin/stdout: you might need a pseudo-tty to write/read such input/output (`pexpect` uses one). Here's [a code example that shows how to read directly from terminal](http://stackoverflow.com/a/22253472/4279). – jfs Oct 16 '14 at 19:50
  • 1
    Each `os.system()` creates a shell process. Why don't you put these commands into a single bash script and run it instead? For example, see how [`subprocess.check_call()` runs multiple shell commands](https://gist.github.com/zed/754cdf6540f8ed7c4e56). – jfs Oct 16 '14 at 19:52
  • @aruisdante: as far as I can tell; the question is not a duplicate. OP sets `stdout=PIPE` and doesn't read from it – jfs Oct 16 '14 at 19:59

0 Answers0