Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Do I need to use pg.connect() every time I query the database? After looking over the githhub page and wiki, the examples show a query inside the pg.connect callback like this (the comments are from the github example, i did not write them)

//this initializes a connection pool
//it will keep idle connections open for a (configurable) 30 seconds
//and set a limit of 20 (also configurable)
pg.connect(conString, function(err, client, done) {
  if(err) {
    return console.error('error fetching client from pool', err);
  }
  client.query('SELECT $1::int AS number', ['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].number);
    //output: 1
  });
});

The comments are confusing because it sounds like pg.connect() is creating a new client pool with each call which would obviously be inefficient. I have seen other examples in the documentation that create a client, but I want to use the client pool.

share|improve this question
    
If you want to avoid the connection complexity, use pg-promise – vitaly-t Dec 12 '15 at 22:50
up vote 2 down vote accepted

Yea pg.connect is the recommended way of doing things. as stated in the github page: https://github.com/brianc/node-postgres. Its not creating a pool for each request, rather a new request will create a 'pool' and all subsequent queries are added to that connection, until the time-out, 30 seconds. //it will keep idle connections open for a (configurable) 30 seconds So when the app isn't being used there is no connection, but once you are getting a few queries every second, they are all queued on that connection. the time out and amount queued can be changed.

share|improve this answer
    
Could you clarify, "Its not creating a pool for each request, rather a new request will create a 'pool' and all subsequent queries are added to that connection"? Say I have a two http routes with db calls in them. One user goes to route A, pg.connect() creates a client pool and executes the request. Now another user goes to route B (within 30 seconds). Does the pg.connect() in route B use an existing client from the pool created in route A or does it create its own pool? I imagine pg.connect() reuses connections from the existing pool and creates a pool only when needed, but I have to ask. – user137717 Dec 12 '15 at 16:25
1  
yea, route B uses an existing connection, pg-node doesn't care how you are routing or which table, as long as you are hitting the same database. – Saad Dec 12 '15 at 19:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.