title: Where to define the maximum number of connections in Postgres?
body: <p>I want to limit the number of users per database in a multi tenant environment. But there are three levels of max connections and I should be grateful for any advice.</p>
  
  <p><strong>Level 1 Entire Server</strong></p>
  
  <p>By editing the config for Postgresql I can set the Max connection for the all databases on a server</p>
  
  <pre><code>postgresql.conf = max_connections = 100
  </code></pre>
  
  <p><strong>Level 2 Per database</strong></p>
  
  <p>I can select and set the database connection limit per database:</p>
  
  <pre><code>SELECT datconnlimit FROM pg_database
  </code></pre>
  
  <p><strong>Level 3 Per role</strong></p>
  
  <p>I can select and set the role connection limit per "user":</p>
  
  <pre><code>SELECT rolconnlimit FROM pg_roles
  </code></pre>
  
  <p>My questions are </p>
  
  <ol>
  <li><p>If the max_connections in postgresql.conf is 100, will it be max connections of all databases regardless database and role settings? e.g. 100 databases can only have 1 connection each simultaneously?</p></li>
  <li><p>Where is the best place to limit max connections. At the database level or at the role level?</p></li>
  <li><p>Anything other to be considered?</p></li>
  </ol>
  
  <p>TIA for any advice or clue!</p>