我已经在我的Ubuntu karma box上安装了PostgreSQL和pgAdminIII。

我能够成功地使用pgAdminIII(即连接/登录),但是当我尝试在命令行(使用psql)上使用相同的用户名/pwd登录到服务器时,我得到错误:

psql: FATAL:  Ident authentication failed for user "postgres"

现在有人知道如何解决这个问题吗?


当前回答

对于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

其他回答

这招对我很管用: http://tecadmin.net/fatal-ident-authentication-failed-for-user-postgres/#

local   all             postgres                                trust
local   all             myapp_usr                               trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
#host    all             all             ::1/128                 trust

解决这个问题的一个方法是编辑pg_hba.conf

sudo vi /etc/postgresql/9.3/main/pg_hba.conf

暂时

# Database administrative login by Unix domain socket
local   all             postgres                                   trust

至此,您就完成了。为了安全,那就去吧

sudo -u postgres psql template1
ALTER USER postgres with encrypted password 'your_password';

然后返回,将pg_hba.conf设置为

# Database administrative login by Unix domain socket
local   all             postgres                                   md5

如果你在CentOS上使用它,你可能需要在做出上述解决方案后重新加载postgres:

systemctl restart postgresql-9.3.service

如果你做了所有这些,它仍然不工作,检查该用户的到期:

Postgres密码认证失败

嗯…

如果可以使用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!