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
,port
anddatabase
values. 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
~/.pgpass
with the following format:hostname:port:database:username:password
and then
chmod 0600 ~/.pgpass
but when connecting as:psql -h hostname -U username -d database -w
psql 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
.pgpass
file. Is there any other way to do it?
Any ideas?