prostock
prostock

Reputation: 9545

use fabric to change default postgres user password

I'm trying to setup python fabric for postgresql, but can't figure out how to input the new password at the prompt. How do you set up the following in fabric:

sudo passwd –d postgres
sudo su postgres –c passwd

Upvotes: 1

Views: 547

Answers (1)

pyfunc
pyfunc

Reputation: 66729

Solution 1:

What I am writing here is a bad hack from the security perspective. It provides the password on the command line hence it should not be used any where apart from toy work:

with hide('running', 'stdout', 'stderr'):
    run('echo -e "%s\n%s\n" | sudo passwd %s' % (passwd, passwd, user))

PS: This solution uses older formatting technique. That should not detract you from using newer one.

Solution 2:

Passwd expects input from stdin, you can instead use pexpect / expect to do this work exclusively and is better approach than the above but not without it's flaws.

Sample pexpect program:

child = pexpect.spawn('some command that expects password')
child.expect('Enter password:')  
child.sendline('somepassword')
child.expect('Enter password:')  --- passwd may ask twice
child.sendline('somepassword')
child.expect(pexpect.EOF, timeout=None)
...

Upvotes: 1

Related Questions