How to continue he execution of python script after user change
Question: 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


Answer: 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`.

