Skip to content

Commit 92cb01d

Browse files
committed
Add documentation on using pgpass
1 parent 3240ea0 commit 92cb01d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

docs/pages/guides/_meta.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export default {
33
'async-express': 'Express with Async/Await',
44
'pool-sizing': 'Pool Sizing',
55
upgrading: 'Upgrading',
6+
pgpass: 'Using .pgpass',
67
}

docs/pages/guides/pgpass.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
title: Using .pgpass
3+
---
4+
5+
PostgreSQL supports a [.pgpass](https://www.postgresql.org/docs/current/libpq-pgpass.html) file for storing passwords. The file is located at `~/.pgpass` on Unix systems and `%APPDATA%\postgresql\pgpass.conf` on Windows. Each line in the file has the format:
6+
7+
```
8+
hostname:port:database:username:password
9+
```
10+
11+
You can use the [pgpass](https://www.npmjs.com/package/pgpass) module together with the `password` callback to look up passwords from your `.pgpass` file:
12+
13+
```js
14+
import pg from 'pg'
15+
import pgpass from 'pgpass'
16+
17+
const { Pool } = pg
18+
19+
const pool = new Pool({
20+
user: 'my-user',
21+
host: 'localhost',
22+
database: 'my-db',
23+
password: (config) => {
24+
return new Promise((resolve, reject) => {
25+
const connection = {
26+
host: config.host,
27+
port: config.port,
28+
database: config.database,
29+
user: config.user,
30+
}
31+
pgpass(connection, (password) => {
32+
resolve(password)
33+
})
34+
})
35+
},
36+
})
37+
```
38+
39+
The `password` option accepts an async function (or a function that returns a `Promise`). It is called with the connection parameters, so you can pass them directly to `pgpass` for lookup.

0 commit comments

Comments
 (0)