postgresql 无法使用 MD5 方法从主机连接到在 VM 上运行的 Postgres
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14139017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot connect to Postgres running on VM from host machine using MD5 method
提问by Hugo Rodger-Brown
I have a VM set up with Vagrant that has Postgres running on it (on port 5432), forwarded to port 8280 on the host machine.
我有一个使用 Vagrant 设置的 VM,它在其上运行 Postgres(在端口 5432 上),转发到主机上的端口 8280。
I have set the password for the default user and I can connect locally just fine.
我已经为默认用户设置了密码,我可以在本地正常连接。
I have been trying to set up access from the host machine over port 8280, and I have been unable to get it working with 'MD5' as the trust method.
我一直试图通过端口 8280 设置从主机的访问,但我一直无法使用“MD5”作为信任方法。
I have set up postgresql.confto listen on all addresses:
我已设置postgresql.conf为侦听所有地址:
# postgresql.conf
listen_addresses = '*'
and I have configured pg_hab.confas follows:
我配置pg_hab.conf如下:
# pg_hab.conf
#TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 0.0.0.0/0 md5
With all of these settings, if I run the following command from my host machine:
使用所有这些设置,如果我从我的主机运行以下命令:
psql --host=127.0.0.1 --port=8280 --username=postgres -d mydb -c '\l'
I am prompted for the password, and then I get:
我被提示输入密码,然后我得到:
psql: FATAL: password authentication failed for user "postgres"
If I then change the METHOD from 'md5' to 'trust' I'm not asked for a password and I can connect as expected. My question is - why can't I connect using 'md5', which is what I want to be able to do? I know that the password I am entering is correct (I have changed it), but for some reason it isn't working.
如果我随后将 METHOD 从“md5”更改为“信任”,则不会要求我输入密码,我可以按预期进行连接。我的问题是 - 为什么我不能使用“md5”进行连接,这是我想要做的?我知道我输入的密码是正确的(我已经更改了它),但由于某种原因它不起作用。
回答by Guillermo Mansilla
I had the same exact problem. The issue was on the host side, basically the firewall was blocking the port I was using. So this is what I did (I am using OSX Mavericks)
我有同样的问题。问题出在主机端,基本上防火墙阻止了我正在使用的端口。所以这就是我所做的(我使用的是 OSX Mavericks)
Open the port (Host)
sudo ipfw add 7000 allow tcp from any to any dst-port 7001Modify Vagrantfile in order to allow portforwarding
config.vm.network "forwarded_port", guest: 5432, host: 7001Edit postgresql.conf (Guest)
listen_addresses = '*'Edit
pg_hba.conf(you might want to tune this better)host all all 0.0.0.0/0 md5Now, from the host connect normally using the port (in my case 7001) and 'localhost' as host address
打开端口(主机)
sudo ipfw add 7000 allow tcp from any to any dst-port 7001修改 Vagrantfile 以允许端口转发
config.vm.network "forwarded_port", guest: 5432, host: 7001编辑 postgresql.conf(访客)
listen_addresses = '*'编辑
pg_hba.conf(您可能想要更好地调整它)host all all 0.0.0.0/0 md5现在,从主机使用端口(在我的情况下为 7001)和“localhost”作为主机地址正常连接
回答by Craig Ringer
You need to set a password for the postgresuser. It does not have one by default, so you cannot connect.
您需要为postgres用户设置密码。默认情况下它没有,因此您无法连接。
ALTER USER postgres PASSWORD 'somepassword';
Your local connections probably work because they're using unix sockets with peerauthentication, not TCP/IP. If you use:
您的本地连接可能有效,因为它们使用带有peer身份验证的unix 套接字,而不是 TCP/IP。如果您使用:
psql -h 127.0.0.1 -U postgres postgres
on the VM, you'll probably find that that fails too, because you're actually testing TCP/IP based connections now.
在 VM 上,您可能会发现它也失败了,因为您现在实际上正在测试基于 TCP/IP 的连接。

