title: Django - postgreSQL - new connection is made for every page
body: <p><strong>settings.py</strong></p>
  
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql',
          'NAME': 'tutorial',
          'USER': 'postgres',
          'PASSWORD': '',
          'HOST': 'localhost',
          'PORT': '5432',
          'CONN_MAX_AGE': 10,
      }
  }
  </code></pre>
  
  <p>As I navigate to links in my website, a new connection is created. For example, upon entering &lt;<code>domain.com</code>>, a new connection is created:</p>
  
  <pre><code>2018-08-10 06:22:15.301 CDT [3380] LOG:  connection received: host=::1 port=56368
  2018-08-10 06:22:15.306 CDT [3380] LOG:  connection authorized: user=postgres database=tutorial
  </code></pre>
  
  <p>And upon navigating to one of the links, like &lt;<code>domain.com/some_page/</code>>, another new connection is created. </p>
  
  <pre><code>2018-08-10 06:20:10.095 CDT [22932] LOG:  connection received: host=::1 port=56181
  2018-08-10 06:20:10.098 CDT [22932] LOG:  connection authorized: user=postgres database=tutorial
  </code></pre>
  
  <p>When I have visited 10 different link within my domain and visit my 11th link, it finally reaches the maximum available connections, and disconnects the very first connection I established by visiting &lt;<code>domain.com</code>>:</p>
  
  <pre><code>2018-08-10 06:30:17.715 CDT [6884] LOG:  disconnection: session time: 0:00:09.011 user=postgres database=tutorial host=::1 port=56368
  </code></pre>
  
  <p>Please note that <code>port=56368</code> was the exact same port I opened upon visiting my &lt;<code>domain.com</code>> the first time. Apparently a new connection is established every time I visit a new link, and if the current number of connections exceeds the max connections, the very first connection is disconnected in order to accept a new connection. </p>
  
  <p>But this is not I want. I don't want a new connection to be created every time a user visits a link inside my domain. I want to use only one connection throughout my entire domain per user. If a user opens a new tab, then I might want to establish a new connection, but not sure if this is the right way to do it.</p>
  
  <p>How can I achieve this? Ideally I would want to use connection pooling, but I don't really know how to implement it inside Django. I know how to establish connection pooling system using a pure <code>psycopg2</code>, but not sure if I want to do it that way, since Django might support such functionality. Or should I just write separate <code>psycopg2</code> codes for connection pooling?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I'm aware that <code>CONN_MAX_AGE</code> specifies number of seconds that a request can be alive. But, the connection I used to load a page does not close after 10 seconds it was loaded. But instead, its behaving as if <code>CONN_MAX_AGE</code> is setting the maximum number of connections, and I do not know why. The log file outputs <code>disconnection:</code> message only if the number of connections exceeds <code>CONN_MAX_AGE</code>. IT DOES NOT outputs <code>disconnection</code> message after 10 seconds a page is loaded. </p>
  
  <p>I'm using <code>Django 2.0.6</code>, <code>psycopg2 2.7.5</code>, <code>postgreSQL 10</code></p>