简体   繁体   English

连接到 Heroku PostgreSQL 数据库时的身份验证错误

[英]Authentication error when connecting to Heroku PostgreSQL database

I'm developing a Node.js application using PostgreSQL and hosting on Heroku.我正在使用 PostgreSQL 开发 Node.js 应用程序并托管在 Heroku 上。 My problem is that I get an authentication error like so:我的问题是我收到这样的身份验证错误:

14:32:05 web.1     | { [error: no pg_hba.conf entry for host "193.40.244.196", user "username", database "database_name", SSL off]
14:32:05 web.1     |   length: 168,
14:32:05 web.1     |   name: 'error',
14:32:05 web.1     |   severity: 'FATAL',
14:32:05 web.1     |   code: '28000',
14:32:05 web.1     |   detail: undefined,
14:32:05 web.1     |   hint: undefined,
14:32:05 web.1     |   position: undefined,
14:32:05 web.1     |   internalPosition: undefined,
14:32:05 web.1     |   internalQuery: undefined,
14:32:05 web.1     |   where: undefined,
14:32:05 web.1     |   file: 'auth.c',
14:32:05 web.1     |   line: '483',
14:32:05 web.1     |   routine: 'ClientAuthentication' }

It might be an SSL problem, but it shouldn't be as mentioned here .这可能是 SSL 问题,但不应该像这里提到的那样。 SSL should be supported out of the box.开箱即用应支持 SSL。 So I'm stumped and can only ask what might cause this error?所以我很难过,只能问什么可能导致这个错误?

I'm not sure if I have to maybe edit the pg_hba.conf on my system, but I can't even find it.我不确定是否必须在我的系统上编辑 pg_hba.conf,但我什至找不到它。

Solved it by setting PGSSLMODE ( http://www.postgresql.org/docs/9.0/static/libpq-envars.html ) on Heroku.通过在 Heroku 上设置PGSSLMODE ( http://www.postgresql.org/docs/9.0/static/libpq-envars.html ) 解决了这个问题。 It tells PostgreSQL to default to SSL.它告诉 PostgreSQL 默认使用 SSL。

$ heroku config:set PGSSLMODE=require

node-postgres doesn't support SSL in it's javascript bindings, which you're using if you do: node-postgres 在它的 javascript 绑定中不支持 SSL,如果你这样做,你正在使用它:

var pg = require('pg');

To get SSL, you need to use the native binding by doing this:要获得 SSL,您需要通过执行以下操作来使用本机绑定:

var pg = require('pg').native;

You don't need to use SSL when your app is running inside Heroku, you only need to use SSL to connect remotely (when your app is running locally).当您的应用在 Heroku 中运行时,您不需要使用 SSL,您只需要使用 SSL 进行远程连接(当您的应用在本地运行时)。

I added these params and can now connect to my heroku postgres instance from an outside server, specifically, in configuration of knex.js in a node express server:我添加了这些参数,现在可以从外部服务器连接到我的 heroku postgres 实例,特别是在节点 express 服务器中的 knex.js 配置中:

var knex = require('knex')({
  client: 'postgres',
  connection: 'postgres://username:password@host:5432/yourdbname?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory'
});

Ran into same issue.遇到同样的问题。 Just Enabled ssl=true in the db params.刚刚在 db 参数中启用了 ssl=true。

var pg = require('pg');
var params = { host: 'heroku_hostname',user: 'username',password: 'password',database: 'database',ssl: true };
var client = new pg.Client(params);
client.connect();

Ran into this myself, it's a simple fix.我自己遇到了这个,这是一个简单的修复。 Just connect over HTTPS instead只需通过 HTTPS 连接即可

I too received the 'ClientAuthentication' no pg_hba.conf entry error in my Heroku PRODUCTION instance in April 2021. I was and continue to use the Plan "Hobby Dev" free account.我也在 2021 年 4 月在我的 Heroku PRODUCTION 实例中收到了'ClientAuthentication' no pg_hba.conf entry错误。我曾经并继续使用 Plan“Hobby Dev”免费帐户。

Heroku began rolling out changes to the platform in February 2021 that enforced SSL for all Heroku Postgres client connections. Heroku 于 2021 年 2 月开始对平台进行更改,为所有 Heroku Postgres 客户端连接强制实施 SSL。 All Heroku Postgres client connections require SSL .所有 Heroku Postgres 客户端连接都需要 SSL

I fixed my PRODUCTION instance (where my NextJS App connected to the Heroku Postgres add-on) by adding the following config ssl: { rejectUnauthorized: false } when using node-postgres v8 :我修复了我的 PRODUCTION 实例(我的 NextJS 应用程序连接到 Heroku Postgres 插件),方法是在使用node-postgres v8时添加以下配置ssl: { rejectUnauthorized: false }

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

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

I just resolved the issue by adding the ssl: { rejectUnautorized: false } to my database configuration.我刚刚通过将ssl: { rejectUnautorized: false }添加到我的数据库配置中解决了这个问题。 Also, run heroku config:set PGSSLMODE=no-verify -a appName另外,运行heroku config:set PGSSLMODE=no-verify -a appName

NOTE: "appName" should be the name of your hosted application注意:“appName”应该是您托管的应用程序的名称

Had the same issue.有同样的问题。 Here's what helped me.这对我有帮助。

new Sequelize(process.env.DATABASE_URL, {
    dialectOptions: {
      ssl: {
        require: true,
        rejectUnauthorized: false
      }
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM