Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue he execution of python script after user change

I want to execute following commands in sequence using a python script:

sudo su - postgres   #login as postgres user
psql

I tried using :

import os

cmd = 'sudo su - postgres'
os.system(cmd)

cmd1='psql'
os.system(cmd1)

The problem with this is that the 2nd command gets executed only after I log out from the postgres user, but I want to run it as postgres user. How can I can I continue the execution of python script after the user change?

Thanks

like image 487
nish Avatar asked Jan 18 '26 03:01

nish


1 Answers

You can use:

sudo su - postgres --command='cat … | psql …'

But you shouldn't. You should configure your database server to allow a user running your python script access to your database without password. Or at least use .pg_pass file in this user home directory to provide username and password for this database.

If you use PGPASSWORD, as you indicated in a comment, then any other user in your system can display it simply using ps auxwww.

like image 147
Tometzky Avatar answered Jan 20 '26 18:01

Tometzky