Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 53400: Fix Configuration Limit Exceeded

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 53400 occurs when the database server reaches a predefined resource limit set in its configuration, most commonly the maximum number of concurrent connections. To fix this, you must increase the specific limit in the postgresql.conf file and restart the database service to apply the changes.

What You'll Need

  • Administrative or root access to the server hosting the PostgreSQL instance.
  • A text editor (such as nano, vim, or Notepad++) capable of editing configuration files.
  • Access to the command line or terminal (SSH for remote servers).
  • Knowledge of the current system memory (RAM) available, as increasing limits consumes more resources.

Step-by-Step

  1. Identify the specific limit being exceeded. While error 53400 is a general "configuration limit exceeded" message, it almost always refers to max_connections. Check your PostgreSQL logs to confirm. Logs are typically found in the pg_log directory or managed by the system logger (like journalctl on Linux). Look for a line stating "too many connections for host" or "remaining connection slots are reserved for non-replication superuser connections."
  2. Locate the postgresql.conf file. The location of this file varies by operating system and version. On Linux, it is often in /etc/postgresql/[version]/main/ or within the data directory. If you are unsure of the path, run the SQL command SHOW config_file; from a psql prompt.
  3. Backup the configuration file. Before making any changes, create a copy of the current configuration to allow for a quick rollback if the server fails to start. Use a command like cp postgresql.conf postgresql.conf.bak.
  4. Edit the max_connections setting. Open the file with your text editor. Search for the line max_connections = 100 (the default is often 100). Increase this number to a value that accommodates your application's needs, such as max_connections = 200 or 500. Ensure you do not set this number higher than your server's RAM can handle, as each connection requires a chunk of shared memory.
  5. Adjust supporting memory settings. Increasing connections often requires adjusting shared_buffers. If you significantly increase the connection limit, you may need to increase the amount of memory PostgreSQL allocates for caching data to prevent performance degradation. Check your work_mem setting as well; since work_mem is allocated per operation per connection, a very high connection count combined with a high work_mem can lead to the operating system killing the PostgreSQL process due to out-of-memory (OOM) errors.
  6. Save the file and exit the editor. Ensure the changes are written to the disk.
  7. Restart the PostgreSQL service. Changes to max_connections cannot be applied with a simple reload; they require a full restart of the database engine. On Linux, use sudo systemctl restart postgresql or sudo service postgresql restart. On Windows, use the Services manager to restart the PostgreSQL service.
  8. Verify the change. Log back into the database and run the SQL command SHOW max_connections; to confirm that the new limit is active.

Common Mistakes

One of the most frequent mistakes is increasing the max_connections value to an arbitrarily high number (e.g., 5,000) without considering the underlying hardware. Every connection to PostgreSQL forks a new process on the server. If you have too many processes, the CPU will spend more time switching between tasks (context switching) than actually executing queries, leading to a massive drop in performance known as "connection thrashing."

Another common error is forgetting to restart the service. Many administrators attempt to use SELECT pg_reload_conf(); or a service reload command. While this works for many parameters, max_connections is a postmaster-level setting that allocates shared memory at startup. A reload will not change this value, and the error 53400 will persist.

Some users also overlook the superuser_reserved_connections setting. By default, PostgreSQL reserves a few slots (usually 3) for superusers. If your application is using a non-superuser account and you see this error, it might be because the total connections have reached max_connections minus superuser_reserved_connections. Simply increasing the limit is the fix, but understanding this distinction helps in diagnosing why a superuser can still log in while the app cannot.

Tips

Instead of indefinitely increasing the connection limit, the best practice for high-traffic applications is to implement a connection pooler. Tools like PgBouncer act as a proxy between your application and the database. PgBouncer maintains a small pool of actual connections to the database and rotates them among hundreds or thousands of incoming application requests. This prevents the database from being overwhelmed by process overhead and eliminates the need to set max_connections to extreme values.

Monitor your active connections regularly to determine the actual peak usage. You can run the following query to see how many connections are currently active: SELECT count(*) FROM pg_stat_activity;. If you find that many connections are in an "idle" state for long periods, your application may be failing to close connections properly, which is a leak that no amount of configuration changes can permanently fix.

Consider optimizing your application's connection logic. Ensure that your code uses a "try-with-resources" block or a similar mechanism to guarantee that connections are returned to the pool or closed immediately after the transaction is complete. This reduces the pressure on the server and keeps the connection count stable.

Troubleshooting

If the PostgreSQL service fails to start after you increase the max_connections limit, it is likely because the server does not have enough shared memory to support the new limit. Check the system logs (e.g., /var/log/syslog or dmesg) for "out of memory" errors or "could not create shared memory segment" messages.

To resolve a startup failure, revert the max_connections value to its previous working state in postgresql.conf and restart the service. If you absolutely need more connections but the server won't start, you may need to increase the kernel's shared memory limits (SHMMAX and SHMALL) in the operating system settings, though this is less common in modern Linux distributions which use dynamic shared memory.

If you have increased the limit and restarted the service, but you are still receiving error 53400, verify that you are editing the correct postgresql.conf file. In some environments, there may be multiple versions of PostgreSQL installed, or a configuration file might be overridden by a file in a different directory (like conf.d). Use the SHOW config_file; command inside the database to be 100% certain of the file path being used by the active process.

Frequently Asked Questions

What is the recommended maximum for max_connections?

There is no single "correct" number, as it depends on your RAM and CPU. However, for most small to medium servers, keeping this value between 100 and 500 is standard. If you need more than 500, a connection pooler like PgBouncer is strongly recommended over increasing the limit further.

Does increasing max_connections slow down the database?

Yes, potentially. Each connection consumes memory and increases the overhead for the OS kernel to manage processes. If the number of active connections far exceeds the number of available CPU cores, performance will degrade due to context switching.

Can I change this limit without restarting the server?

No. max_connections is a setting that requires a full restart of the PostgreSQL service because it affects how shared memory is allocated at the start of the process.

What is the difference between max_connections and superuser_reserved_connections?

max_connections is the total number of connections allowed. superuser_reserved_connections is a subset of that total reserved exclusively for users with superuser privileges, ensuring that an administrator can always log in to fix the system even when the connection limit is reached by regular users.

Why am I getting this error if my app only has a few users?

This usually indicates a "connection leak." If your application opens a connection to the database but fails to close it (due to an unhandled exception or poor coding logic), those connections remain "idle" on the server until they time out or the server is restarted, eventually hitting the limit.