Skip to content

Commit fb78eaa

Browse files
authored
Update 1-connecting.mdx
More realistic example.
1 parent d5db1ea commit fb78eaa

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

Diff for: content/features/1-connecting.mdx

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

93-
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.
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.
9494

95-
```
95+
```js
96+
const { Pool } = require('pg')
97+
const { RDS } = require('aws-sdk')
9698

97-
const pool = new pg.Pool({
98-
host: 'db.example.com',
99-
port: 5432,
100-
user: 'user',
101-
database: 'my-db',
102-
password: async () => 'random-' + Date.now(),
103-
});
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()
104111

112+
/** @returns {string} */
113+
const getPassword = () => signer.getAuthToken(signerOptions)
114+
115+
const pool = new Pool({
116+
host: signerOptions.hostname,
117+
port: signerOptions.port,
118+
user: signerOptions.username,
119+
database: 'my-db',
120+
password: getPassword,
121+
});
105122
```
106123

107124
### Programmatic Connection to Sockets

0 commit comments

Comments
 (0)