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
Copy file name to clipboardExpand all lines: README.md
+56-53Lines changed: 56 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -7,10 +7,12 @@ PostgreSQL client for node.js. Pure JavaScript and native libpq bindings.
7
7
## Installation
8
8
9
9
npm install pg
10
-
10
+
11
11
## Examples
12
12
13
-
### Callbacks
13
+
### Simple
14
+
15
+
Connect to a postgres instance, run a query, and disconnect.
14
16
15
17
```javascript
16
18
var pg =require('pg');
@@ -19,88 +21,77 @@ var pg = require('pg');
19
21
20
22
var conString ="tcp://postgres:1234@localhost/postgres";
21
23
22
-
//note: error handling omitted
23
24
var client =newpg.Client(conString);
24
25
client.connect(function(err) {
26
+
if(err) {
27
+
console.error('could not connect to postgres', err);
28
+
return;
29
+
}
25
30
client.query('SELECT NOW() AS "theTime"', function(err, result) {
31
+
if(err) {
32
+
console.error('error running query', err);
33
+
return;
34
+
}
26
35
console.log(result.rows[0].theTime);
27
36
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
37
+
client.end();
28
38
})
29
39
});
30
40
31
41
```
32
42
33
-
### Events
43
+
### Client pooling
44
+
45
+
Typically you will access the PostgreSQL server through a pool of clients. node-postgres ships with a built in pool to help get you up and running quickly.
34
46
35
47
```javascript
36
-
var pg =require('pg');//native libpq bindings = `var pg = require('pg').native`
48
+
var pg =require('pg');
37
49
var conString ="tcp://postgres:1234@localhost/postgres";
38
50
39
-
var client =newpg.Client(conString);
40
-
client.connect();
41
-
42
-
//queries are queued and executed one after another once the connection becomes available
console.error('error fetching client from pool', err);
54
+
return;
55
+
}
56
+
client.query('SELECT $1::int AS numbor', ['1'], function(err, result) {
57
+
//call `done()` to release the client back to the pool
58
+
done();
59
+
60
+
if(err) {
61
+
console.error('error running query', err);
62
+
return;
63
+
}
64
+
console.log(result.rows[0].numbor);
65
+
//output: 1
66
+
});
58
67
});
59
68
```
60
69
61
-
### Example notes
70
+
##Documentation
62
71
63
-
node-postgres supports both an 'event emitter' style API and a 'callback' style. The callback style is more concise and generally preferred, but the evented API can come in handy when you want to handle row events as they come in.
72
+
Documentation is a work in progress primarily taking place on the github WIKI
They can be mixed and matched. The only events which do __not__ fire when callbacks are supplied are the `error` events, as they are to be handled within the callback function.
76
+
## Native Bindings
66
77
67
-
All examples will work with the pure javascript bindings or the libpq native (c/c++) bindings
78
+
node-postgres contains a pure JavaScript driver and also exposes JavaScript bindings to libpq. You can use either interface. I personally use the JavaScript bindings as the are quite fast, and I like having everything implemented in JavaScript.
68
79
69
80
To use native libpq bindings replace `require('pg')` with `require('pg').native`.
70
81
71
82
The two share the same interface so __no other code changes should be required__. If you find yourself having to change code other than the require statement when switching from `pg` to `pg.native`, please report an issue.
72
83
73
-
### Features
74
84
75
-
* pure javascript client and native libpq bindings share _the same api_
76
-
* row-by-row result streaming
77
-
* responsive project maintainer
85
+
## Features
86
+
87
+
* pure JavaScript client and native libpq bindings share _the same api_
88
+
* optional connection pooling
89
+
* extensible js<->postgresql data-type coercion
78
90
* supported PostgreSQL features
79
91
* parameterized queries
80
92
* named statements with query plan caching
81
93
* async notifications with `LISTEN/NOTIFY`
82
94
* bulk import & export with `COPY TO/COPY FROM`
83
-
* extensible js<->postgresql data-type coercion
84
-
85
-
## Documentation
86
-
87
-
Documentation is a work in progress primarily taking place on the github WIKI
0 commit comments