Pergunta

I am trying to connect to a psql server remotely from within a python script. Despite the fact that I have set .pgpass properly, when I run the script the remote server prompts for password, in which case pressing enter will cause it to read the password from .pgpass successfully.

Since I plan to set a cron job running this script, I want to avoid the password prompt. Most searches here say setting PGPASS or PGPASSFILE would eliminate the password prompt but it doesn't in my case.

I understand I can use ORM libs for connecting, but it would be an overkill as this query is a one liner that will never change.

Any ideas how I can get rid of the password prompt?

os.environ["PGPASSFILE"]=".pgpass"
dest=os.path.dirname(os.path.abspath(__file__))
#get the newest data from the table                                                                                                                
subprocess.call("psql -U username --no-password -h host.address -W mydatabasename -c  '\copy (select ....) To '%s/latest.csv' With CSV'"%dest,shell=True)
Foi útil?

Solução

Solved the problem, -W modifier in the query forces psql to prompt for password, removing -W and it works fine.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top