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

时间:2019-06-19 04:59:45

标签: postgresql ssl nestjs typeorm

这是我第一次使用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

4 个答案:

答案 0 :(得分:10)

如果您使用 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
    }
  }
}

答案 1 :(得分:0)

如果有人遇到相同的问题,我通过添加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'],
};

答案 2 :(得分:0)

这是我在 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

答案 3 :(得分:-1)

  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