title: How to close inactive connections in Postgres
body: <p>Postgres 9.1 sever is running under Debian. 
  It is accessed by Mono MVC4 web applications using ngpsql, Apache and mod_mono.
  It is also accessed by pg_dump in same computer every night and from 5432 port from internet.
  Connections were cleared at June 20.
  pgAdmin statuc window shows lot of idle connections starting from this day.
  (see image below).</p>
  
  <p>After some time number of connections goes to 100 and server stops to accept connections. Connections needs to be manually killed in pgAdmin. Only after that server start accepting connections again.</p>
  
  <p>It is interesting that start times of some of those connections are exactly the same as shown in image.
  How to fix or diagnose the issue ?
  I tried <code>/etc/init.d/apache2 restart</code> . This closed 25 and 27 June connections but 20 June connections are still open.</p>
  
  <p>How to close idle connections automatically or find the reason of open connections ? Maybe there is some postgres or Debian setting which closes idle connections automatically ?</p>
  
  <p><img src="https://i.stack.imgur.com/MPfym.png" alt="open">
  Using</p>
  
  <p>"PostgreSQL 9.1.2 on x86_64-unknown-linux-gnu, compiled by gcc-4.4.real (Debian 4.4.5-8) 4.4.5, 64-bit"</p>
  
  <p><strong>Update</strong></p>
  
  <p>Application uses the following connection string:</p>
  
  <pre><code>    NpgsqlConnectionStringBuilder csb = new NpgsqlConnectionStringBuilder()
      {
          SearchPath = schema == null ? "public" : schema + ",public",
          Timeout = 115,
          CommandTimeout = 115,
          Host = Config.Server,
          Database = Config.DefaultDataBase,
          UserName = Config.ServerUser,
          Port = Config.Port,
          // Apache allows 150 connections, MaxPoolSize is set accordingly:
          MaxPoolSize = 155
      };
  </code></pre>