Skip to content

Commit e8e16f8

Browse files
committed
Update README.md
Reorder example section to highlight the connection pooling as this is typically what web application developers should be using.
1 parent 8d44a39 commit e8e16f8

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

Diff for: README.md

+25-25
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,55 @@ PostgreSQL client for node.js. Pure JavaScript and native libpq bindings.
1010
1111
## Examples
1212

13-
### Simple
13+
### Client pooling
1414

15-
Connect to a postgres instance, run a query, and disconnect.
15+
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.
1616

1717
```javascript
18-
var pg = require('pg');
19-
//or native libpq bindings
20-
//var pg = require('pg').native
21-
18+
var pg = require('pg');
2219
var conString = "postgres://postgres:1234@localhost/postgres";
2320

24-
var client = new pg.Client(conString);
25-
client.connect(function(err) {
21+
pg.connect(conString, function(err, client, done) {
2622
if(err) {
27-
return console.error('could not connect to postgres', err);
23+
return console.error('error fetching client from pool', err);
2824
}
29-
client.query('SELECT NOW() AS "theTime"', function(err, result) {
25+
client.query('SELECT $1::int AS numbor', ['1'], function(err, result) {
26+
//call `done()` to release the client back to the pool
27+
done();
28+
3029
if(err) {
3130
return console.error('error running query', err);
3231
}
33-
console.log(result.rows[0].theTime);
34-
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
35-
client.end();
32+
console.log(result.rows[0].numbor);
33+
//output: 1
3634
});
3735
});
3836

3937
```
4038

41-
### Client pooling
39+
### Simple
4240

43-
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.
41+
Sometimes you may not want to use a pool of connections. You can easily connect a single client to a postgres instance, run a query, and disconnect.
4442

4543
```javascript
46-
var pg = require('pg');
44+
var pg = require('pg');
45+
//or native libpq bindings
46+
//var pg = require('pg').native
47+
4748
var conString = "postgres://postgres:1234@localhost/postgres";
4849

49-
pg.connect(conString, function(err, client, done) {
50+
var client = new pg.Client(conString);
51+
client.connect(function(err) {
5052
if(err) {
51-
return console.error('error fetching client from pool', err);
53+
return console.error('could not connect to postgres', err);
5254
}
53-
client.query('SELECT $1::int AS numbor', ['1'], function(err, result) {
54-
//call `done()` to release the client back to the pool
55-
done();
56-
55+
client.query('SELECT NOW() AS "theTime"', function(err, result) {
5756
if(err) {
5857
return console.error('error running query', err);
5958
}
60-
console.log(result.rows[0].numbor);
61-
//output: 1
59+
console.log(result.rows[0].theTime);
60+
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
61+
client.end();
6262
});
6363
});
6464

@@ -149,7 +149,7 @@ _If you use node-postgres in production and would like your site listed here, fo
149149

150150
## License
151151

152-
Copyright (c) 2010 Brian Carlson ([email protected])
152+
Copyright (c) 2010-2014 Brian Carlson ([email protected])
153153

154154
Permission is hereby granted, free of charge, to any person obtaining a copy
155155
of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)