Skip to content

Commit

Permalink
Add example use for password function (#126)
Browse files Browse the repository at this point in the history
Feature introduced in brianc/node-postgres#1926.
  • Loading branch information
JoelVenable committed Apr 9, 2021
1 parent 40393b8 commit eabf9c3
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions content/features/1-connecting.mdx
Expand Up @@ -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.
Expand Down

0 comments on commit eabf9c3

Please sign in to comment.