[英]Connect with psql from remote machine to remote postgres database with saved password
I am working on a remote Unix machine where I connect via SSH from my local machine. 我正在使用从本地计算机通过SSH连接的远程Unix计算机。 There is a Postgres database on another host that I need to connect from my remote machine using psql. 我需要使用psql从远程计算机连接到另一个主机上的Postgres数据库。
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
and database
values. 用我的username
, password
, hostname
, port
和database
值。 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). 这行得通,但是我没有找到为输入/输出文件添加参数的方法(不需要通过Postgres交互式环境)。
According to another SO question , I created a file ~/.pgpass
with the following format: hostname:port:database:username:password
根据另一个SO问题 ,我创建了一个文件~/.pgpass
,其格式如下: 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.) 然后chmod 0600 ~/.pgpass
但以以下方式连接时: psql -h hostname -U username -d database -w
psql忽略了这一点(我得到了以下失败的身份验证消息。)
psql: fe_sendauth: no password supplied. psql:fe_sendauth:不提供密码。
Note that my remote machine's name is different than my username, so this might also create a problem when creating the .pgpass
file. 请注意,我的远程计算机的名称与我的用户名不同,因此在创建.pgpass
文件时这也可能会产生问题。 Is there any other way to do it? 还有其他方法吗?
Any ideas? 有任何想法吗?
This worked OK but I did not find a way to add arguments for input/output files 这个工作正常,但是我没有找到为输入/输出文件添加参数的方法
You can either redirect the input file to psql, or specify it in -f
option: 您可以将输入文件重定向到psql,或在-f
选项中指定它:
psql < input.sql
psql -f input.sql
As for output file, just redirect psql's output to it: 至于输出文件,只需将psql的输出重定向到它:
psql -f input.sql > output.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.