title: What is the proper way to use the node.js postgresql module?
body: <p>I am writing a node.js app on Heroku and using the <a href="https://github.com/brianc/node-postgres" rel="noreferrer">pg module</a>. I can't figure out the "right" way to get a client object for each request that I need to query the database.</p>
  
  <p>The documentation uses code like this:</p>
  
  <pre><code>pg.connect(conString, function(err, client) {
    // Use the client to do things here
  });
  </code></pre>
  
  <p>But surely you don't need to call <code>pg.connect</code> inside every function that uses the database right? I've seen <a href="https://github.com/metadaddy-sfdc/node_chat" rel="noreferrer">other code</a> that does this:</p>
  
  <pre><code>var conString = process.env.DATABASE_URL || "tcp://postgres:1234@localhost/postgres";
  var client = new pg.Client(conString);
  client.connect();
  // client is a global so you can use it anywhere now
  </code></pre>
  
  <p>I am leaning toward the second option since I believe the free database instance for Heroku is limited to one connection anyway, but are there any drawbacks to doing it this way? Do I need to check if my client object is still connected every time before I use it?</p>