Skip to content

Commit eabf9c3

Browse files
authored
Add example use for password function (#126)
Feature introduced in brianc/node-postgres#1926.
1 parent 40393b8 commit eabf9c3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

content/features/1-connecting.mdx

+30
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,36 @@ client.query('SELECT NOW()', (err, res) => {
9090
})
9191
```
9292

93+
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.
94+
95+
```js
96+
const { Pool } = require('pg')
97+
const { RDS } = require('aws-sdk')
98+
99+
const signerOptions = {
100+
credentials: {
101+
accessKeyId: 'YOUR-ACCESS-KEY',
102+
secretAccessKey: 'YOUR-SECRET-ACCESS-KEY',
103+
},
104+
region: 'us-east-1',
105+
hostname: 'example.aslfdewrlk.us-east-1.rds.amazonaws.com',
106+
port: 5432,
107+
username: 'api-user',
108+
}
109+
110+
const signer = new RDS.Signer()
111+
112+
const getPassword = () => signer.getAuthToken(signerOptions)
113+
114+
const pool = new Pool({
115+
host: signerOptions.hostname,
116+
port: signerOptions.port,
117+
user: signerOptions.username,
118+
database: 'my-db',
119+
password: getPassword,
120+
})
121+
```
122+
93123
### Programmatic Connection to Sockets
94124

95125
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.

0 commit comments

Comments
 (0)