diff --git a/docs/connection-options.md b/docs/connection-options.md index e0ec1f1e0d..a965de94d1 100644 --- a/docs/connection-options.md +++ b/docs/connection-options.md @@ -191,6 +191,8 @@ See [SSL options](https://github.com/mysqljs/mysql#ssl-options). * `installExtensions` - A boolean to control whether to install necessary postgres extensions automatically or not (default: `true`) +* `applicationName` - A string visible in statistics and logs to help referencing an application to a connection (default: `undefined`) + ## `sqlite` connection options * `database` - Database path. For example "./mydb.sql" diff --git a/src/driver/postgres/PostgresConnectionOptions.ts b/src/driver/postgres/PostgresConnectionOptions.ts index 1353c3713f..c0f62046ab 100644 --- a/src/driver/postgres/PostgresConnectionOptions.ts +++ b/src/driver/postgres/PostgresConnectionOptions.ts @@ -67,4 +67,10 @@ export interface PostgresConnectionOptions extends BaseConnectionOptions, Postgr * Automatically install postgres extensions */ readonly installExtensions?: boolean; + + /** + * sets the application_name var to help db administrators identify + * the service using this connection. Defaults to 'undefined' + */ + readonly applicationName?: string; } diff --git a/src/driver/postgres/PostgresDriver.ts b/src/driver/postgres/PostgresDriver.ts index cdc6cc8cbd..7aac3019d2 100644 --- a/src/driver/postgres/PostgresDriver.ts +++ b/src/driver/postgres/PostgresDriver.ts @@ -1101,7 +1101,8 @@ export class PostgresDriver implements Driver { database: credentials.database, port: credentials.port, ssl: credentials.ssl, - connectionTimeoutMillis: options.connectTimeoutMS + connectionTimeoutMillis: options.connectTimeoutMS, + application_name: options.applicationName }, options.extra || {}); // create a connection pool diff --git a/test/functional/driver/postgres/specific-options.ts b/test/functional/driver/postgres/specific-options.ts new file mode 100644 index 0000000000..4c1342d11c --- /dev/null +++ b/test/functional/driver/postgres/specific-options.ts @@ -0,0 +1,24 @@ +import "reflect-metadata"; +import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../../utils/test-utils"; +import { Connection } from "../../../../src/connection/Connection"; +import { expect } from "chai"; + +describe("postgres specific options", () => { + let connections: Connection[]; + before(async () => connections = await createTestingConnections({ + enabledDrivers: ["postgres"], + driverSpecific: { + applicationName: "some test name" + } + })); + beforeEach(() => reloadTestingDatabases(connections)); + after(() => closeTestingConnections(connections)); + + it("should set application_name", () => Promise.all(connections.map(async connection => { + const result = await connection.query( + "select current_setting('application_name') as application_name" + ); + expect(result.length).equals(1); + expect(result[0].application_name).equals("some test name"); + }))); +});