我正在远程Unix服务器上工作,我是通过本地机器上的SSH连接到服务器的。另外有一台Postgres数据库主机,我需要从我的远程机器使用psql连接到它。
我想能够从一个输入文件运行查询并保存结果到输出文件,但是我没有成功自动设置密码,每次运行查询时都需要手动输入密码。
我尝试了以下方法:
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?
psql
调用中没有指定端口,但我认为在这种情况下这并不重要。 - zb226sudo
调用psql
。您是否正在使用sudo
调用psql
? - zb226