Skip to content

Commit b9f103d

Browse files
committed
Updated README.md
1 parent 58b4f26 commit b9f103d

File tree

1 file changed

+56
-53
lines changed

1 file changed

+56
-53
lines changed

README.md

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ PostgreSQL client for node.js. Pure JavaScript and native libpq bindings.
77
## Installation
88

99
npm install pg
10-
10+
1111
## Examples
1212

13-
### Callbacks
13+
### Simple
14+
15+
Connect to a postgres instance, run a query, and disconnect.
1416

1517
```javascript
1618
var pg = require('pg');
@@ -19,88 +21,77 @@ var pg = require('pg');
1921

2022
var conString = "tcp://postgres:1234@localhost/postgres";
2123

22-
//note: error handling omitted
2324
var client = new pg.Client(conString);
2425
client.connect(function(err) {
26+
if(err) {
27+
console.error('could not connect to postgres', err);
28+
return;
29+
}
2530
client.query('SELECT NOW() AS "theTime"', function(err, result) {
31+
if(err) {
32+
console.error('error running query', err);
33+
return;
34+
}
2635
console.log(result.rows[0].theTime);
2736
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
37+
client.end();
2838
})
2939
});
3040

3141
```
3242

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.
3446

3547
```javascript
36-
var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native`
48+
var pg = require('pg');
3749
var conString = "tcp://postgres:1234@localhost/postgres";
3850

39-
var client = new pg.Client(conString);
40-
client.connect();
41-
42-
//queries are queued and executed one after another once the connection becomes available
43-
client.query("CREATE TEMP TABLE beatles(name varchar(10), height integer, birthday timestamptz)");
44-
client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['John', 68, new Date(1944, 10, 13)]);
45-
var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']);
46-
47-
//can stream row results back 1 at a time
48-
query.on('row', function(row) {
49-
console.log(row);
50-
console.log("Beatle name: %s", row.name); //Beatle name: John
51-
console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates
52-
console.log("Beatle height: %d' %d\"", Math.floor(row.height/12), row.height%12); //integers are returned as javascript ints
53-
});
54-
55-
//fired after last row is emitted
56-
query.on('end', function() {
57-
client.end();
51+
pg.connect(conString, function(err, client, done) {
52+
if(err) {
53+
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+
});
5867
});
5968
```
6069

61-
### Example notes
70+
## Documentation
6271

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
73+
74+
### [Documentation](https://github.com/brianc/node-postgres/wiki)
6475

65-
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
6677

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.
6879

6980
To use native libpq bindings replace `require('pg')` with `require('pg').native`.
7081

7182
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.
7283

73-
### Features
7484

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
7890
* supported PostgreSQL features
7991
* parameterized queries
8092
* named statements with query plan caching
8193
* async notifications with `LISTEN/NOTIFY`
8294
* 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
88-
89-
### [Documentation](https://github.com/brianc/node-postgres/wiki)
90-
91-
### __PLEASE__ check out the WIKI
92-
93-
If you have a question, post it to the FAQ section of the WIKI so everyone can read the answer
94-
95-
## Production Use
96-
* [yammer.com](http://www.yammer.com)
97-
* [bayt.com](http://bayt.com)
98-
* [bitfloor.com](https://bitfloor.com)
99-
* [Vendly](http://www.vend.ly)
100-
* [SaferAging](http://www.saferaging.com)
101-
* [CartoDB](http://www.cartodb.com)
102-
103-
_if you use node-postgres in production and would like your site listed here, fork & add it_
10495

10596
## Contributing
10697

@@ -140,6 +131,18 @@ node-postgres is by design _low level_ with the bare minimum of abstraction. Th
140131
- https://github.com/grncdr/node-any-db
141132
- https://github.com/brianc/node-sql
142133

134+
135+
## Production Use
136+
* [yammer.com](http://www.yammer.com)
137+
* [bayt.com](http://bayt.com)
138+
* [bitfloor.com](https://bitfloor.com)
139+
* [Vendly](http://www.vend.ly)
140+
* [SaferAging](http://www.saferaging.com)
141+
* [CartoDB](http://www.cartodb.com)
142+
143+
_if you use node-postgres in production and would like your site listed here, fork & add it_
144+
145+
143146
## License
144147

145148
Copyright (c) 2010 Brian Carlson (brian.m.carlson@gmail.com)

0 commit comments

Comments
 (0)