【问题标题】:Failing to run migration Postgres on Heroku无法在 Heroku 上运行迁移 Postgres
【发布时间】:2021-09-25 07:18:31
【问题描述】:

好吧,当我进入 heroku bash 并尝试运行 npx typeorm migration:run 时,它只会给我一个错误:

奇怪的是,当 DATABASE 在本地主机上时,它在本地工作,就像 .env 文件中这样: DATABASE_URL=postgres://postgres:docker@localhost:5432/gittin

这是我的ormconfig.js:

module.exports = {
    "type": "postgres",
    "url": process.env.DATABASE_URL,
    "entities": ["dist/entities/*.js"],
    "cli": {
        "migrationsDir": "src/database/migrations",
        "entitiesDir": "src/entities"
    }
}

是的,我在应用中添加了 heroku postgres 插件。

PS:如果需要,这是项目的repo:https://github.com/joaocasarin/gittin

【问题讨论】:

  • 你见过this吗?
  • 我还找到了this。我认为您必须编辑ormconfig.js 并添加:ssl: process.env.NODE_ENV === 'production'。仅当 Node.js 环境是 production 时才启用 ssl,从而允许在 Heorku 中进行安全连接,并且“用于本地开发的非安全连接。
  • 嗨@CarloCorradini,我看不到第一个链接对我有什么帮助,因为我没有找到conf文件,而且连接仍然会请求激活SSL...关于您的第二条评论,我尝试了您的方式,但在我研究时,ormconfig.js 中的 ssl 字段不应是布尔值,它应该是这样的对象:gist.github.com/joaocasarin/85c29ca545ddc77cafcd8a5aa41af49a
  • 它有效,我在许多项目中使用过ssl :)。例如,看看here。 PS:你试过吗?
  • @CarloCorradini 是的,我试过了,这是新的错误:user-images.githubusercontent.com/48847394/…

标签: node.js postgresql heroku typeorm


【解决方案1】:

当我在 cmets 中与 Carlo 讨论时,我必须在 ormconfig.js 中添加 ssl 属性,但不仅要在环境为 production 时将其设置为 true。所以根据this,我必须在生产模式下输入{ rejectUnauthorized: false },而不是在非生产模式下。

所以ormconfig.js 现在是这样的:

module.exports = {
    "type": "postgres",
    "ssl": process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false,
    "url": process.env.DATABASE_URL,
    "entities": ["dist/entities/*.js"],
    "cli": {
        "migrationsDir": "src/database/migrations",
        "entitiesDir": "src/entities"
    }
}

【讨论】:

  • 既然这是解决方案,请将其标记为正确答案:)
  • @CarloCorradini 我等了 2 天才能将其选为正确,感谢您记住我!
猜你喜欢
  • 1970-01-01
  • 2021-06-17
  • 2018-10-17
  • 2015-10-25
  • 2021-05-23
  • 1970-01-01
  • 2015-09-21
  • 2022-08-19
  • 2020-02-12
相关资源
最近更新 更多