无法使用Sequelize从本地节点应用程序连接到Heroku Postgresql数据库

编程入门 行业动态 更新时间:2024-10-24 19:20:31
本文介绍了无法使用Sequelize从本地节点应用程序连接到Heroku Postgresql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Sequelize从本地nodejs应用程序连接到Heroku postgresql数据库.我遵循了这两个指南,在Heroky服务器端一切正常,但是当我在Mac上本地运行时,我的节点应用程序将无法连接到heroku.

I'm trying to connect to a Heroku postgresql database from a local nodejs app with Sequelize. I followed this two guides an everything is working perfectly fine on the heroky server side, but my node app won't connect to heroku when I run it locally on my Mac.

  • sequelizejs/articles/heroku
  • devcenter.heroku/articles/从heroku的外部连接到heroku-postgres-databases

这是我启动本地应用程序的方式:

Here is how I start the local app:

DATABASE_URL=$(heroku config:get DATABASE_URL) nodemon

得到我:

Sequelize: Unable to connect to the database:

但是我通过执行以下操作获得了正确的URL:

But I get the correct URL by doing this:

echo $(heroku config:get DATABASE_URL)

这些命令运行正常:

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

这是我的nodejs代码:

Here is my nodejs code :

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() plete(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); }); } });

我尝试将 native:true 添加到sequelize选项中,但是随后我得到了:

I tried to add native: true to the sequelize options, but then I get:

/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.

But i'd rather use Sequelize.

推荐答案

好,通过浏览sequelize源代码找到了答案: github/sequelize/sequelize/blob/master/lib/dialects/postgres/connection-manager.js#L39

OK, found the answer by browsing sequelize source code : github/sequelize/sequelize/blob/master/lib/dialects/postgres/connection-manager.js#L39

要为PG连接激活SSL,您不需要 native:true 或 ssl:true ,但是 dialectOptions.ssl:true 以下的方法终于奏效了:

To activate SSL for PG connections you don't need native: true or ssl: true but dialectOptions.ssl: true so the following did finally work:

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

要解决在 SequelizeConnectionError:自签名证书您可以改用:

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

更多推荐

无法使用Sequelize从本地节点应用程序连接到Heroku Postgresql数据库

本文发布于:2023-10-23 13:45:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   连接到   应用程序   数据库   Sequelize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!