我已经在我的Ubuntu karma box上安装了PostgreSQL和pgAdminIII。
我能够成功地使用pgAdminIII(即连接/登录),但是当我尝试在命令行(使用psql)上使用相同的用户名/pwd登录到服务器时,我得到错误:
psql: FATAL: Ident authentication failed for user "postgres"
现在有人知道如何解决这个问题吗?
我已经在我的Ubuntu karma box上安装了PostgreSQL和pgAdminIII。
我能够成功地使用pgAdminIII(即连接/登录),但是当我尝试在命令行(使用psql)上使用相同的用户名/pwd登录到服务器时,我得到错误:
psql: FATAL: Ident authentication failed for user "postgres"
现在有人知道如何解决这个问题吗?
当前回答
我在Mac OSX的PostgreSQL 9.3上的解决方案是在bash shell中使用sudo进入数据文件夹,然后将必要的行追加到pg_hba.conf文件,以允许所有用户都是可信的,并能够登录。这就是我所做的:
# in bash_profile edit PGDATA environmental variable
open ~/.bash_profile
# append this line to bash_profile
export PGDATA="/Library/PostgreSQL/9.3/data"
# reload bash_profile
source ~/.bash_profile
# open pg_hba.conf in vim
sudo vi /Library/PostgreSQL/9.3/data/pg_hba.conf
# append these two lines to the end of the pg_hba.conf file
local all all trust
host all all 127.0.0.1/32 trust
# can now login as user in bash
psql -d <db_name> -U <user_name> -W
其他回答
嗯…
如果可以使用pgAdminIII中的用户名和密码连接,但不能使用psql连接,那么这两个程序连接到数据库的方式可能不同。
[如果您连接到不同的数据库,首先尝试连接到相同的数据库。见下文。)
From PostgreSQL: Documentation: 9.3: psql:
如果省略主机名,psql将通过unix域套接字连接到本地主机上的服务器,或者在没有unix域套接字的机器上通过TCP/IP连接到localhost。
如果你没有运行类似psql的程序…-h host_name…如果你运行的是Ubuntu, psql应该通过unix域套接字连接,所以PostgreSQL可能没有配置为允许postgres用户的密码验证方法之一。
你可以通过运行:
sudo -u postgres psql
如果上述工作,您的服务器可能被配置为使用postgres用户对本地连接进行对等身份验证,即请求操作系统提供您的用户名以确认您是postgres。
所以这可能是你的pg_hba.conf文件
该文件的完整路径将类似于/etc/postgresql/9.3/main/pg_hba.conf。可以通过sudo cat /etc/postgresql/9.3/main/pg_hba.conf | more查看。
如果你在psql命令中忽略了主机名,如果你在pg_hba.conf文件中添加以下条目,你应该能够连接:
# Connection type Database User IP addresses Method
local all postgres md5
[pg_hba.conf文件中的注释行以#开头。]
如果你在psql命令中包含主机名,添加以下条目:
# Connection type Database User IP addresses Method
host all postgres 127.0.0.1/32 md5
您需要在通过psql为您的连接匹配任何其他条目之前放置该条目。如果不知道该把它放在哪里,就把它放在第一个未注释的行之前。
更多关于pg_hba.conf的信息
pg_hba.conf文件[粗体强调]:
具有匹配的连接类型、客户端地址、请求的数据库和用户名的第一条记录用于执行身份验证。没有“失败”或“备份”:如果选择了一条记录并且认证失败,则不考虑后续记录。如果没有匹配的记录,则拒绝访问。
请注意,在认证方法上没有匹配记录。所以,如果你的pg_hba.conf文件包含以下条目:
# Connection type Database User IP addresses Method
local all postgres peer
那么你将无法通过以下方式连接:
psql -u postgres
除非你的pg_hba.conf文件中有一个条目在前一个条目的上面:
# Connection type Database User IP addresses Method
local all postgres md5
local all postgres password # Unencrypted!
local all all md5
local all all password # Unencrypted!
对于fedora26和postgres9.6
首先,以root用户登录,然后通过以下命令进入psql
$ su postgres
then
$ psql
在PSQL中查找hba_file ==>的位置意味着pg_hba.conf
postgres=# show hba_file ;
hba_file
--------------------------------------
/etc/postgresql/9.6/main/pg_hba.conf
(1 row)
在pg_hba.conf文件中将用户访问权限更改为this
host all all 127.0.0.1/32 md5
在Ubuntu 12.04中为Rails开发设置PostgreSQL之后,我也遇到了同样的问题
我尝试了其他答案,但我所要做的就是:“config/database.yml”
development:
adapter: postgresql
encoding: unicode
database: (appname)_development
pool: 5
username: (username you granted appname database priviledges to)
password:
在pg_hba.conf中设置正确了吗?
参见https://ubuntu.com/server/docs/databases-postgresql如何做。
我花了更多的时间来解决这个我愿意承认的错误。
我认为pg_hba.conf中的身份验证配置顺序与您的情况相关。默认配置文件在普通安装中包含几行。这些默认值可以匹配身份验证尝试的条件,从而导致身份验证失败。不管在.conf文件末尾添加了什么额外的配置,它都会失败。
要检查使用的是哪一行配置,请确保查看消息的默认日志文件。你可能会看到这样的东西
LOG: could not connect to Ident server at address "127.0.0.1", port 113: Connection refused
FATAL: Ident authentication failed for user "acme"
DETAIL: Connection matched pg_hba.conf line 82: "host all all 127.0.0.1/32 ident"
结果是这条默认行导致了拒绝。
host all all 127.0.0.1/32 ident
试着把它注释掉。