无法将 SSL 安全数据库连接到 typeorm

bbo*_*usq 7 postgresql ssl typeorm nestjs

这是我第一次使用 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"]
}
Run Code Online (Sandbox Code Playgroud)

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

bbo*_*usq 21

如果有人遇到同样的问题,我通过为 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'],
};
Run Code Online (Sandbox Code Playgroud)

  • 但 SSL_CERT 环境变量中实际存储了什么? (3认同)

noo*_*oor 12

如果您使用 typeorm 从本地主机连接到 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
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


thi*_*ign 7

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

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

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