Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot correctly use "psql" command-line via subprocess.call in my script

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?

like image 209
Luis Masuelli Avatar asked Apr 14 '26 19:04

Luis Masuelli


1 Answers

(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'})
like image 51
Luis Masuelli Avatar answered Apr 17 '26 09:04

Luis Masuelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!