Skip to content Skip to sidebar Skip to footer

Os.system With Embedded Commands

I've now been struggling a long time and still can't get this line of code working : os.system('su - postgres -c 'psql -c \'ALTER USER postgres WITH ENCRYPTED PASSWORD \\\'{0}\\\';

Solution 1:

Urgh. Please use psycopg2, the native Python client driver for PostgreSQL, if at all possible.

If you must shell out to psql, use the subprocess module's check_call function, which is easier and safer as it takes care of quoting for you.

import subprocess
subprocess.check_call([
    'psql', '-c',
    'ALTER USER postgres WITH ENCRYPTED PASSWORD "{0}";'.format(self.password)
])

os.system should really be a last resort IMO.

Solution 2:

Password should be in single quotes -

postgres=# createuser usr with password "password";
ERROR:  syntax error ator near ""password""
LINE 1: createuser usr with password "password";
                                      ^
postgres=# createuser usr with password 'password';
CREATE ROLE

Post a Comment for "Os.system With Embedded Commands"