PostgreSQL 的 SSH 端口转发密码问题
我正在尝试在我的计算机上连接到 PostgreSQL 数据库,该数据库只能在某些服务器上访问(数据库仅允许本地连接)。我想我可以像这样使用端口转发:
$ ssh someserver.com -L 5100:127.0.0.1:5432
然后像这样连接到数据库:
$ psql -h 127.0.0.1 -p 5100 -U postgres dbname
但是,问题是服务器上没有密码,但是当我尝试连接到转发的端口时,它会要求我输入密码。当我将其留空时,它会返回“psql:fe_sendauth:未提供密码”并且不允许我连接。
可能是什么原因?我的计算机上还有一个 PostgreSQL 实例,这会导致一些冲突吗?
I'm trying to connect on my computer to a PostgreSQL database which is only accessible on some server (db allows only local connections). I thought I could use port forwarding like this:
$ ssh someserver.com -L 5100:127.0.0.1:5432
and then connect to the database like this:
$ psql -h 127.0.0.1 -p 5100 -U postgres dbname
However, the problem is that on the server there is no password, but when I try to connect to the forwarded port it asks me for a password. When I leave it empty it returns "psql: fe_sendauth: no password supplied" and doesn't let me connect.
What could be the reason? I have also a PostgreSQL instance on my computer, could this cause some conflicts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您说数据库仅允许本地连接时:您需要区分“unix socket”连接和“TCP to localhost”连接,它们不是同一件事,并且可能会受到完全不同的对待。
测试:在数据库服务器上,查看在 psql 命令行中添加
-h 127.0.0.1
是否会影响是否可以连接。是否需要密码的规则位于 pg_hba.conf 中。您的数据库服务器可能设置为对 unix(“本地”)连接使用“ident”身份验证,因此您的 Unix 用户名会自动接受为数据库用户名。由于该身份验证方法通常不适用于 TCP/IP(“主机”)连接,因此您必须使用其他方法 - 如果将其设置为“md5”,则它将需要密码。
也许您所要做的就是在本地连接时为数据库上的用户帐户设置密码:
ALTER USER username PASSWORD 'password'
。如果您想避免在远程连接时一直提示输入密码,请编写.pgpass
文件将其存储在您的客户端用户帐户中:http://www.postgresql.org/docs/9.0/interactive/libpq-pgpass.htmlWhen you say that the db allows only local connections: you need to distinguish between "unix socket" connections and "TCP to localhost" connections, which are not the same thing and may be treated quite differently.
To test: on the database server, see if adding
-h 127.0.0.1
onto the psql commandline makes a difference in whether you can connect or not.The rules for whether a password is required or not are in
pg_hba.conf
. It might be that your database server is set to use "ident" authentication for unix ("local") connections, so your Unix username is automatically accepted as the database username. Since that authentication method isn't generally available for TCP/IP ("host") connections, you have to use some other method- if it is set to "md5", then it will require a password.Probably all you have to do is set a password for your user account on the database while connected locally:
ALTER USER username PASSWORD 'password'
. If you want to avoid being prompted for a password all the time when connecting remotely, write a.pgpass
file to store it in your client user account: http://www.postgresql.org/docs/9.0/interactive/libpq-pgpass.html