【问题标题】:cannot connect an SSL secured database to typeorm无法将 SSL 安全数据库连接到 typeorm
【发布时间】:2019-11-01 18:13:19
【问题描述】:

这是我第一次使用 NestJS,我无法将托管在 Digitalocean 上的 Postgres 数据库连接到 NestJS。

我在网上搜索了解决方案并尝试添加"ssl": "true" or "extra": { "ssl": "true" }

这是我的 ormconfig.json

{
  "type": "postgres",
  "host": "host",
  "port": "port",
  "username": "username",
  "password": "password",
  "database": "database",
  "extra": {
    "ssl": "true"
  },
  "synchronize": "true",
  "logging": "true",
  "entities": ["src/**/*.entity.ts", "dist/**/*.entity.js"]
}

我希望它连接到服务器。我得到的错误是[TypeOrmModule] Unable to connect to the database. error: no pg_hba.conf entry for host "", user "", database "", SSL off

【问题讨论】:

    标签: postgresql ssl nestjs typeorm


    【解决方案1】:

    如果有人遇到同样的问题,我会通过为 ssl 添加一个字段并设置我从 Digital Ocean 获得的 ca 证书来修复它。 这是我的 ormconfig 的样子:

    module.exports = {
      name: 'default',
      type: 'postgres',
      host: 'host',
      port: port,
      username: 'username',
      password: 'password',
      database: 'database',
      synchronize: true,
      dropSchema: false,
      logging: true,
      ssl: {
        ca: process.env.SSL_CERT,
      },
      entities: ['src/**/*.entity.ts', 'dist/**/*.entity.js'],
    };
    

    【讨论】:

    • 您好,我也有同样的问题。但是我没有任何ssl证书。你有什么建议或意见吗?还是必须获得ssl证书?你能帮我解决这个问题吗stackoverflow.com/questions/65136834/… 请。
    • 如果您有要连接的托管数据库,您需要从该平台下载 SSL 证书,然后您可以使用该证书。它可能类似于 ca_certificate.crt,您可以在云托管服务提供商文档中找到更多相关信息。
    • 我在 Digitalocean 上遇到了类似的问题。但是我在 typeorm 配置中使用了 url 参数,并且来自 DO 的 url 最后包含 sslmode=require 。事实证明,url 中的 sslmode 参数覆盖了 config 中的 ssl 参数,因此从未设置 ca 参数。见:node-postgres.com/features/ssl
    • 但是 SSL_CERT 环境变量中实际存储了什么??
    【解决方案2】:
      ssl: {
        rejectUnauthorized: false,
        ca: fs.readFileSync('/path/to/server-certificates/root.crt').toString(),
        key: fs.readFileSync('/path/to/client-key/postgresql.key').toString(),
        cert: fs.readFileSync('/path/to/client-certificates/postgresql.crt').toString(),
      },
    

    通过https://node-postgres.com/features/ssl

    【讨论】:

    • 这不是最好的答案,但这确实回答了实际问题,链接是正确的
    【解决方案3】:

    如果您使用 typeorm 从 localhost 连接到 heroku 上的 postgres 数据库,则此方法有效。

    ormconfig.json

    {
      "name": "default",
      "type": "postgres",
      "url": "postgres://username:password@host:port/database",
      "synchronize": true,
      "logging": true,
      "entities": ["src/entity/*.*"],
      "ssl": true,
      "extra": {
        "ssl": {
          "rejectUnauthorized": false
        }
      }
    }
    

    【讨论】:

    【解决方案4】:

    这是我在 Heroku 上的 NestJS TypeORM 配置:

    TypeOrmModule.forRoot({
      type: 'postgres',
      url: process.env.DATABASE_URL,
      autoLoadEntities: true,
      ssl:
        process.env.NODE_ENV === 'production'
          ? { rejectUnauthorized: false }
          : false,
    }),
    

    SSL 选项是强制性的,如下所述:https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js

    【讨论】:

      【解决方案5】:

      您可以将PQSSLMODE 环境变量设置为require - libpq 将自动读取这些变量(如果未另行设置),并建立安全连接。

      另请参阅:https://www.postgresql.org/docs/current/libpq-envars.html

      【讨论】:

        猜你喜欢
        • 2020-11-25
        • 2021-10-19
        • 1970-01-01
        • 2021-05-26
        • 1970-01-01
        • 2020-07-13
        • 2020-12-11
        • 1970-01-01
        • 2013-02-01
        相关资源
        最近更新 更多