Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot connect an SSL secured database to typeorm

This is my first time using NestJS and I am having trouble connecting my Postgres database which is hosted on Digitalocean to NestJS.

I searched online for solutions and tried adding "ssl": "true" or "extra": { "ssl": "true" }

Heres my 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"]
}

I expect it to connect to the server. The error I'm getting is [TypeOrmModule] Unable to connect to the database. error: no pg_hba.conf entry for host "", user "", database "", SSL off

like image 239
bbousq Avatar asked Jun 19 '19 04:06

bbousq


People also ask

How do I enable SSL in typeorm?

1 Define TypeORM environment variables. DATABASE_URL is the name of the default environment variable (or “config var” as heroku likes to call it) that contains the full database connection string. 2 Enable SSL. ... 3 Allow self-signed certificates. ... 4 Prevent overrides from the node TLS core module. ...

How to encrypt a connection from SQL Server management studio?

To encrypt a connection from SQL Server Management Studio: 1 On the Object Explorer toolbar, click Connect, and then click Database Engine. 2 In the Connect to Server dialog box, complete the connection information, and then click Options. 3 On the Connection Properties tab, click Encrypt connection. More ...

Is it possible to allow SSL connections with self-signed certificates?

Now, this is a painful point… the node stack itself forbids SSL connections encrypted with self-signed certificates and it overrides the behaviour of any library since it comes from the core modules. It is possible to allow them by setting NODE_TLS_REJECT_UNAUTHORIZED="0", but I would rather not.

Does the URL parameter in the typeorm config contain sslmode= require?

But I was using the url parameter in the typeorm config, and the url from DO contains sslmode=require at the end. It turns out that the sslmode parmeter in the url overwrites the ssl-parameter in config, s o the ca parameter was never set. See: node-postgres.com/features/ssl But what's actually stored in that SSL_CERT environment variable??


1 Answers

If anyone has the same issue, I fixed it by adding a field for ssl and setting my ca certificate that I got from Digital Ocean. Heres what my ormconfig looks like:

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'],
};
like image 177
bbousq Avatar answered Sep 21 '22 15:09

bbousq