错误:用户身份验证失败

Posted

技术标签:

【中文标题】错误:用户身份验证失败【英文标题】:error: Ident authentication failed for user 【发布时间】:2015-12-01 07:37:18 【问题描述】:

我无法通过 nodejs pg 模块或 pgAdmin3 连接 postgres 以下是我的配置和错误消息,我找不到解决方法

用户、密码、角色权限、数据库创建

su - postgres

psql

ALTER USER db_admin WITH PASSWORD ‘aaee11’;

ALTER ROLE db_admin SUPERUSER CREATEDB CREATEROLE;


CREATE DATABASE “testdb” WITH OWNER = db_admin ENCODING = 'UTF8' TABLESPACE = pg_default LC_COLLATE = 'en_GB.UTF-8' LC_CTYPE = 'en_GB.UTF-8' CONNECTION LIMIT = -1 TEMPLATE template0;

全部听

vi /var/lib/pgsql/9.4/data/postgresql.conf

#listen_addresses = 'localhost'
listen_addresses = '*'

重启

service postgresql-9.4 restart

nodejs pg 连接和错误信息

var conString = 'postgres://db_admin: aaee11@localhost/testdb';
var dbClient = new pg.Client(conString);
dbClient.connect(function(error) 
  if (error) 
    console.log(error);
  
);

[root@li61-123 app]# node --harmony app.js staging
Express server listening on port 8002
 [error: Ident authentication failed for user "db_admin"]
  name: 'error',
  length: 94,
  severity: 'FATAL',
  code: '28000',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'auth.c',
  line: '285',
  routine: 'auth_failed' 

pg_hba.conf 配置

vi /var/lib/pgsql/9.4/data/pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

更新

编辑pg_hba.confswitch 所有方法到 md5 后,它可以在 nodejs 中连接,但在 pgadmin 中仍然无法显示如下错误消息:

Server doesn't listen
The server doesn't accept connections: the connection library reports 
could not connect to server: Operation timed out Is the server running on host "xx.xxx.xxx.xxx" and accepting TCP/IP connections on port 5432? 
If you encounter this message, please check if the server you're trying to contact is actually running PostgreSQL on the given port. Test if you have network connectivity from your client to the server host using ping or equivalent tools. Is your network / *** / SSH tunnel / firewall configured correctly? 
For security reasons, PostgreSQL does not listen on all available IP addresses on the server machine initially. In order to access the server over the network, you need to enable listening on the address first. 
For PostgreSQL servers starting with version 8.0, this is controlled using the "listen_addresses" parameter in the postgresql.conf file. Here, you can enter a list of IP addresses the server should listen on, or simply use '*' to listen on all available IP addresses. For earlier servers (Version 7.3 or 7.4), you'll need to set the "tcpip_socket" parameter to 'true'. 
You can use the postgresql.conf editor that is built into pgAdmin III to edit the postgresql.conf configuration file. After changing this file, you need to restart 

pgadmin

[Properties]
Name: xx.xxx.xxx.xxx
Host: xx.xxx.xxx.xxx
Port: 5432
Maintenance: postgres
Username: db_admin
Password: ........

【问题讨论】:

psql: FATAL: Ident authentication failed for user "postgres"的可能重复 @AndyRay 我之前检查过但找不到答案,我还尝试将 pg_hba.conf 配置本地和主机方法更新为 md5,仍然无法正常工作 【参考方案1】:

根据 Postgresql pg_hba.conf documentation ident auth-method 如下:

通过联系客户端上的ident服务器获取客户端的操作系统用户名,检查是否匹配 请求的数据库用户名。身份验证只能用于 TCP/IP 连接。当为本地连接指定时,对等 将改为使用身份验证。

你需要切换到md5,然后在psql中运行select pg_reload_conf();或者重启postgres服务。

【讨论】:

感谢回复,切换到md5后就可以了,但是在pgadmin中还是无法连接。如何在pgadmin中设置 确保重新启动 pgAdmin,您应该不会看到这种错误,因为您可以通过 psql 连接到同一端口 @user1575921,对于我的 pgAdmin,我只有 Name,Host ,Port,Maintenance DB,UsernamePassword 指定。 不需要指定[SSH] 或[SSH Tunnel] 对吧?仅 [属性] 不,我假设您没有使用它。你有哪个版本的pgAdmin? @user1575921 psql -h xx.xxx.xxx.xxx -U db_admin -d postgres 命令是否在安装 pgAdmin 的地方工作?如果不是 - 您需要检查防火墙设置。

以上是关于错误:用户身份验证失败的主要内容,如果未能解决你的问题,请参考以下文章

为啥sql server身份验证失败

MapServer错误:致命:用户“...”的身份验证失败

Azure AD:50155 设备身份验证失败

PostgreSQL - 用户“postgres”的对等身份验证失败

(iOS) AWS S3 上传失败且没有错误(使用联合身份验证的用户 - Apple SSO)

IIS 7.5 中的 Windows 身份验证失败