Skip to content

Commit cb85dad

Browse files
authored
Remove built-in pgpass support. (#3636)
* Remove built-in pgpass support. * Add documentation on using pgpass
1 parent 412dc88 commit cb85dad

File tree

4 files changed

+63
-42
lines changed

4 files changed

+63
-42
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.

packages/pg/lib/client.js

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ const queryQueueDeprecationNotice = nodeUtils.deprecate(
2020
'Client.queryQueue is deprecated and will be removed in pg@9.0.'
2121
)
2222

23-
const pgPassDeprecationNotice = nodeUtils.deprecate(
24-
() => {},
25-
'pgpass support is deprecated and will be removed in pg@9.0. ' +
26-
'You can provide an async function as the password property to the Client/Pool constructor that returns a password instead. Within this function you can call the pgpass module in your own code.'
27-
)
28-
2923
const byoPromiseDeprecationNotice = nodeUtils.deprecate(
3024
() => {},
3125
'Passing a custom Promise implementation to the Client/Pool constructor is deprecated and will be removed in pg@9.0.'
@@ -248,42 +242,29 @@ class Client extends EventEmitter {
248242
}
249243

250244
_getPassword(cb) {
251-
const con = this.connection
252-
if (typeof this.password === 'function') {
253-
this._Promise
254-
.resolve()
255-
.then(() => this.password(this.connectionParameters))
256-
.then((pass) => {
257-
if (pass !== undefined) {
258-
if (typeof pass !== 'string') {
259-
con.emit('error', new TypeError('Password must be a string'))
260-
return
261-
}
262-
this.connectionParameters.password = this.password = pass
263-
} else {
264-
this.connectionParameters.password = this.password = null
265-
}
266-
cb()
267-
})
268-
.catch((err) => {
269-
con.emit('error', err)
270-
})
271-
} else if (this.password !== null) {
245+
if (typeof this.password !== 'function') {
272246
cb()
273-
} else {
274-
try {
275-
const pgPass = require('pgpass')
276-
pgPass(this.connectionParameters, (pass) => {
277-
if (undefined !== pass) {
278-
pgPassDeprecationNotice()
279-
this.connectionParameters.password = this.password = pass
280-
}
281-
cb()
282-
})
283-
} catch (e) {
284-
this.emit('error', e)
285-
}
247+
return
286248
}
249+
const con = this.connection
250+
this._Promise
251+
.resolve()
252+
.then(() => this.password(this.connectionParameters))
253+
.then((pass) => {
254+
if (pass !== undefined) {
255+
if (typeof pass !== 'string') {
256+
con.emit('error', new TypeError('Password must be a string'))
257+
return
258+
}
259+
this.connectionParameters.password = this.password = pass
260+
} else {
261+
this.connectionParameters.password = this.password = null
262+
}
263+
cb()
264+
})
265+
.catch((err) => {
266+
con.emit('error', err)
267+
})
287268
}
288269

289270
_handleAuthCleartextPassword(msg) {

packages/pg/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535
"pg-connection-string": "^2.12.0",
3636
"pg-pool": "^3.13.0",
3737
"pg-protocol": "^1.13.0",
38-
"pg-types": "2.2.0",
39-
"pgpass": "1.0.5"
38+
"pg-types": "2.2.0"
4039
},
4140
"devDependencies": {
4241
"@cloudflare/vitest-pool-workers": "0.8.23",
4342
"@cloudflare/workers-types": "^4.20230404.0",
4443
"async": "2.6.4",
4544
"bluebird": "3.7.2",
4645
"co": "4.6.0",
46+
"pgpass": "1.0.5",
4747
"pg-copy-streams": "0.3.0",
4848
"typescript": "^4.0.3",
4949
"vitest": "~3.0.9",

0 commit comments

Comments
 (0)