postgresql 可以通过没有密码的本地主机连接到 postgres,但不能通过 127.0.0.1。为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27005952/
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
Can connect to postgres through localhost without password but not through 127.0.0.1. Why?
提问by silmeth
I have Kubuntu 14.10 desktop with PostgreSQL 9.4 database installed.
I have changed the password of the postgres
user in the database by executing the SQL:
我有安装了 PostgreSQL 9.4 数据库的 Kubuntu 14.10 桌面。我postgres
通过执行 SQL更改了数据库中用户的密码:
ALTER USER postgres PASSWORD 'password';
And I can connect to DB server by psql -h localhost -U postgres -W
and giving that password butI can also connect without a password requirement by simply psql -h localhost -U postgres
.
我可以通过psql -h localhost -U postgres -W
并提供该密码连接到数据库服务器,但我也可以通过简单的psql -h localhost -U postgres
.
On the other hand, if I run psql -h 127.0.0.1 -U postgres
it prompts me for the password set before.
另一方面,如果我运行psql -h 127.0.0.1 -U postgres
它会提示我输入之前设置的密码。
What is the difference between the localhost
and 127.0.0.1
hosts and their login method? Where is it set? I see no localhost
-related entries in the pg_hba.conf
file.
localhost
和127.0.0.1
主机之间的区别及其登录方法是什么?它在哪里设置?我localhost
在pg_hba.conf
文件中没有看到相关条目。
回答by Mike Sherrill 'Cat Recall'
The behavior you see mightbe caused by a password file. The password file is conventionally named ~/.pgpass on Unix systems, but a different filename can be given through the PGPASSFILE environmental variable.
您看到的行为可能是由密码文件引起的。密码文件在 Unix 系统上通常命名为 ~/.pgpass,但可以通过 PGPASSFILE 环境变量指定不同的文件名。
I think a password file that contains a line for "localhost", but does notcontain a line for "127.0.0.1" will show the behavior you're seeing. My own ~/.pgpass file contains this line.
我认为,包含“localhost”的一条线,但密码文件不包含“127.0.0.1”行会显示的行为,你看到。我自己的 ~/.pgpass 文件包含这一行。
localhost:*:*:postgres:password
Here's what happens when I try to connect just like you did.
当我像您一样尝试连接时,会发生以下情况。
$ psql -h localhost -U postgres psql (9.3.5) SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256) Type "help" for help. sandbox=# \q $ psql -h 127.0.0.1 -U postgres Password for user postgres:
Adding the line 127.0.0.1:*:*:postgres:password
to ~/.pgpass lets me log in using 127.0.0.1 and no password.
将该行添加127.0.0.1:*:*:postgres:password
到 ~/.pgpass 允许我使用 127.0.0.1 登录,无需密码。
$ psql -h 127.0.0.1 -U postgres psql (9.3.5) SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256) Type "help" for help. sandbox=#