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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10279965/
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-10-20 23:50:46  来源:igfitidea点击:

Authentication error when connecting to Heroku PostgreSQL database

postgresqlnode.jsauthenticationnode-postgres

提问by j0ntech

I'm developing a Node.js application using PostgreSQL and hosting on Heroku. My problem is that I get an authentication error like so:

我正在使用 PostgreSQL 开发一个 Node.js 应用程序并在 Heroku 上托管。我的问题是我收到如下身份验证错误:

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 should be supported out of the box. So I'm stumped and can only ask what might cause this error?

这可能是 SSL 问题,但不应该像这里提到的那样。SSL 应该是开箱即用的。所以我很难过,只能问可能导致这个错误的原因是什么?

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,但我什至找不到它。

采纳答案by Jeff Dickey

Ran into this myself, it's a simple fix. Just connect over HTTPS instead

我自己遇到了这个,这是一个简单的修复。只需通过 HTTPS 连接

回答by jede

Solved it by setting PGSSLMODE(http://www.postgresql.org/docs/9.0/static/libpq-envars.html) on Heroku. It tells PostgreSQL to default to SSL.

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

$ heroku config:set PGSSLMODE=require

回答by matt

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'tneed 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远程连接(如果您的应用程序在本地运行)。

回答by adrichman

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 实例,特别是在节点快速服务器中的 knex.js 配置中:

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

回答by Avinash

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();