diff --git a/content/features/1-connecting.mdx b/content/features/1-connecting.mdx index ceac920..ef9945a 100644 --- a/content/features/1-connecting.mdx +++ b/content/features/1-connecting.mdx @@ -90,6 +90,36 @@ 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. + +```js +const { Pool } = require('pg') +const { RDS } = require('aws-sdk') + +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() + +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 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.