连接到Heroku PostgreSQL数据库时validation错误

我正在开发一个使用PostgreSQL的Node.js应用程序,并在Heroku上托pipe。 我的问题是,我得到这样的authentication错误:

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连接即可

通过在Heroku上设置PGSSLMODEhttp://www.postgresql.org/docs/9.0/static/libpq-envars.html )来解决这个问题。 它告诉PostgreSQL默认为SSL。

 $ heroku config:set PGSSLMODE=require 

node-postgres在它的javascript绑定中不支持SSL,如果你这样做的话就使用它:

 var pg = require('pg'); 

要获得SSL,您需要通过执行以下操作来使用本地绑定:

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

当您的应用程序在Heroku中运行时,您不需要使用SSL,只需使用SSL进行远程连接(当您的应用程序在本地运行时)。

我添加了这些参数,现在可以从外部服务器连接到我的heroku postgres实例,具体来说,在节点快递服务器中configurationknex.js:

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

跑到同样的问题。 只是在数据库参数中启用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();