【问题标题】:Heroku/Postgres "ClientAuthentication" Error with Node and node-postgres节点和 node-postgres 的 Heroku/Postgres“ClientAuthentication”错误
【发布时间】:2021-06-04 17:24:34
【问题描述】:

我正处于使用 Postgres 在 Heroku 上设置开发应用程序的早期阶段。 NodeJS中问题代码的一个简单版本是:

const {Client} = require('pg');
const client = new Client({connectionString: 'postgres://{username}:{password}@{host}:5432/{dbname}'});
client.connect();
const result = client.query('SELECT now()');
client.end();

connectionString 是从 data.heroku.com 的凭据窗格中提供的字符串的副本,经过检查和重新检查。我可以:

  • 使用来自多个不同本地应用程序(例如 DataGrip)的连接字符串进行连接
  • 使用该代码(使用不同的连接字符串)连接到在 Docker 映像中运行的本地版本的 Postgres

我不能:

  • 从本地计算机上的 Node 应用程序连接
  • 从部署到 Heroku 的 Node 应用程序连接

当代码对远程数据库失败时,node-postgres 会抛出此错误:

{
        "length": 168,
        "name": "error",
        "severity": "FATAL",
        "code": "28000",
        "file": "auth.c",
        "line": "496",
        "routine": "ClientAuthentication"
    }

使用 Node v14.15.1,node-postgres ("pg") 8.5.1

更新: 可能还值得一提的是,我无法找到一种方法来从 Java 中获得与 fail 的这种连接……故障肯定在 Node node-postgres [基础 postgres 驱动程序] 中的某个地方 Heroku(即,数据库很好,连接也很好)……但是在哪里?

【问题讨论】:

    标签: node.js postgresql heroku heroku-postgres node-postgres


    【解决方案1】:

    在创建client 对象时添加ssl: { rejectUnauthorized: false } 为我解决了这个问题。 (source)

    const client = new Client({
        connectionString: 'postgres://{username}:{password}@{host}:5432/{dbname}',
        ssl: { rejectUnauthorized: false }
    });
    

    如果其他代码没有解决任何问题,可以尝试添加 extra: { ssl: { rejectUnauthorized: false } } (source)

    【讨论】:

    • 嗨,@Paul……不幸的是,这不起作用。 Fwiw 如果从 Java 中调用,我无法获得此连接 fail,因此我倾向于认为这不是 SSL/cert 问题。想一想,这是一个值得添加到原始帖子中的数据点……
    • 嗨@RiqueW。你是说JavaScript吗?此外,字符串 'postgres://{username}:{password}@{host}:5432/{dbname}' 正是您使用的代码,或者您只是想隐藏凭据?
    • 嘿,@Paul……我指的是 Java 本身,而不是 Javascript,只是一个用于测试这种连接的小型实用程序。有趣的是,当我说我可以从桌面客户端连接时,我认为它们都在使用 JDBC 驱动程序,所以有这样的共性。
    • 是的,这些只是路径中的混淆变量。我最初使用 Heroku 提供的 URL,并且一直在尝试一些我认为可能会有所帮助的小变化,但到目前为止:没有
    • 嘿@Paul……你的回答是_so _ close 并且应该起作用了,imo。我不确定为什么我在下面使用 'pg-connection-string' 发布的基于 Heroku 支持的答案可以解决问题(但我现在会接受)
    【解决方案2】:

    在 Heroku 支持的帮助下,我找到了基于 https://help.heroku.com/MDM23G46/why-am-i-getting-an-error-when-i-upgrade-to-pg-8 的解决方案。 “解决方案 1”对我没有任何改变,但“解决方案 2”(使用 pg-connection-string)确实有效。

    值得一提的是,我还使用基于 NODE_ENV 环境变量的条件来包装 SSL,这样代码既可以与 Heroku Postgres 实例一起使用,也可以与同一个数据库的本地开发版本一起使用。即:

      // centralizing the logic for this so the 'useLocalDb' can be reused
      // in multiple functions in the module (in case the logic needs to change
      // later, among other reasons)
      const useLocalDb = process.env.USE_LOCAL_DB;
      
      // a short version of the working connection code
      const {Pool} = require('pg');
      const {parse} = require('pg-connection-string')
      const config = parse(process.env.DATABASE_URL)
      if (!useLocalDb) {
        config.ssl = {
          rejectUnauthorized: false
        }
      }
      const pool = new Pool(config);
    
    

    【讨论】:

    • 我不明白这会在节点应用程序中的什么位置。你能提供更多解释吗?
    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多