title: Node JS and pg module 'How can I really close connection?'
body: <p>I'm going crazy with node pg module, getting 'too many clients already' error.</p>
  
  <p>My <code>app.js</code> file for example, manages some routes in which I query some data to postgres. <code>app.js</code> looks like bellow:</p>
  
  <pre><code>//First I create a client
  var client = new pg.Client(connectionString);
  // Then I use that client to every routes, for example:
  ContPg.prototype.someController = function(req, res){
      client.connect(function(error){
          if(error) return console.error('error conectando', error); 
          // Need to close client if there's an error connecting??
  
          client.query(someQuery, function(e,r){
              client.end(); 
              // Here sometimes I dont end client if i need to query more data 
              if(e) return console.error('error consultando', e);
              // Do anything with result...
          })
      });
  }
  </code></pre>
  
  <p>As I said I use that client for all routes in file <code>pg.js</code>, but in other files with other routes I do the same to connect to postgres (create client and use for all routes that manage that file)</p>
  
  <p><strong>Questions</strong></p>
  
  <p>Is something wrong with my code? I ended wrong client connection?
  If there's nothing wrong, what could be causing 'too many clients already' error?</p>
  
  <p>Thanks in advance!! </p>