3

I am trying to run a command like this in python:

subprocess.call(['psql', '--username=openerp', '--dbname=sf_template', '--no-password', '--command="select pg_terminate_backend(procpid) from pg_stat_activity where datname = \'sf_template\' and procpid <> pg_backend_pid()"'], env={'PGPASSWORD': 'mypassword'})

And I get the following verbose output:

psql: FATAL:  password authentication failed for user "<myCurrentOSuser>"

But if I run this command by shell:

PGPASSWORD=mypassword
psql --username=openerp --dbname=sf_template --no-password --command="select pg_terminate_backend(procpid) from pg_stat_activity where datname = 'sf_template' and procpid <> pg_backend_pid()"

It works properly.

Why? What am I missing here?

3
  • 1
    You don't need the quotes Commented Jun 4, 2015 at 22:56
  • 1
    To find out how the argument list should look like, call shlex.split(r'''psql --username=openerp --dbname=sf_template --no-password --command="select pg_terminate_backend(procpid) from pg_stat_activity where datname = 'sf_template' and procpid <> pg_backend_pid()"''') Commented Jun 5, 2015 at 11:21
  • 2
    use env=dict(os.environ, PGPASSWORD='mypassword') to avoid clearing the environment. Commented Jun 5, 2015 at 11:25

1 Answer 1

2

(Thanks for the first three commenters - I solved the problem with your help).

I sent the whole arguments to subprocess.call as one single string (did not fully understand the array syntax).

subprocess.call(['psql --username=openerp --dbname=sf_template --no-password --command="select pg_terminate_backend(procpid) from pg_stat_activity where datname = \'sf_template\' and procpid <> pg_backend_pid()"'], env={'PGPASSWORD': 'mypassword'})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.