<p>We have PostgreSQL 9.6 instance at a ubuntu 18.04 machine. When we restart java services deployed in a Kubernetes cluster then already existing idle connection didn't get remove and service create new connections on each restart. Due to this, we have reached a connection limit so many times and we have to terminate connection manually every time.  Same service versions are deployed on other instances but we are not getting this scenario on other servers.</p>

<p>I have some questions regarding this</p>

<ol>
<li><p>Can it be a PostgreSQL configuration issue?  However, i didn't find any timeout-related setting difference in 2 instances (1 is working fine and another isnt)</p></li>
<li><p>If this is a java service issue then what should I check?</p></li>
<li><p>If its neither a PostgreSQL issue not a java issue then what should i look into?</p></li>
</ol>

<p>If the client process dies without closing the database connection properly, it takes a while (2 hours by default) for the server to notice that the connection is dead.</p>

<p>The mechanism for that is provided by TCP and is called <em>keepalive</em>: after a certain idle time, the operating system starts sending <em>keepalive packets</em>. After a certain number of such packets without response, the TCP connection is closed, and the database backend process will die.</p>

<p>To make PostgreSQL detect dead connections faster, set the <code>tcp_keepalives_idle</code> parameter in <code>postgresql.conf</code> to less than 7200 seconds.</p>
