首页 > 解决方案 > 无法使用 typeorm 将 nodejs 应用程序连接到 heroku 上的 Postrgres 数据库

问题描述

我正在使用 typeorm 连接到我的 nodejs 应用程序上的 Postgres 数据库。我能够在我的本地成功连接,但是在部署到 heroku 后,根据我的数据库配置,我遇到了这些错误。

import { createConnection } from 'typeorm';
...

export const databaseConnection = async (databaseURL:string) => {
    try {
        await createConnection({
            type: "postgres",
            url: databaseURL,
            entities: [User],
            synchronize: true,
            ssl:  false
        });
        

    } catch (error) {
    }
};

databaseConnection("<DATABASE_URL_FROM_HEROKU>")

当我这样做时,我得到了错误no pg_hba.conf entry for host , userdatabase SSL off at Parser.parseErrorMessage

当我ssl: true在连接中使用时,我得到了错误:Error: self signed certificate at TLSSocket.onConnectSecure heroku

当我完全删除 ssl 属性时,它仍然不起作用。

我遇到的大多数解决方案都是在这里使用 sequelize 完成的。但我正在使用 typeorm。我请我摆脱这个错误。

标签: node.jspostgresqlherokutypeorm

解决方案


你有没有尝试过这样的:

await createConnection({
  type: "postgres",
  url: databaseURL,
  entities: [User],
  synchronize: true,
  ssl: {
    rejectUnauthorized: false
  }
});

推荐阅读