首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Postgresql:我可以使用命令行连接,但不能使用node-postgres连接

Postgresql:我可以使用命令行连接,但不能使用node-postgres连接
EN

Stack Overflow用户
提问于 2016-03-15 20:49:07
回答 2查看 818关注 0票数 0

当我使用CLI连接到我的数据库时,一切工作正常。

代码语言:javascript
运行
复制
psql dbname postgres

psql询问我之前设置的密码(使用ALTER),然后我就连接上了。

在我的pg_hba.conf文件中,我得到了以下行:

代码语言:javascript
运行
复制
local   all             postgres                                md5

但是当我尝试使用node-postgres做同样的事情时,我总是得到一个错误:

代码语言:javascript
运行
复制
could not connect to postgres { [error: Ident authentication failed for user "postgres"]

我使用的代码是basic (假设我的密码是mypwd)

代码语言:javascript
运行
复制
var pg = require('pg');
var conString = "postgres://postgres:mypwd@localhost/database";

var client = new pg.Client(conString);
client.connect(function(err) {
  if(err) {
    return console.error('could not connect to postgres', err);
  }
  client.query('SELECT NOW() AS "theTime"', function(err, result) {
    if(err) {
      return console.error('error running query', err);
    }
    console.log(result.rows[0].theTime);
    //output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
    client.end();
  });
});

你有什么想法吗?

编辑: My full pg_hba.conf

代码语言:javascript
运行
复制
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             postgres                                md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
EN

回答 2

Stack Overflow用户

发布于 2016-03-15 21:33:36

"local“和"localhost”是有区别的。第一个将通过unix域套接字连接,第二个将通过tcp/ip连接到(通常) 127.0.0.1

您应该会在pg_hba.conf中看到它们的单独行。

您通常可以通过将路径(例如/var/run/postgresql/)放在服务器名称的位置来连接到unix域套接字。

票数 1
EN

Stack Overflow用户

发布于 2016-03-15 21:33:44

实际上,正如alexander.polomodov注意到的那样,问题出在pg_hba.conf文件中。我只是把"ident“这个词改成了"md5”,这样就行了。我不明白为什么我需要把它放在这两行中:

代码语言:javascript
运行
复制
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

因此,如果您可以解释,请随时发表评论。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36011812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档