连接到Heroku PostgreSQL数据库时发生身份验证错误
我正在使用PostgreSQL开发Node.js应用程序,并在Heroku上托管。我的问题是我遇到了如下身份验证错误:连接到Heroku PostgreSQL数据库时发生身份验证错误,postgresql,node.js,authentication,node-postgres,Postgresql,Node.js,Authentication,Node Postgres,我正在使用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 |
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' }
这可能是一个SSL问题,但不应该像前面提到的那样。SSL应该得到开箱即用的支持。所以我很困惑,只能问是什么导致了这个错误
我不确定是否需要在我的系统上编辑pg_hba.conf,但我甚至找不到它。我自己也遇到过,这是一个简单的修复。只需通过HTTPS连接即可节点postgres在其javascript绑定中不支持SSL,如果您:
var pg = require('pg');
要获取SSL,您需要通过以下操作使用本机绑定:
var pg = require('pg').native;
当你的应用程序在Heroku内部运行时,你不需要使用SSL,你只需要使用SSL进行远程连接(当你的应用程序在本地运行时)。遇到同样的问题。刚刚在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();
我添加了这些参数,现在可以从外部服务器连接到我的heroku postgres实例,特别是在node express服务器中配置knex.js:
var knex = require('knex')({
client: 'postgres',
connection: 'postgres://username:password@host:5432/yourdbname?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory'
});
通过在Heroku上设置
PGSSLMODE
()解决了此问题。它告诉PostgreSQL默认为SSL
$ heroku config:set PGSSLMODE=require
2021年4月,我在Heroku生产实例中也收到了
'ClientAuthentication'
无pg_hba.conf条目
错误。我是并将继续使用计划“爱好开发”免费帐户
Heroku于2021年2月开始对平台进行更改,对所有Heroku Postgres客户端连接实施SSL
我修复了我的生产实例(其中我的NextJS应用程序连接到Heroku Postgres加载项),方法是在使用时添加以下配置ssl:{rejectUnauthorized:false}
:
通常,只需将端口强制设置为与heroku config一起列出的5572即可。“只需通过HTTPS连接即可”。你能详细说明一下吗?您的意思是按照David Pelaez的建议指定端口5572吗?这对我来说不起作用,在参数中指定ssl:true也不能像其他人建议的那样,也不能同时指定两者。我使用的是javascript绑定,不是本机绑定。运行SSL不再需要本机绑定。为我工作了…谢谢!(如果您正在使用Sequelize,请将
native:true
添加到新Sequelize
选项对象)这需要npm i pg native
设置pg.defaults.ssl=true代码>连接之前。感谢您提供的knex示例!你在哪里找到这些信息的?不确定!但我看到它是有文档记录的,所以也许它就在这里:这也帮助我使用heroku postgre dbAgreed开发了本地nodejs。这是最好的答案,因为它使我的本地和远程版本以最少的代码工作。这确实是最准确的答案,因为提供的其他解决方案没有使用process.env.DATABASE\urlu!几个小时后,这就是我的解决方案。谢谢。我在升级postgresql实例时遇到了这个问题。PGSSLMODE是修复程序。非常感谢!这应该是答案。
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});