Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Reorder example section to highlight the connection pooling as this is typically what web application developers should be using.
  • Loading branch information
brianc committed Mar 14, 2014
1 parent 8d44a39 commit e8e16f8
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions README.md
Expand Up @@ -10,55 +10,55 @@ PostgreSQL client for node.js. Pure JavaScript and native libpq bindings.
## Examples

### Simple
### Client pooling

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

```javascript
var pg = require('pg');
//or native libpq bindings
//var pg = require('pg').native

var pg = require('pg');
var conString = "postgres://postgres:1234@localhost/postgres";

var client = new pg.Client(conString);
client.connect(function(err) {
pg.connect(conString, function(err, client, done) {
if(err) {
return console.error('could not connect to postgres', err);
return console.error('error fetching client from pool', err);
}
client.query('SELECT NOW() AS "theTime"', function(err, result) {
client.query('SELECT $1::int AS numbor', ['1'], function(err, result) {
//call `done()` to release the client back to the pool
done();

if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].theTime);
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
client.end();
console.log(result.rows[0].numbor);
//output: 1
});
});

```

### Client pooling
### Simple

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

```javascript
var pg = require('pg');
var pg = require('pg');
//or native libpq bindings
//var pg = require('pg').native

var conString = "postgres://postgres:1234@localhost/postgres";

pg.connect(conString, function(err, client, done) {
var client = new pg.Client(conString);
client.connect(function(err) {
if(err) {
return console.error('error fetching client from pool', err);
return console.error('could not connect to postgres', err);
}
client.query('SELECT $1::int AS numbor', ['1'], function(err, result) {
//call `done()` to release the client back to the pool
done();

client.query('SELECT NOW() AS "theTime"', function(err, result) {
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].numbor);
//output: 1
console.log(result.rows[0].theTime);
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
client.end();
});
});

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

## License

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

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

0 comments on commit e8e16f8

Please sign in to comment.