【问题标题】:Can't connect to heroku postgres from heroku local using node sample无法使用节点示例从 heroku local 连接到 heroku postgres
【发布时间】:2020-01-20 06:55:19
【问题描述】:

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

当我在本地运行 heroku 时,它无法连接 - 我看到它正在使用 process.env.DATABASE_URL 并使用: https://devcenter.heroku.com/articles/heroku-local

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

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

现在呢?

【问题讨论】:

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


    【解决方案1】:

    经过大量搜索,事实证明添加 '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'))
    ....
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 2018-05-19
        • 2018-01-31
        • 1970-01-01
        • 1970-01-01
        • 2013-04-21
        • 2021-07-10
        • 2015-03-19
        • 2016-02-11
        相关资源
        最近更新 更多