Skip to content

Update 1-connecting.mdx #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions content/features/1-connecting.mdx
Original file line number Diff line number Diff line change
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