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()