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
Generally you will access the PostgreSQL server through a pool of clients. A client takes a non-trivial amount of time to establish a new connection. A client also consumes a non-trivial amount of resources on the PostgreSQL server - not something you want to do on every http request. Good news: node-postgres ships with built in client pooling.
48
+
If you're working on something like a web application which makes frequent queries you'll want to access the PostgreSQL server through a pool of clients. Why? There is ~20-30 millisecond delay (YMMV) when connecting a new client to the PostgreSQL server because of the startup handshake. Also, PostgreSQL can only support a limited number of clients...it depends on the amount of ram on your database server, but generally more than 100 clients at a time is a bad thing. :tm: Finally
49
+
PostgreSQL can only execute 1 query at a time per connected client.
50
+
51
+
With that in mind we can imagine a situtation where you have a web server which connects and disconnects a new client for every web request or every query (don't do this!). If you get only 1 request at a time everything will seem to work fine, though it will be a touch slower due to the connection overhead. Once you get >500 simultaneous requests your web server will attempt to open 500 connections to the PostgreSQL backend and :boom: you'll run out of memory on the PostgreSQL server, it will become
52
+
unresponsive, your app will seem to hang, and everything will break. Boooo!
53
+
54
+
__Good news__: node-postgres ships with built in client pooling.
20
55
21
56
```javascript
22
-
varPool=require('pg').Pool;
57
+
varpg=require('pg');
23
58
59
+
// create a config to configure both pooling behavior
60
+
// and client options
61
+
// note: all config is optional and the environment variables
62
+
// will be read if the config is not present
24
63
var config = {
25
-
user:'foo',
26
-
password:'secret',
27
-
database:'my_db',
28
-
port:5432
64
+
user:'foo', //env var: PGUSER
65
+
database:'my_db', //env var: PGDATABASE
66
+
password:'secret', //env var: PGPASSWORD
67
+
port:5432, //env var: PGPORT
68
+
max:10, // max number of clients in the pool
69
+
idleTimeoutMillis:30000, // how long a client is allowed to remain idle before being closed
29
70
};
30
71
31
-
var pool =newPool(config);
72
+
var pool =newpg.Pool(config);
32
73
33
74
//this initializes a connection pool
34
-
//it will keep idle connections open for a (configurable) 30 seconds
35
-
//and set a limit of 10 (also configurable)
75
+
//it will keep idle connections open for a 30 seconds
76
+
//and set a limit of maximum 10 idle clients
36
77
pool.connect(function(err, client, done) {
37
78
if(err) {
38
79
returnconsole.error('error fetching client from pool', err);
node-postgres uses [pg-pool](https://github.com/brianc/node-pg-pool.git) to manage pooling and only provides a very thin layer on top.
94
+
node-postgres uses [pg-pool](https://github.com/brianc/node-pg-pool.git) to manage pooling and includes it and exports it for convienience. If you want, you can `require('pg-pool')` and use it directly - its the same as the constructor exported at `pg.Pool`.
54
95
55
-
It's _highly recommend_ you read the documentation for [pg-pool](https://github.com/brianc/node-pg-pool.git)
96
+
It's __highly recommend__ you read the documentation for [pg-pool](https://github.com/brianc/node-pg-pool.git).
56
97
57
98
58
-
[Here is a tl;dr get up & running quickly example](https://github.com/brianc/node-postgres/wiki/Example)
99
+
[Here is an up & running quickly example](https://github.com/brianc/node-postgres/wiki/Example)
59
100
60
-
### Client instance
61
-
62
-
Sometimes you may not want to use a pool of connections. You can easily connect a single client to a postgres instance, run some queries, and disconnect.
63
-
64
-
```javascript
65
-
var pg =require('pg');
66
-
67
-
var conString ="postgres://username:password@localhost/database";
68
-
69
-
var client =newpg.Client(conString);
70
-
client.connect(function(err) {
71
-
if(err) {
72
-
returnconsole.error('could not connect to postgres', err);
73
-
}
74
-
client.query('SELECT NOW() AS "theTime"', function(err, result) {
node-postgres contains a pure JavaScript protocol implementation which is quite fast, but you can optionally use native bindings for a 20-30% increase in parsing speed. Both versions are adequate for production workloads.
113
+
node-postgres contains a pure JavaScript protocol implementation which is quite fast, but you can optionally use [native](https://github.com/brianc/node-pg-native)[bindings](https://github.com/brianc/node-libpq) for a 20-30% increase in parsing speed (YMMV). Both versions are adequate for production workloads. I personally use the pure JavaScript implementation because I like knowing whats going on all the way down to the binary on the socket, and it allows for some fancier [use](https://github.com/brianc/node-pg-cursor)[cases](https://github.com/brianc/node-pg-query-stream) which are difficult to do with libpq. :smile:
114
+
115
+
To use the native bindings, first install [pg-native](https://github.com/brianc/node-pg-native.git). Once pg-native is installed, simply replace `var pg = require('pg')` with `var pg = require('pg').native`. Make sure any exported constructors from `pg` are from the native instance. Example:
98
116
99
-
To use the native bindings, first install [pg-native](https://github.com/brianc/node-pg-native.git). Once pg-native is installed, simply replace `require('pg')` with `require('pg').native`.
117
+
```js
118
+
var pg =require('pg').native
119
+
var Pool =require('pg').Pool// bad! this is not bound to the native client
120
+
var Client =require('pg').Client// bad! this is the pure JavaScript client
121
+
122
+
var pg =require('pg').native
123
+
var Pool =pg.Pool// good! a pool bound to the native client
124
+
var Client =pg.Client// good! this client uses libpq bindings
125
+
```
100
126
101
127
node-postgres abstracts over the pg-native module to provide exactly the same interface as the pure JavaScript version. Care has been taken to keep the number of api differences between the two modules to a minimum; however, it is recommend you use either the pure JavaScript or native bindings in both development and production and don't mix & match them in the same process - it can get confusing!
102
128
103
129
## Features
104
130
105
131
* pure JavaScript client and native libpq bindings share _the same api_
106
-
*optional connection pooling
132
+
* connection pooling
107
133
* extensible js<->postgresql data-type coercion
108
134
* supported PostgreSQL features
109
135
* parameterized queries
110
136
* named statements with query plan caching
111
137
* async notifications with `LISTEN/NOTIFY`
112
138
* bulk import & export with `COPY TO/COPY FROM`
113
139
140
+
## Extras
141
+
142
+
node-postgres is by design pretty light on abstractions. These are some handy modules we've been using over the years to complete the picture.
143
+
Entire list can be found on [wiki](https://github.com/brianc/node-postgres/wiki/Extras)
144
+
114
145
## Contributing
115
146
116
-
__We love contributions!__
147
+
__:heart: contributions!__
117
148
118
149
If you need help getting the tests running locally or have any questions about the code when working on a patch please feel free to email me or gchat me.
119
150
@@ -135,17 +166,14 @@ If at all possible when you open an issue please provide
135
166
136
167
Usually I'll pop the code into the repo as a test. Hopefully the test fails. Then I make the test pass. Then everyone's happy!
137
168
138
-
If you need help or run into _any_ issues getting node-postgres to work on your system please report a bug or contact me directly. I am usually available via google-talk at my github account public email address.
169
+
If you need help or run into _any_ issues getting node-postgres to work on your system please report a bug or contact me directly. I am usually available via google-talk at my github account public email address. Remember this is a labor of love, and though I try to get back to everything sometimes life takes priority, and I might take a while. It helps if you use nice code formatting in your issue, search for existing answers before posting, and come back and close out the issue if you figure out a solution. The easier you can make it for me, the quicker I'll try and respond to you!
170
+
171
+
If you need deeper support, have application specific questions, would like to sponsor development, or want consulting around node & postgres please send me an email, I'm always happy to discuss!
139
172
140
173
I usually tweet about any important status updates or changes to node-postgres on twitter.
141
174
Follow me [@briancarlson](https://twitter.com/briancarlson) to keep up to date.
142
175
143
176
144
-
## Extras
145
-
146
-
node-postgres is by design pretty light on abstractions. These are some handy modules we've been using over the years to complete the picture.
147
-
Entire list can be found on [wiki](https://github.com/brianc/node-postgres/wiki/Extras)
148
-
149
177
## License
150
178
151
179
Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
0 commit comments