You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
importpgfrom'pg'
15
+
importpgpassfrom'pgpass'
16
+
17
+
const { Pool } = pg
18
+
19
+
constpool=newPool({
20
+
user:'my-user',
21
+
host:'localhost',
22
+
database:'my-db',
23
+
password: (config) => {
24
+
returnnewPromise((resolve, reject) => {
25
+
constconnection= {
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