Node.js、PostgreSQL 错误:没有主机的 pg_hba.conf 条目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25000183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:43:22  来源:igfitidea点击:

Node.js, PostgreSQL error: no pg_hba.conf entry for host

node.jspostgresqlexpressheroku

提问by stcho

I am following this article ((http://nodeexamples.com/2012/09/21/connecting-to-a-postgresql-database-from-node-js-using-the-pg-module/). I have already deployed my app to heroku and currently using express, node.js, to try and connect to a PostgresSQL database in Heroku that I just installed. I get to the very end of the article and I use the command

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

node myfile.js

I get this error

我收到这个错误

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

How do I go about creating one and where in my app directory should I put it?

我该如何创建一个,我应该把它放在我的应用程序目录中的哪个位置?

Below is the entire error message. I changed the strings for IP address, user, and database but it looks basically just like it.

以下是整个错误消息。我更改了 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)


Edit: I did some more research and found that the 'pg_hba.conf' file is in my

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

/usr/local/var/postgres 

and I added this line into the 'pg_hba.conf' file

我将此行添加到“pg_hba.conf”文件中

# TYPE  DATABASE        USER            ADDRESS                 METHOD
 host   all             all                                     trust

also tried

也试过

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

but it keeps saying there is no entry for my host, user, database, etc... is my 'pg_hba.conf' syntax wrong in any way?

但它一直说我的主机、用户、数据库等没有条目......我的“pg_hba.conf”语法有什么问题吗?

回答by aembke

Change your connection code to use ssl. Following your linked example:

更改您的连接代码以使用 ssl。按照您的链接示例:

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

becomes:

变成:

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

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

回答by vizmi

In the case sequelize ignores all of your efforts to turn ssl on, you can try to convince pg to enable ssl for all conncetions by default:

如果 sequelize 忽略了您打开 ssl 的所有努力,您可以尝试说服 pg 默认为所有连接启用 ssl:

var pg = require('pg');
pg.defaults.ssl = true;

const Sequelize = require('sequelize');
const sequelize = new Sequelize('postgres://...');

回答by Simon Franzen

We ran into this error while upgrading the pg database on heroku from hobbytier to standard-0. SSL is required, but we didnt set it in our config.

我们在将 heroku 上的 pg 数据库从hobbytier升级到standard-0. SSL 是必需的,但我们没有在我们的配置中设置它。

Include in config when initialize new Sequelize(...)

初始化 new Sequelize(...) 时包含在配置中

"dialect": "postgres",
"dialectOptions": {
  "ssl": true
}

This trick was, that the ssl option is wrapped in dialectOptions. found here: https://github.com/sequelize/sequelize/issues/956#issuecomment-147745033

这个技巧是,ssl 选项包含在dialectOptions. 在这里找到:https: //github.com/sequelize/sequelize/issues/956#issuecomment-147745033

回答by tabish89

You can also use the ENVIRONMENT CONFIG VARIABLE 'PGSSLMODE' to 'require' via Heroku's web interface or CLI.

您还可以通过 Heroku 的 Web 界面或 CLI 使用 ENVIRONMENT CONFIG VARIABLE 'PGSSLMODE' 来“要求”。

Case: Postgres dB set up as a Heroku add-on and attached to app on a Heroku Dyno.

案例:Postgres dB 设置为 Heroku 插件并附加到 Heroku Dyno 上的应用程序。

Heroku provides some pretty good support on how to connect to one of its add-on databases; however, it unfortunately leaves out (or, I missed it) any mention of what do to enforce SSL since all Heroku dB tiers starting with Standard-0 enforces SSL by default.

Heroku 对如何连接到其附加数据库之一提供了一些很好的支持;然而,不幸的是,它没有提到(或者,我错过了)任何关于强制执行 SSL 的内容,因为从 Standard-0 开始的所有 Heroku dB 层默认情况下都强制执行 SSL。

回答by Openwell

Adding ?ssl=trueshould work to the end of the uri.

添加?ssl=true应该工作到 uri 的末尾。

var conString = "pg://admin:guest@localhost:5432/Employees?ssl=true";

回答by rahuljain1311

What worked for me was a combination of above answers and a comment(from @schybo)

对我有用的是上述答案和评论的组合(来自@schybo)

let cloud_config = {
  username: process.env.DB_USERNAME,
  database: process.env.DB_DATABASE,
  password: process.env.DB_PASSWORD,
  host: process.env.DB_HOSTNAME,
  port: 5432,
  ssl: true,
  dialect: 'postgres',
  dialectOptions: {
    "ssl": {"require":true }
  }
};

Use both ssl: true,and dialectOptions: { "ssl": {"require":true }}

同时使用ssl: true,dialectOptions: { "ssl": {"require":true }}

Commenton Sequelize issue which is also added to the docs.

评论Sequelize 问题,该问题也已添加到文档中。

回答by Edwin Ikechukwu Okonkwo

setting ssl: trueworked for me

设置ssl: true对我有用

let pg = require('pg');

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

回答by álvaro Agüero

You have to add

你必须添加

ssl: true

in json connection

在 json 连接中

    {
        host: 'myHost.com',
        user: 'myUser',     
        password: 'myPassword',
        database: 'myDb',
        port: 5432,
        ssl: true
    }

回答by James Broad

Just add a flag to the client initialisation:

只需在客户端初始化中添加一个标志:

Change

改变

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

To

const conString = "pg://admin:guest@localhost:5432/Employees"
const client = new pg.Client({
  connectionString: process.env.DATABASE_URL,
  ssl: true
});
client.connect();