From d5db1ea6eebc48d267be78e8a6aa29bff4811a02 Mon Sep 17 00:00:00 2001 From: Joel Venable <30368779+JoelVenable@users.noreply.github.com> Date: Thu, 8 Apr 2021 08:44:31 -0500 Subject: [PATCH 1/4] Update 1-connecting.mdx Documentation should mention supported feature. https://github.com/brianc/node-postgres/issues/2513 --- content/features/1-connecting.mdx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/content/features/1-connecting.mdx b/content/features/1-connecting.mdx index ceac920..cf0c99c 100644 --- a/content/features/1-connecting.mdx +++ b/content/features/1-connecting.mdx @@ -90,6 +90,20 @@ client.query('SELECT NOW()', (err, res) => { }) ``` +Many cloud providers also provide alternative methods of connecting to database instances using short-lived authentication tokens. node-postgres supports dynamic passwords via a callback function, either synchronous or asynchronous. The callback function must resolve to a string. + +``` + +const pool = new pg.Pool({ + host: 'db.example.com', + port: 5432, + user: 'user', + database: 'my-db', + password: async () => 'random-' + Date.now(), +}); + +``` + ### Programmatic Connection to Sockets Connections to unix sockets can also be made. This can be useful on distros like Ubuntu, where authentication is managed via the socket connection instead of a password. From fb78eaaa6a2ab6d7d888b8792c9424abab8b9998 Mon Sep 17 00:00:00 2001 From: Joel Venable <30368779+JoelVenable@users.noreply.github.com> Date: Fri, 9 Apr 2021 10:13:47 -0500 Subject: [PATCH 2/4] Update 1-connecting.mdx More realistic example. --- content/features/1-connecting.mdx | 35 +++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/content/features/1-connecting.mdx b/content/features/1-connecting.mdx index cf0c99c..875fa0d 100644 --- a/content/features/1-connecting.mdx +++ b/content/features/1-connecting.mdx @@ -90,18 +90,35 @@ client.query('SELECT NOW()', (err, res) => { }) ``` -Many cloud providers also provide alternative methods of connecting to database instances using short-lived authentication tokens. node-postgres supports dynamic passwords via a callback function, either synchronous or asynchronous. The callback function must resolve to a string. +Many cloud providers include alternative methods for connecting to database instances using short-lived authentication tokens. node-postgres supports dynamic passwords via a callback function, either synchronous or asynchronous. The callback function must resolve to a string. -``` +```js +const { Pool } = require('pg') +const { RDS } = require('aws-sdk') -const pool = new pg.Pool({ - host: 'db.example.com', - port: 5432, - user: 'user', - database: 'my-db', - password: async () => 'random-' + Date.now(), -}); +const signerOptions = { + credentials: { + accessKeyId: 'YOUR-ACCESS-KEY', + secretAccessKey: 'YOUR-SECRET-ACCESS-KEY' + }, + region: 'us-east-1', + hostname: 'example.aslfdewrlk.us-east-1.rds.amazonaws.com', + port: 5432, + username: 'api-user' +} + +const signer = new RDS.Signer() +/** @returns {string} */ +const getPassword = () => signer.getAuthToken(signerOptions) + +const pool = new Pool({ + host: signerOptions.hostname, + port: signerOptions.port, + user: signerOptions.username, + database: 'my-db', + password: getPassword, +}); ``` ### Programmatic Connection to Sockets From 561a12e76e1f3fdb75e9db92a3b9cfa3a7306565 Mon Sep 17 00:00:00 2001 From: Joel Venable <30368779+JoelVenable@users.noreply.github.com> Date: Fri, 9 Apr 2021 10:22:23 -0500 Subject: [PATCH 3/4] Fixed spacing --- content/features/1-connecting.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/features/1-connecting.mdx b/content/features/1-connecting.mdx index 875fa0d..6edc6ab 100644 --- a/content/features/1-connecting.mdx +++ b/content/features/1-connecting.mdx @@ -90,7 +90,7 @@ client.query('SELECT NOW()', (err, res) => { }) ``` -Many cloud providers include alternative methods for connecting to database instances using short-lived authentication tokens. node-postgres supports dynamic passwords via a callback function, either synchronous or asynchronous. The callback function must resolve to a string. +Many cloud providers include alternative methods for connecting to database instances using short-lived authentication tokens. node-postgres supports dynamic passwords via a callback function, either synchronous or asynchronous. The callback function must resolve to a string. ```js const { Pool } = require('pg') From 80f61e041538ff0922e217f60ea595ec1eeb6a1a Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Fri, 9 Apr 2021 16:29:37 -0700 Subject: [PATCH 4/4] Make formatting more consistent for the merge --- content/features/1-connecting.mdx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/content/features/1-connecting.mdx b/content/features/1-connecting.mdx index 6edc6ab..ef9945a 100644 --- a/content/features/1-connecting.mdx +++ b/content/features/1-connecting.mdx @@ -99,17 +99,16 @@ const { RDS } = require('aws-sdk') const signerOptions = { credentials: { accessKeyId: 'YOUR-ACCESS-KEY', - secretAccessKey: 'YOUR-SECRET-ACCESS-KEY' + secretAccessKey: 'YOUR-SECRET-ACCESS-KEY', }, region: 'us-east-1', hostname: 'example.aslfdewrlk.us-east-1.rds.amazonaws.com', port: 5432, - username: 'api-user' + username: 'api-user', } const signer = new RDS.Signer() -/** @returns {string} */ const getPassword = () => signer.getAuthToken(signerOptions) const pool = new Pool({ @@ -118,7 +117,7 @@ const pool = new Pool({ user: signerOptions.username, database: 'my-db', password: getPassword, -}); +}) ``` ### Programmatic Connection to Sockets