如何在 sql_error_code = 28000(主机没有 pg_hba.conf 条目)时正确设置 Heroku Node.js 与 Heroku-Postgres 的连接

How to properly setup Heroku Node.js Connection to Heroku-Postgres when sql_error_code = 28000 (no pg_hba.conf entry for host) occurs

我想弄清楚如何设置我的后端 api (next.js/api) 到数据库 (postgresql),两者都由 heroku 托管。

在 pg.pool 的调解下,我使用以下代码进行设置。

const pool = new Pool(
    {
        connectionString: process.env.DATABASE_URL,
        // ssl: {
        //     rejectUnauthorized: false,
        //   }
    })

但被 heroku 返回并出现以下错误:

sql_error_code = 28000 FATAL: no pg_hba.conf entry for host "122.180.247.11", user "u3idolso5k2v83", database "dc85788d13v9ej", SSL off

错误描述来自: https://help.heroku.com/DR0TTWWD/seeing-fatal-no-pg_hba-conf-entry-errors-in-postgres

编辑:意味着 post 这个 link、

The authentication failed because the connection didn't use SSL encryption: (SSL off). All Heroku Postgres production databases require using SSL connections to ensure that communications between applications and the database remain secure. If your client is not using SSL to connect to your database, you would see these errors even if you're using the right credentials to connect to it.

我觉得这很奇怪,因为 heroku 默认已经为我的服务器提供了 ssl,所以发生这样的错误完全出乎意料?

我在网上遇到的旁路解决方案是取消对连接中的 ssl 属性 的注释...这有效,但我对这个感到不安。

const pool = new Pool(
    {
        connectionString: process.env.DATABASE_URL,
         ssl: {
             rejectUnauthorized: false,
           }
    })

如前所述,这里不安全:https://security.stackexchange.com/questions/229282/is-it-safe-to-set-rejectunauthorized-to-false-when-using-herokus-postgres-datab

我完全不明白为什么会出现此错误,以及如何通过适当的安全措施修复它。

Postgres 服务器的 SSL 证书无效是很正常的。即使是官方的 postgres 客户端也不验证证书。您使用的库默认用于验证证书,但只占少数。

在为 https://www.atdatabases.org/docs/pg-options 设置时,我让它默认不验证证书以匹配 Postgres 的标准行为。

这让您可以简单地为 heroku 创建一个连接池:

import createConnectionPool from '@databases/pg';

createConnectionPool(process.env.DATABASE_URL);

如您的链接答案中所述,您可以升级到支持此功能的 Heroku 付费产品之一。或者你可以停止使用 Heroku。或者你可以忍受有人对你进行 MITM 的极低风险。

I don't understand why this error occur at all,

不明白怎么办?您链接到的解释似乎很清楚。如果您不能比目前更清楚地表述您的不确定性,那么谁能帮助您理解?