diff --git a/README.md b/README.md index 5bb16b4e2..f4592d6be 100644 --- a/README.md +++ b/README.md @@ -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(); }); }); @@ -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