PostgreSQL – How to Connect When ‘Sorry, Too Many Clients Already’ Error Occurs

postgresql

I want to see what application is the cause, but even I change to user postgres and run psql, it still throw error

Best Answer

If you want to connect to database any how you need to free some connections on the server

  1. Check all the idle postgres connection ps auxwww|grep 'idle in transaction' which will return list of all idle transaction processes with pid .

  2. kill "pid" e.g say 10544 process having idle connection to database so kill 10544 will free up a single connection.

Now you can get access to database and check what/whom/when/where is holding your connection using query for monitoring SELECT * FROM pg_stat_activity;

It's better to increase max_connections of your postgresql server if you have more connections and users.

Just follow below steps

  1. First find your postgresql.conf file If you don't know where it is, query the database with the sql: SHOW config_file;
    Mine is in: /var/lib/pgsql/data/postgresql.conf

  2. Login as root and edit that file. Search for the string: max_connections. You'll see a line that says max_connections=100

  3. Set that number bigger, check the limit for your postgresql version.

  4. Restart the postgresql database for the changes to take effect service postgresql restart.