I am working on a remote Unix machine where I connect via SSH from my local machine. There is a Postgres database on another host that I need to connect from my remote machine using psql.
I would like to be able to run queries from an input file and save the results to an output file, however I did not manage to automatically set the password, surpassing the need to do it every time I run a query.
I tried the following:
Run
psql "user=username password=password host=hostname port=port dbname=database"`with my
username,password,hostname,portanddatabasevalues. This worked OK but I did not find a way to add arguments for input/output files (not needing to go through Postgres interactive environment).According to another SO question, I created a file
~/.pgpasswith the following format:hostname:port:database:username:passwordand then
chmod 0600 ~/.pgpassbut when connecting as:psql -h hostname -U username -d database -wpsql ignored this (I got the following failed authentication message.)psql: fe_sendauth: no password supplied.
Note that my remote machine's name is different than my username, so this might also create a problem when creating the
.pgpassfile. Is there any other way to do it?
Any ideas?
psqlinvocation, but I doubt that matters in this context.sudoto callpsql. Are you usingsudoto callpsql?PGPASSWORD=password psql -h hostname -U username -d database -f inputfile -o outputfile