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
The Pool constructor does not support passing a Database URL as the parameter. To use pg-pool on heroku, for example, you need to parse the URL into a config object. Here is an example of how to parse a Database URL.
52
55
53
56
```js
54
-
constPool=require('pg-pool');
57
+
constPool=require('pg-pool')
55
58
consturl=require('url')
56
59
57
-
constparams=url.parse(process.env.DATABASE_URL);
58
-
constauth=params.auth.split(':');
60
+
constparams=url.parse(process.env.DATABASE_URL)
61
+
constauth=params.auth.split(':')
59
62
60
63
constconfig= {
61
64
user: auth[0],
62
65
password: auth[1],
63
66
host:params.hostname,
64
67
port:params.port,
65
68
database:params.pathname.split('/')[1],
66
-
ssl:true
67
-
};
69
+
ssl:true,
70
+
}
68
71
69
-
constpool=newPool(config);
72
+
constpool=newPool(config)
70
73
71
74
/*
72
75
Transforms, 'postgres://DBuser:secret@DBHost:#####/myDB', into
@@ -79,23 +82,25 @@ const pool = new Pool(config);
79
82
ssl: true
80
83
}
81
84
*/
82
-
```
85
+
```
83
86
84
87
### acquire clients with a promise
85
88
86
89
pg-pool supports a fully promise-based api for acquiring clients
87
90
88
91
```js
89
92
constpool=newPool()
90
-
pool.connect().then(client=> {
91
-
client.query('select $1::text as name', ['pg-pool']).then(res=> {
92
-
client.release()
93
-
console.log('hello from', res.rows[0].name)
94
-
})
95
-
.catch(e=> {
96
-
client.release()
97
-
console.error('query error', e.message, e.stack)
98
-
})
93
+
pool.connect().then((client) => {
94
+
client
95
+
.query('select $1::text as name', ['pg-pool'])
96
+
.then((res) => {
97
+
client.release()
98
+
console.log('hello from', res.rows[0].name)
99
+
})
100
+
.catch((e) => {
101
+
client.release()
102
+
console.error('query error', e.message, e.stack)
103
+
})
99
104
})
100
105
```
101
106
@@ -105,7 +110,7 @@ this ends up looking much nicer if you're using [co](https://github.com/tj/co) o
105
110
106
111
```js
107
112
// with async/await
108
-
(async () => {
113
+
;(async () => {
109
114
constpool=newPool()
110
115
constclient=awaitpool.connect()
111
116
try {
@@ -114,18 +119,18 @@ this ends up looking much nicer if you're using [co](https://github.com/tj/co) o
constresult=yieldclient.query('select $1::text as name', ['brianc'])
124
129
console.log('hello from', result.rows[0])
125
130
} finally {
126
131
client.release()
127
132
}
128
-
}).catch(e=>console.error(e.message, e.stack))
133
+
}).catch((e)=>console.error(e.message, e.stack))
129
134
```
130
135
131
136
### your new favorite helper method
@@ -148,14 +153,14 @@ pool.query('SELECT $1::text as name', ['brianc'], function (err, res) {
148
153
})
149
154
```
150
155
151
-
__pro tip:__ unless you need to run a transaction (which requires a single client for multiple queries) or you
156
+
**pro tip:** unless you need to run a transaction (which requires a single client for multiple queries) or you
152
157
have some other edge case like [streaming rows](https://github.com/brianc/node-pg-query-stream) or using a [cursor](https://github.com/brianc/node-pg-cursor)
153
-
you should almost always just use `pool.query`. Its easy, it does the right thing :tm:, and wont ever forget to return
158
+
you should almost always just use `pool.query`. Its easy, it does the right thing :tm:, and wont ever forget to return
154
159
clients back to the pool after the query is done.
155
160
156
161
### drop-in backwards compatible
157
162
158
-
pg-pool still and will always support the traditional callback api for acquiring a client. This is the exact API node-postgres has shipped with for years:
163
+
pg-pool still and will always support the traditional callback api for acquiring a client. This is the exact API node-postgres has shipped with for years:
When you are finished with the pool if all the clients are idle the pool will close them after `config.idleTimeoutMillis` and your app
178
-
will shutdown gracefully. If you don't want to wait for the timeout you can end the pool as follows:
183
+
will shutdown gracefully. If you don't want to wait for the timeout you can end the pool as follows:
179
184
180
185
```js
181
186
constpool=newPool()
@@ -187,7 +192,7 @@ await pool.end()
187
192
188
193
### a note on instances
189
194
190
-
The pool should be a __long-lived object__ in your application. Generally you'll want to instantiate one pool when your app starts up and use the same instance of the pool throughout the lifetime of your application. If you are frequently creating a new pool within your code you likely don't have your pool initialization code in the correct place. Example:
195
+
The pool should be a **long-lived object** in your application. Generally you'll want to instantiate one pool when your app starts up and use the same instance of the pool throughout the lifetime of your application. If you are frequently creating a new pool within your code you likely don't have your pool initialization code in the correct place. Example:
191
196
192
197
```js
193
198
// assume this is a file in your program at ./your-app/lib/db.js
Every instance of a `Pool` is an event emitter. These instances emit the following events:
223
+
Every instance of a `Pool` is an event emitter. These instances emit the following events:
219
224
220
225
#### error
221
226
222
-
Emitted whenever an idle client in the pool encounters an error. This is common when your PostgreSQL server shuts down, reboots, or a network partition otherwise causes it to become unavailable while your pool has connected clients.
227
+
Emitted whenever an idle client in the pool encounters an error. This is common when your PostgreSQL server shuts down, reboots, or a network partition otherwise causes it to become unavailable while your pool has connected clients.
223
228
224
229
Example:
225
230
@@ -229,15 +234,15 @@ const pool = new Pool()
229
234
230
235
// attach an error handler to the pool for when a connected, idle client
231
236
// receives an error by being disconnected, etc
232
-
pool.on('error', function(error, client) {
237
+
pool.on('error', function(error, client) {
233
238
// handle this in the same way you would treat process.on('uncaughtException')
234
239
// it is supplied the error as well as the idle client which received the error
235
240
})
236
241
```
237
242
238
243
#### connect
239
244
240
-
Fired whenever the pool creates a __new__`pg.Client` instance and successfully connects it to the backend.
245
+
Fired whenever the pool creates a **new**`pg.Client` instance and successfully connects it to the backend.
241
246
242
247
Example:
243
248
@@ -247,20 +252,19 @@ const pool = new Pool()
247
252
248
253
constcount=0
249
254
250
-
pool.on('connect', client=> {
255
+
pool.on('connect', (client)=> {
251
256
client.count= count++
252
257
})
253
258
254
259
pool
255
260
.connect()
256
-
.then(client=> {
261
+
.then((client)=> {
257
262
return client
258
263
.query('SELECT $1::int AS "clientCount"', [client.count])
pg-pool & node-postgres support some of the same environment variables as `psql` supports. The most common are:
304
+
pg-pool & node-postgres support some of the same environment variables as `psql` supports. The most common are:
302
305
303
306
```
304
307
PGDATABASE=my_db
@@ -308,40 +311,19 @@ PGPORT=5432
308
311
PGSSLMODE=require
309
312
```
310
313
311
-
Usually I will export these into my local environment via a `.env` file with environment settings or export them in `~/.bash_profile` or something similar. This way I get configurability which works with both the postgres suite of tools (`psql`, `pg_dump`, `pg_restore`) and node, I can vary the environment variables locally and in production, and it supports the concept of a [12-factor app](http://12factor.net/) out of the box.
312
-
313
-
## bring your own promise
314
-
315
-
In versions of node `<=0.12.x` there is no native promise implementation available globally. You can polyfill the promise globally like this:
316
-
317
-
```js
318
-
// first run `npm install promise-polyfill --save
319
-
if (typeofPromise=='undefined') {
320
-
global.Promise=require('promise-polyfill')
321
-
}
322
-
```
323
-
324
-
You can use any other promise implementation you'd like. The pool also allows you to configure the promise implementation on a per-pool level:
325
-
326
-
```js
327
-
constbluebirdPool=newPool({
328
-
Promise:require('bluebird')
329
-
})
330
-
```
331
-
332
-
__please note:__ in node `<=0.12.x` the pool will throw if you do not provide a promise constructor in one of the two ways mentioned above. In node `>=4.0.0` the pool will use the native promise implementation by default; however, the two methods above still allow you to "bring your own."
314
+
Usually I will export these into my local environment via a `.env` file with environment settings or export them in `~/.bash_profile` or something similar. This way I get configurability which works with both the postgres suite of tools (`psql`, `pg_dump`, `pg_restore`) and node, I can vary the environment variables locally and in production, and it supports the concept of a [12-factor app](http://12factor.net/) out of the box.
333
315
334
316
## maxUses and read-replica autoscaling (e.g. AWS Aurora)
335
317
336
318
The maxUses config option can help an application instance rebalance load against a replica set that has been auto-scaled after the connection pool is already full of healthy connections.
337
319
338
-
The mechanism here is that a connection is considered "expended" after it has been acquired and released `maxUses` number of times. Depending on the load on your system, this means there will be an approximate time in which any given connection will live, thus creating a window for rebalancing.
320
+
The mechanism here is that a connection is considered "expended" after it has been acquired and released `maxUses` number of times. Depending on the load on your system, this means there will be an approximate time in which any given connection will live, thus creating a window for rebalancing.
339
321
340
-
Imagine a scenario where you have 10 app instances providing an API running against a replica cluster of 3 that are accessed via a round-robin DNS entry. Each instance runs a connection pool size of 20. With an ambient load of 50 requests per second, the connection pool will likely fill up in a few minutes with healthy connections.
322
+
Imagine a scenario where you have 10 app instances providing an API running against a replica cluster of 3 that are accessed via a round-robin DNS entry. Each instance runs a connection pool size of 20. With an ambient load of 50 requests per second, the connection pool will likely fill up in a few minutes with healthy connections.
341
323
342
-
If you have weekly bursts of traffic which peak at 1,000 requests per second, you might want to grow your replicas to 10 during this period. Without setting `maxUses`, the new replicas will not be adopted by the app servers without an intervention -- namely, restarting each in turn in order to build up new connection pools that are balanced against all the replicas. Adding additional app server instances will help to some extent because they will adopt all the replicas in an even way, but the initial app servers will continue to focus additional load on the original replicas.
324
+
If you have weekly bursts of traffic which peak at 1,000 requests per second, you might want to grow your replicas to 10 during this period. Without setting `maxUses`, the new replicas will not be adopted by the app servers without an intervention -- namely, restarting each in turn in order to build up new connection pools that are balanced against all the replicas. Adding additional app server instances will help to some extent because they will adopt all the replicas in an even way, but the initial app servers will continue to focus additional load on the original replicas.
343
325
344
-
This is where the `maxUses` configuration option comes into play. Setting `maxUses` to 7500 will ensure that over a period of 30 minutes or so the new replicas will be adopted as the pre-existing connections are closed and replaced with new ones, thus creating a window for eventual balance.
326
+
This is where the `maxUses` configuration option comes into play. Setting `maxUses` to 7500 will ensure that over a period of 30 minutes or so the new replicas will be adopted as the pre-existing connections are closed and replaced with new ones, thus creating a window for eventual balance.
345
327
346
328
You'll want to test based on your own scenarios, but one way to make a first guess at `maxUses` is to identify an acceptable window for rebalancing and then solve for the value:
347
329
@@ -362,7 +344,7 @@ To run tests clone the repo, `npm i` in the working dir, and then run `npm test`
362
344
363
345
## contributions
364
346
365
-
I love contributions. Please make sure they have tests, and submit a PR. If you're not sure if the issue is worth it or will be accepted it never hurts to open an issue to begin the conversation. If you're interested in keeping up with node-postgres releated stuff, you can follow me on twitter at [@briancarlson](https://twitter.com/briancarlson) - I generally announce any noteworthy updates there.
347
+
I love contributions. Please make sure they have tests, and submit a PR. If you're not sure if the issue is worth it or will be accepted it never hurts to open an issue to begin the conversation. If you're interested in keeping up with node-postgres releated stuff, you can follow me on twitter at [@briancarlson](https://twitter.com/briancarlson) - I generally announce any noteworthy updates there.
0 commit comments