node.js - 无法使用 Node 示例从heroku local连接到heroku postgres

标签 node.js heroku heroku-postgres heroku-cli

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

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

但仍然无法连接,我添加了一个console.log来查看错误:

“错误:主机“62.90.xxx.yyy”、用户“用户名”、数据库“密码”、SSL 关闭没有 pg_hba.conf 条目

现在怎么办?

最佳答案

经过大量搜索后发现,添加“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'))
....

关于node.js - 无法使用 Node 示例从heroku local连接到heroku postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49606515/

相关文章:

node.js - 尝试启动 Atom/Nuclide 时未找到流

angularjs - 使用 Webpack 构建的 Heroku 上的 Angular 应用程序 - 环境变量?

node.js - Heroku、NodeJs 和 React 问题 : SCRIPT5007: Unable to get property 'apply' of undefined or null reference

ruby-on-rails - Heroku不会为Rails 3.2创建postgres数据库表(以及数据库本身)

javascript - 我们如何在 nodeJS 中使用 module.exports 返回函数?

node.js - 根据用户类型限制 API 访问的正确方法是什么?

mysql - 为什么即使我在 POSTMAN 中插入数据,我的数据库仍显示未定义?

python - 将 Pandas 部署到 Heroku?

node.js - NodeJS-SequelizeConnectionError : self signed certificate

postgresql - Heroku 评论应用程序 : copy DB to review app