diff --git a/docs/connection-options.md b/docs/connection-options.md index 485b8d17fb..cd9915732f 100644 --- a/docs/connection-options.md +++ b/docs/connection-options.md @@ -173,6 +173,8 @@ See [SSL options](https://github.com/mysqljs/mysql#ssl-options). * `schema` - Schema name. Default is "public". +* `connectTimeoutMS` - The milliseconds before a timeout occurs during the initial connection to the postgres server. If `undefined`, or set to `0`, there is no timeout. Defaults to `undefined`. + * `ssl` - Object with ssl parameters. See [TLS/SSL](https://node-postgres.com/features/ssl). * `uuidExtension` - The Postgres extension to use when generating UUIDs. Defaults to `uuid-ossp`. Can be changed to `pgcrypto` if the `uuid-ossp` extension is unavailable. diff --git a/src/driver/postgres/PostgresConnectionOptions.ts b/src/driver/postgres/PostgresConnectionOptions.ts index aa70411217..18e65183b9 100644 --- a/src/driver/postgres/PostgresConnectionOptions.ts +++ b/src/driver/postgres/PostgresConnectionOptions.ts @@ -33,6 +33,12 @@ export interface PostgresConnectionOptions extends BaseConnectionOptions, Postgr }; + /** + * The milliseconds before a timeout occurs during the initial connection to the postgres + * server. If undefined, or set to 0, there is no timeout. Defaults to undefined. + */ + readonly connectTimeoutMS?: number; + /** * The Postgres extension to use to generate UUID columns. Defaults to uuid-ossp. * If pgcrypto is selected, TypeORM will use the gen_random_uuid() function from this extension. diff --git a/src/driver/postgres/PostgresDriver.ts b/src/driver/postgres/PostgresDriver.ts index a31755dd09..b45ac1c935 100644 --- a/src/driver/postgres/PostgresDriver.ts +++ b/src/driver/postgres/PostgresDriver.ts @@ -925,13 +925,15 @@ export class PostgresDriver implements Driver { credentials = Object.assign({}, credentials, DriverUtils.buildDriverOptions(credentials)); // todo: do it better way // build connection options for the driver + // See: https://github.com/brianc/node-postgres/tree/master/packages/pg-pool#create const connectionOptions = Object.assign({}, { host: credentials.host, user: credentials.username, password: credentials.password, database: credentials.database, port: credentials.port, - ssl: credentials.ssl + ssl: credentials.ssl, + connectionTimeoutMillis: options.connectTimeoutMS }, options.extra || {}); // create a connection pool