Database error "Too many connections": causes and fixes
On this page
Symptoms
- A page shows
SQLSTATE[HY000] [1040] Too many connectionsor WordPress shows "Error establishing a database connection" only at busy times. - A PostgreSQL app logs
FATAL: too many connections for role "username_app". - The errors come and go, often during traffic peaks, imports or cron jobs.
Causes
- Many slow requests at the same time. Each PHP request that is still running keeps its database connection open.
- Persistent connections or a large connection pool in an application that keeps connections open while idle.
- Cron jobs or imports that open many connections or run for a long time.
- PostgreSQL: one user has 20 connections open. Each PostgreSQL user may have up to 20 at the same time.
- The whole database server is busy with all accounts on the server (MySQL).
Fix
Make pages lighter
- For WordPress, turn on the page cache on the WordPress page (section Files): cached pages do not open a database connection at all.
- Remove plugins that run slow queries on every page, and check Errors (section Advanced) for repeated database errors.
Use fewer connections in applications
- Turn off persistent connections (PHP: do not use
PDO::ATTR_PERSISTENTormysqlip:hosts). - In Node.js or Python apps, set the connection pool to a small size, for example 5, and close connections you no longer need.
- For PostgreSQL, keep the pool of all processes together under 20 per user, or give a second app its own user.
Spread out cron jobs and imports
Run heavy cron jobs less often or at quiet times, and import large files with the mysql command over SSH instead of many small requests.
Still not working?
If the error appears while your own site is quiet, the database server itself may be overloaded. Contact your hosting provider through Support with the time of the errors and the exact message.
Thank you — this helps us improve the article.
Still stuck?
Ask the AI about your exact situation — it answers from this knowledge base — or contact us.
Related articles
- Error establishing a database connection or access denied
Fix "Error establishing a database connection" and MySQL "Access denied for user" errors: wrong name prefix, password, privileges, host or remote access.
- How to create a PostgreSQL database and user
Create a PostgreSQL database and user in the control panel, add the user to the database, and connect your app with host localhost and port 5432.
- MySQL connection details: host, port, socket and examples
The database host (localhost), port 3306, socket path, name and user format to connect WordPress, PHP, Laravel, Node.js or Python to MySQL, with examples.