无法使用节点示例从 heroku local 连接到 heroku postgres

Can't connect to heroku postgres from heroku local using node sample

我按照以下步骤操作: https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

当我 运行 heroku 本地时它无法连接 - 我看到它正在使用 process.env.DATABASE_URL 并使用以下方法将它获取到我的本地 .env 文件: https://devcenter.heroku.com/articles/heroku-local

但是还是连接不上,我加了一个console.log看错误:

"error: no pg_hba.conf entry for host "62.90.xxx.yyy",用户 "username",数据库 "password",SSL 关闭"=12=]

现在怎么办?

经过大量搜索后发现添加 'pg.defaults.ssl = true' 解决了我的问题,同时让我尽可能接近 Heroku 提供的示例。

这是我的代码

const cool = require('cool-ascii-faces');
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000

const pg = require('pg');
pg.defaults.ssl = true; //this is it!!!

express()
  .use(express.static(path.join(__dirname, 'public')))
  .set('views', path.join(__dirname, 'views'))
....

一个简洁的解决方案是只为 Heroku 服务器连接启用 SSL。为此,将 dialectOptions.ssl=true 添加到 config.json 文件中的 Heroku 环境:

  {
"production": {
    "use_env_variable": "DATABASE_URL",
    "dialectOptions": { "ssl": true },
    "logging": false
  }