我关注这篇文章((http://nodeexamples.com/2012/09/21/connecting-to-a-postgresql-database-from-node-js-using-the-pg-module/).我已经将我的应用程序部署到heroku,目前正在使用express、node.js,try 连接到我刚刚安装的Heroku中的PostgresSQL数据库.到了文章的最后,我使用了这个命令

node myfile.js

我得到了这个错误

error: no pg_hba.conf entry for host "...", user "...", database "...", ... 

我该如何创建一个应用程序,我应该把它放在我的应用程序目录中的什么地方?

下面是整个错误消息.我更改了IP地址、用户和数据库的字符串,但看起来基本上是一样的.

events.js:72
    throw er; // Unhandled 'error' event
          ^
error: no pg_hba.conf entry for host "00.000.000.00", user "username", database "databasename", SSL off
at Connection.parseE (/Users/user/workspace/MyApp/app/node_modules/pg/lib/connection.js:526:11)
at Connection.parseMessage (/Users/user/workspace/MyApp/app/node_modules/pg/lib/connection.js:356:17)
at Socket.<anonymous> (/Users/user/workspace/MyApp/app/node_modules/pg/lib/connection.js:105:22)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:748:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:410:10)
at emitReadable (_stream_readable.js:406:5)
at readableAddChunk (_stream_readable.js:168:9)
at Socket.Readable.push (_stream_readable.js:130:10)

编辑:我做了更多的研究,发现"pg_hba".conf的文件在我的

/usr/local/var/postgres 

我在"pg_hba"中添加了这一行.配置文件

# TYPE  DATABASE        USER            ADDRESS                 METHOD
 host   all             all                                     trust

也试过

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host   all             all              0.0.0.0/0               md5

但它一直说我的主机、用户、数据库等没有条目...

推荐答案

更改连接代码以使用ssl.下面是您的链接示例:

var conString = "pg://admin:guest@localhost:5432/Employees";
var client = new pg.Client(conString);
client.connect();

变成:

var client = new pg.Client({
    user: "admin",
    password: "guest",
    database: "Employees",
    port: 5432,
    host: "localhost",
    ssl: true
}); 
client.connect();

https://github.com/brianc/node-postgres/wiki/Client#new-clientobject-config--client

Node.js相关问答推荐

如何在Node js中从MongoDB获取特定数据,使用GET方法?

聚合发布/订阅消息

Mongoose查询-如何根据当前查找ID获取其他集合并将其插入到当前查找中?

JEST模拟由http服务器控制器导入的ES模块

函数声明中的异步在没有等待的函数中做什么?

GitLab 依赖扫描需要源代码中的 package-lock.json 才能执行

为什么它无法发送发布请求并更改为 chrome 中的获取方法?

Node.js 上的 CLI 应用程序如何通过 child_process 将选项值作为参数传递给 Shell 命令

从 Response.json() 返回的 JSON 似乎无效?

MERN 堆栈项目中的 React [create-react-app] 正在提供依赖项

部署云功能 Firebase/Stripe 时出错

baseurl64 缓冲区解码

解决并行保存到 mongodb

node.js 中 res.setHeader 和 res.header 的区别

Puppeteer 错误:未下载 Chromium 修订版

如何可靠地散列 JavaScript 对象?

如何监控 node.js 上的网络,类似于 chrome/firefox 开发者工具?

如何在 node 中转义 shell 命令的字符串?

从 node.js 连接到 mongodb 时出现 ECONNREFUSED 错误

react-native run-android 无法识别