我正在try 使用Sequelize从本地nodejs应用程序连接到Heroku postgresql数据库.我遵循了这两个指南,heroky服务器端的一切都运行得很好,但当我在Mac上本地运行时,我的 node 应用程序无法连接到heroku.

以下是我如何启动本地应用程序:

DATABASE_URL=$(heroku config:get DATABASE_URL) nodemon

让我明白:

Sequelize: Unable to connect to the database:

但我通过这样做得到了正确的URL:

echo $(heroku config:get DATABASE_URL)

这些命令运行良好:

heroku pg:psql
psql $(heroku config:get DATABASE_URL)

以下是我的nodejs代码:

var match = process.env.DATABASE_URL.match(/postgres:\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)/)
sequelize = new Sequelize(match[5], match[1], match[2], {
    dialect:  'postgres',
    protocol: 'postgres',
    port:     match[4],
    host:     match[3],
    logging: false
})

sequelize
.authenticate()
.complete(function(err) {
    if (!!err) {
        log('Sequelize: Unable to connect to the database:', err);
    } else {
        http.listen(process.env.PORT || config.server.port, function(){
            log('Web server listening on port '+process.env.PORT || config.server.port);
        });
    }
});

我试图在sequelize选项中添加native: true个,但后来我得到了:

    /Users/clement/Projets/XMM/node_modules/sequelize/lib/sequelize.js:188
      throw new Error('The dialect ' + this.getDialect() + ' is not supported.
            ^
Error: The dialect postgres is not supported. (Error: Please install postgres package manually)
    at new module.exports.Sequelize (/Users/clement/Projets/XMM/node_modules/sequelize/lib/sequelize.js:188:13)
    at Object.<anonymous> (/Users/clement/Projets/XMM/server.js:17:14)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

即使在做了以下事情之后:

npm install pg
npm install -g pg
brew install postgresql

顺便说一下,这是可行的:

var pg = require('pg');
pg.connect(process.env.DATABASE_URL+'?ssl=true', function(err, client, done) {
    if (err) return console.log(err);
    client.query('SELECT * FROM pg_catalog.pg_tables', function(err, result) {
        done();
        if(err) return console.error(err);
        console.log(result.rows);
    });
});

但我宁愿使用Sequelize.

推荐答案

好的,通过浏览sequelize源代码找到答案:

要激活PG连接的SSL,您不需要native: truessl: true,但需要dialectOptions.ssl: true,因此以下操作最终奏效:

sequelize = new Sequelize(process.env.DATABASE_URL, {
    dialect: 'postgres',
    protocol: 'postgres',
    dialectOptions: {
        ssl: true
    }
});

要解决SequelizeConnectionError: self signed certificate中提到的node-postgres版本8上的self signed certificate错误,您可以使用:

sequelize = new Sequelize(process.env.DATABASE_URL, {
    dialect: 'postgres',
    protocol: 'postgres',
    dialectOptions: {
        ssl: {
            require: true,
            rejectUnauthorized: false
        }
    }
});

Postgresql相关问答推荐

在Haskell中定义新类型与持久化类型的惯用方法

是否可以在psql查询输出中删除新行末尾的+号?

PostGresql :正则表达式 Select 其中只有一个正斜杠的行

使用FastAPI和PostgreSQL进行测试

ST_Intersects 太慢

错误:分区表的唯一约束必须包括所有分区列

我在try 访问我的数据库表时在 postgresql 中收到 aclcheck_error 错误

gorm 创建并返回值 many2many

从 PostgreSQL 中的每个组中抽取 N 个样本

PostgresQL:获取两个数组之间的对应数量

Regexp_replace 行为会改变,如果它用空白字符串或 null 替换字符串数组中的字符串

使用包含重复元素的数组 Select 重复值

如何使用 pg_dump 或 psql 从 *.sql 恢复 PostgreSQL 表?

如何使 Java 和 Postgres 枚举一起工作以进行更新?

用于更改 postgresql 用户密码的 bash 脚本

在 Postgresql 中拆分逗号分隔的字段并对所有结果表执行 UNION ALL

获取json列键为空的记录

使用 Spring、Hibernate 和 C3P0 管理多租户 Web 应用程序中的连接池

重命名 Amazon RDS 主用户名

如何在 PostgreSQL 中生成一系列重复数字?