{"id":112897,"date":"2026-10-21T12:00:00","date_gmt":"2026-10-21T12:00:00","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=112897"},"modified":"2026-09-29T15:13:36","modified_gmt":"2026-09-29T15:13:36","slug":"how-to-fix-too-many-connections-in-postgresql-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/how-to-fix-too-many-connections-in-postgresql-step-by-step-guide\/","title":{"rendered":"How to fix &#8220;too many connections\u201d in PostgreSQL (step-by-step guide)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Getting the <em>&#8220;too many connections&#8221;<\/em> error in PostgreSQL? You&#8217;ve hit the database&#8217;s <code>max_connections<\/code> limit \u2014 and it&#8217;s one of the most common issues developers run into as traffic grows. <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>In this guide, we&#8217;ll reproduce the error on a local PostgreSQL setup using a simple Python script, then walk through practical fixes: from closing unused connections to setting up connection pooling and monitoring active sessions with <code>pg_stat_activity<\/code>. By the end, you&#8217;ll know exactly why this error happens, and how to stop it from recurring.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-are-database-connections-and-what-happens-when-there-are-too-many\">What are database connections, and what happens when there are too many?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Database_connection\" target=\"_blank\" rel=\"noreferrer noopener\">Database connections<\/a><\/strong> are essential for any program to manage and access data in the database. When a server already has too many active and open connections &#8211; a result of a large number of users creating numerous, simultaneous demands on the system &#8211; no new connections can open, and the system indicates that it has reached its maximum connection limit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This issue can be caused by several factors, including <strong>connections that are opened unnecessarily and no longer used by the application, programs that create a large number of connections, incorrectly configured connection pools, <\/strong>and <strong>a sudden increase in traffic.<\/strong> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When these issues aren&#8217;t managed properly, the system encounters <strong>request failures<\/strong> &#8211; resulting in no new connections being made.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-the-too-many-connections-error-in-postgresql\">What is the <em>&#8220;too many connections&#8221;<\/em> error in PostgreSQL?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The <em>&#8220;too many connections&#8221;<\/em> error in <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL <\/a>is raised when a user tries to create a new connection a database that has exceeded the default maximum limit of 100 connections. <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In other words, the database already has 100 connections to it and cannot accept any more &#8211; it&#8217;s at its limit. This limit can be reached for two reasons. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, a program establishes a connection to the database when it wants to utilize PostgreSQL and will use as many connections as it requires. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Second, there&#8217;s the factor of additional users who <em>also<\/em> want to establish their own connections to the database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-why-does-the-connection-limit-get-reached\">Why does the connection limit get reached?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>When an application wants to establish a database connection to PostgreSQL, it checks the limit established by the <code><a href=\"https:\/\/www.postgresql.org\/docs\/current\/runtime-config-connection.html\" target=\"_blank\" rel=\"noreferrer noopener\">max_connections<\/a><\/code> setting. The default limit is 100. When the limit is reached, the connection fails, and thus the<em>&#8220;too many connections&#8221;<\/em> error is raised.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here are some of the reasons why the connection limit may be reached.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-rapid-connection-creation-per-request\">Rapid connection creation per request<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned earlier, a program or application establishing a connection to a PostgreSQL database will use as many connections as it requires. So, if the program generates a new database connection for each request, the number of connections increases rapidly, and the limit is reached sooner. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-ghost-connections-from-terminated-programs\">Ghost connections from terminated programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s also the issue of a program perhaps being terminated, but its connections are still active. This is because, even when the program <em>isn&#8217;t <\/em>executing a query (or being used for anything), an open connection still uses a connection slot. These are known as <strong>ghost connections<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-idle-or-long-lived-connections\">Idle or long-lived connections<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Another aspect to look at are <strong>idle (or long-lived) connections<\/strong>. Because some programs purposefully leave connections open for reuse, an idle connection is not always an issue. However, the number of slots available for future requests may be decreased by unforeseen connections that stay open for an extended period of time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-insufficient-max-connections-for-your-workload\">Insufficient <code>max_connections<\/code> for your workload<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Lastly, database connection resources are limited. Connection failures may occur when the application wants a very large number of connections and you haven&#8217;t increased the <code>max_connections<\/code> limit to accommodate for it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Increasing <code>max_connections<\/code> alone isn&#8217;t the solution, however. It&#8217;s also crucial to properly manage and monitor all active database connections.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-reproduce-the-too-many-connections-error-in-postgresql-step-by-step\">How to reproduce the <em>&#8220;too many connections&#8221;<\/em> error in PostgreSQL (step-by-step)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, I&#8217;ll demonstrate <strong>how to reproduce the problem<\/strong> on your local system. This will give you a better understanding of what the error looks like and how it works.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>If you want to get straight to the solution, feel free to skip this section.<\/em><\/p>\n\n\n\n<div id=\"callout-block_0afbc38940d3bf64fd79698aff9b5c5e\" class=\"callout alignnone\">\n    <div class=\"child-last:mb-0 child-first:mt-0 bg-gray-50 dark:bg-gray-950 p-4xl my-3xl\">\n\n<p class=\"wp-block-paragraph\"><strong>Installing PostgreSQL<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This guide assumes you have PostgreSQL already installed. If not, download the installer <a href=\"https:\/\/www.postgresql.org\/download\/windows\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n<\/div>\n<\/div> \n\n\n<h3 class=\"wp-block-heading\" id=\"h-connect-to-postgresql-using-pgadmin\">Connect to PostgreSQL using pgAdmin<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open Windows and search for <a href=\"https:\/\/www.pgadmin.org\/download\/pgadmin-4-windows\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>pgAdmin 4<\/strong><\/a>, where you&#8217;ll register the local PostgreSQL server to make it usable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>You may also be interested in: <\/strong><a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/getting-connected-to-postgresql-for-the-first-time\/#connecting-to-postgresql:~:text=meet%20your%20needs.-,pgAdmin,-This%20is%20another\" target=\"_blank\" rel=\"noreferrer noopener\">Getting connected to PostgreSQL for the first time<\/a> (featuring pgAdmin<\/em>).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-how-to-register-the-local-postgresql-server\">How to register the local PostgreSQL server<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Open pgAdmin 4. On the left side you&#8217;ll see <em>Servers<\/em> \u2014 right-click on <em>Servers<\/em>, then <em>Register<\/em>, then <em>Server<\/em>. You&#8217;ll see something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"901\" height=\"509\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image.gif\" alt=\"image showing what you see when hitting Register, and then Server\" class=\"wp-image-112898\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll see the <em>Register &#8211; Server<\/em> page. On that page, in the <em>General<\/em> section, enter the following details:<\/p>\n\n\n<div class=\"block-core-list\">\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Name- PostgreSQL Local<\/li>\n\n\n\n<li>Server Group &#8211; Server<\/li>\n<\/ol>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">And everything remains the same:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"901\" height=\"509\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-9.gif\" alt=\"an image showing that everything remains the same.\" class=\"wp-image-112912\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now click on the <em>Connection<\/em> tab and add the following details:<\/p>\n\n\n<div class=\"block-core-list\">\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Host name- localhost<\/li>\n\n\n\n<li>Port- 5432<\/li>\n\n\n\n<li>Maintenance database &#8211; postgres<\/li>\n\n\n\n<li>Username- postgres<\/li>\n\n\n\n<li>Password- \u201center your admin password\u201d<\/li>\n<\/ol>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Here, use the password you created when installing PostgreSQL:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"901\" height=\"509\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-6.gif\" alt=\"an image showing use of the password you created when installing PostgreSQL.\" class=\"wp-image-112904\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Leave all the other sections as they are, then click <em>Save<\/em>. You can now create the server on which you&#8217;ll run the queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll now see the following page, where <em>PostgreSQL Local<\/em> appears under <em>Servers<\/em>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"326\" height=\"116\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-4.gif\" alt=\"an image showing the page where PostgreSQL Local appears under Servers.\" class=\"wp-image-112901\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-open-the-postgresql-query-tool\">Open the PostgreSQL Query Tool<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To run a query, open the <a href=\"https:\/\/www.pgadmin.org\/docs\/pgadmin4\/8.14\/query_tool.html\" target=\"_blank\" rel=\"noreferrer noopener\">Query Tool<\/a> under the <em>PostgreSQL Local<\/em> server you created earlier.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-how-to-create-a-sql-query-session\">How to create a SQL Query session<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To create the SQL query session under <em>PostgreSQL Local<\/em>, left-click on the database, then click on the Query Tool:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"901\" height=\"509\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-14.gif\" alt=\"an image showing how to create the SQL query session under the PostgreSQL Local server.\" class=\"wp-image-112917\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-verify-the-postgresql-installation\">Verify the PostgreSQL installation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s important to verify that everything has installed to PostgreSQL correctly.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-how-to-verify-check-the-version-of-postgresql-you-re-running\">How to verify (check) the version of PostgreSQL you&#8217;re running<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To check the version of PostgreSQL you&#8217;re currently running, run the <code>SELECT version();<\/code> command.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You should receive this output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"318\" height=\"116\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-1.gif\" alt=\"An image showing the output you should receive.\" class=\"wp-image-112899\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the output, you&#8217;ll see the version of PostgreSQL you&#8217;re currently running. Having this output appear means everything is working correctly, so you can continue onto the next step.<\/p>\n\n\n\n<section id=\"my-first-block-block_4dfed756ed2480500b8902f74f6afcb7\" class=\"my-first-block alignwide\">\n    <div class=\"bg-brand-600 text-base-white py-5xl px-4xl rounded-sm bg-gradient-to-r from-brand-600 to-brand-500 red\">\n        <div class=\"gap-4xl items-start md:items-center flex flex-col md:flex-row justify-between\">\n            <div class=\"flex-1 col-span-10 lg:col-span-7\">\n                <h3 class=\"mt-0 font-display mb-2 text-display-sm\">Get started with PostgreSQL &#8211; free book download<\/h3>\n                <div class=\"child:last-of-type:mb-0\">\n                                            &#8216;Introduction to PostgreSQL for the data professional&#8217;, written by Grant Fritchey and Ryan Booz, covers all the basics of how to get started with PostgreSQL.                                    <\/div>\n            <\/div>\n                                            <a href=\"https:\/\/www.red-gate.com\/hub\/books\/introduction-to-postgresql-for-the-data-professional\/\" class=\"btn btn--secondary btn--lg\" aria-label=\"Download your free copy: Get started with PostgreSQL - free book download\">Download your free copy<\/a>\n                    <\/div>\n    <\/div>\n<\/section>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-check-postgresql-s-connection-limit\">Check PostgreSQL&#8217;s connection limit<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, check the <strong>maximum connection limit of your PostgreSQL server. By default, it&#8217;s 100 \u2014 this is the maximum number of concurrent client connections the database server will accept at one time.<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-how-to-find-the-max-connections-value-nbsp\">How to find the <code>max_connections<\/code> value&nbsp;<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To find the maximum connection (<code>max_connections<\/code>) value, run the <code>SHOW max_connections;<\/code> command. This command shows how many connections your server accepts at any one time:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"318\" height=\"116\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-1.gif\" alt=\"an image showing the output you should receive to know your PostgreSQL's maximum number of connections (100, by default).\" class=\"wp-image-112907\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The output shows 100 connections that the database server will accept at one time. This is also the default.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-check-the-current-postgresql-connections\">Check the current PostgreSQL connections<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s now time to check the current PostgreSQL connections \u2014 in other words, how many connections are active in your system.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-how-to-count-the-existing-postgresql-connections\">How to count the existing PostgreSQL connections<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Run:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>SELECT count(*)<br>FROM pg_stat_activity;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The output you should receive is:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"318\" height=\"116\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-1.gif\" alt=\"\" class=\"wp-image-112905\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Record how many connections there are before going any further. The output above shows that there are currently 10 PostgreSQL connections.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-inspect-the-active-postgresql-sessions\">Inspect the active PostgreSQL sessions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you&#8217;ve checked the total number of connections (10), the next step is to look at the individual PostgreSQL sessions. You&#8217;ll use the <code>pg_stat_activity<\/code> system view for this. This view contains information about the current connections, and what activity they&#8217;re doing in the session. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following SQL query prints the current connection details:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>SELECT<br>&nbsp;&nbsp;&nbsp; pid,<br>&nbsp;&nbsp;&nbsp; usename,<br>&nbsp;&nbsp;&nbsp; datname,<br>&nbsp;&nbsp;&nbsp; application_name,<br>&nbsp;&nbsp;&nbsp; client_addr,<br>&nbsp;&nbsp;&nbsp; state<br>FROM pg_stat_activity<br>ORDER BY pid;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The different columns tell us where a connection came from, which database is used, and whether the connection is <em>active<\/em> or<em> inactive<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The output you should receive is:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"393\" height=\"263\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-16.gif\" alt=\"\" class=\"wp-image-112920\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-create-a-dedicated-test-database\">Create a dedicated test database<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The next step is to create a test database purely for this recreation (to be used later on). This is just to ensure you don&#8217;t have to interfere with your existing local database. To do this, run <code>CREATE DATABASE test_connection;<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After running the query, you should have now successfully created the database:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"693\" height=\"123\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-2.gif\" alt=\"the output you'll receive when you create the database.\" class=\"wp-image-112900\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-temporarily-lower-the-connections-limit\">Temporarily lower the connections limit<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now it&#8217;s time to reproduce the error &#8211; and then fix it. As mentioned earlier: the maximum connections limit is 100, by default, so you&#8217;ll reduce it to 20 and then run multiple connections for the purpose of triggering the &#8220;too many connections&#8221; error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, run <code>SHOW max_connections;<\/code> to again check that the connections limit is currently 100.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, run the <code>ALTER SYSTEM SET max_connections = 20;<\/code> query to reduce the connections limit to 20. You should see:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"566\" height=\"101\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-5.gif\" alt=\"\" class=\"wp-image-112902\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Your maximum connections limit should now be 20. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-how-to-verify-the-new-limit\">How to verify the new limit<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To verify this, restart PostgreSQL and, once reopened, run the <code>SHOW max_connections;<\/code> query. You should now see:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"326\" height=\"116\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-4.gif\" alt=\"The output you should now see, showing max connections of 20.\" class=\"wp-image-112903\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-create-multiple-postgresql-connections\">Create multiple PostgreSQL connections<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">So, the next step is to create multiple PostgreSQL connections &#8211; all connecting to the <code>test_connection<\/code> database you created a bit earlier. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To put it simply, the <code>test_connection<\/code> database is like a room, and each connection is like a person entering that room. We&#8217;re now going to deliberately put too many people in the room. This will trigger the &#8220;too many connections&#8221; error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-create-the-python-connection-test\">Create the Python connection test<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, though, you&#8217;ll need to create a small Python program. When we run the program, the code will open multiple connections for us automatically.<\/p>\n\n\n\n<div id=\"callout-block_0afbc38940d3bf64fd79698aff9b5c5e\" class=\"callout alignnone\">\n    <div class=\"child-last:mb-0 child-first:mt-0 bg-gray-50 dark:bg-gray-950 p-4xl my-3xl\">\n\n<p class=\"wp-block-paragraph\"><strong>Don&#8217;t have Python installed on your system?<\/strong> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Download and install it from the official Python website <a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>If you&#8217;re unsure whether you have Python installed already, or simply want to check which version you have, you can run the<\/em> <code>python --version<\/code> <em>command through Command Prompt.<\/em><\/p>\n\n<\/div>\n<\/div> \n\n\n<h4 class=\"wp-block-heading\" id=\"h-how-to-create-a-local-connection-test\">How to create a local connection test<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The Python program you&#8217;ll create has one purpose only: open multiple connections on PostgreSQL (and keep them open for some amount of time.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before creating the Python program, you&#8217;ll need a folder on the local system to hold it. Create one called <code>postgres-test<\/code>, open it, and open Command Prompt <em>within <\/em>the folder. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, install the PostgreSQL Python driver by running <code>python -m pip install psycopg<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This should only take a minute or two and, when finished, you should see some indication that <code>psycopg<\/code> installed successfully:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"216\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-17.gif\" alt=\"Image showing the output once psycopg has installed.\" class=\"wp-image-112921\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">With <code>psycopg<\/code> installed, you can now create the Python test file.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-what-is-psycopg\">What is <code>psycopg<\/code>?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>psycopg<\/code> is a popular PostgreSQL adapter for Python &#8211; learn more <a href=\"https:\/\/www.psycopg.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> (website) and <a href=\"https:\/\/www.psycopg.org\/docs\/index.html#\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> (documentation).<\/p>\n\n\n\n<div id=\"callout-block_0afbc38940d3bf64fd79698aff9b5c5e\" class=\"callout alignnone\">\n    <div class=\"child-last:mb-0 child-first:mt-0 bg-gray-50 dark:bg-gray-950 p-4xl my-3xl\">\n\n<p class=\"wp-block-paragraph\"><strong>You may also be interested in&#8230;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/how-data-scientists-work-with-postgresql-from-python-and-how-to-write-queries-that-dont-break-at-scale\/\" target=\"_blank\" rel=\"noreferrer noopener\">How data scientists work with PostgreSQL from Python (and how to write queries that don\u2019t break at scale)<\/a><\/p>\n\n<\/div>\n<\/div> \n\n\n<h4 class=\"wp-block-heading\" id=\"h-how-to-create-the-python-test-file-and-add-the-connection-test-code\">How to create the Python test file and add the connection test code<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In the <code>postgres-test<\/code> folder, create a Python file and save it as <code>Test.py<\/code>. Open this in Notepad and paste in the following code, which will create multiple connections (and keep them open):<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>import psycopg<br>import time<br><br>connections = []<br><br>for i in range(30):<br>&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conn = psycopg.connect(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; host=\"localhost\",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; port=5432,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbname=\"test_connection\",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; user=\"postgres\",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; password=\"YOUR_POSTGRES_PASSWORD\"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connections.append(conn)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(f\"Connection {i + 1} opened successfully\")<br><br>&nbsp;&nbsp;&nbsp; except Exception as e:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(f\"Connection {i + 1} failed:\")<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(e)<br><br>time.sleep(300)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This code uses <code>psycopg<\/code> to open 30 separate PostgreSQL connections. Each successful one is stored in a Python list, where they remain open for 300 seconds &#8211; allowing you to observe their effect on the PostgreSQL server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Remember, you previously changed <code>max_connections<\/code> to 20, so this Python program will give you the &#8220;too many connections&#8221; error you want to trigger. It&#8217;ll also report if any of the connections suffer failures within the 300 seconds they are open.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the code, replace the password with the one you created when you installed PostgreSQL.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-how-to-check-the-current-postgresql-connections-again\">How to check the current PostgreSQL connections (again)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Before running the Python program, check the current PostgreSQL connections again (in other words, how many connections are active in your system.) It&#8217;s the same process as earlier &#8211; simply run:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>SELECT count(*)<br>FROM pg_stat_activity;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"198\" height=\"116\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-3.gif\" alt=\"\" class=\"wp-image-112906\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">It should give you the output above, confirming that your system currently has 10 active sessions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-run-the-connection-test\">Run the connection test<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now go back to your command prompt and run the Python program: <code>Python test.py<\/code>. This will show you the results:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"552\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-22.gif\" alt=\"an image showing the results of running the Python test.py program.\" class=\"wp-image-112926\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can clearly see that the connection attempts began failing once the available connection slots were exhausted. For example, connection 21 failed (which you can also see in Command Prompt.) So, that confirms you&#8217;ve successfully recreated the error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Don&#8217;t close Command Prompt just yet! It deliberately sleeps for 300 seconds so the connections remain available for inspection.<\/em><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-how-to-check-the-connections-from-pgadmin\">How to check the connections from pgAdmin<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">While the Python program is still running, go back to your pgAdmin 4 page and run the same query as above:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>SELECT count(*)<br>FROM pg_stat_activity;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You should now see that, during the test, there are 28 active connections &#8211; a big jump from the earlier 10.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-inspect-the-connections\">Inspect the connections<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You now need to inspect the connections during the test. The Python program keeps successful connections open for 300 seconds, so go back to the Query Tool and run the SQL query to see the 28 active sessions opened by the program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The SQL query is:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>SELECT<br>&nbsp;&nbsp;&nbsp; pid,<br>&nbsp;&nbsp;&nbsp; usename,<br>&nbsp;&nbsp;&nbsp; datname,<br>&nbsp;&nbsp;&nbsp; application_name,<br>&nbsp;&nbsp;&nbsp; client_addr,<br>&nbsp;&nbsp;&nbsp; state<br>FROM pg_stat_activity<br>ORDER BY pid;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This query displays the active PostgreSQL sessions. Since you now have 28 active sessions, it shows the process ID, username, database, application name, client address, and connection state for all of them. This is useful for checking whether the database connections are still active.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"824\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-20.gif\" alt=\"Image showing the output: 28 active sessions.\" class=\"wp-image-112925\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"258\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-19.gif\" alt=\"Image showing the output: 28 active sessions.\" class=\"wp-image-112924\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, the output shows all 28 active sessions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-observe-the-failed-connections\">Observe the failed connections<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Take another look at Command Prompt, where you ran the Python program. You&#8217;ll see that the available connection slots were exhausted during the test, and the server returned the <em>\u201ctoo many clients already\u201d<\/em> error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The program tried to open 30 connections but, since the limit was set to 20, it failed to open the 21st connection. This is exactly what you wanted to happen.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"552\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-21.gif\" alt=\"image showing the output\" class=\"wp-image-112927\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Look at the failed connection in Command Prompt. You&#8217;ll see the <em>\u201csorry, too many clients already\u201d<\/em> error:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"18\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-8.gif\" alt=\"&quot;sorry, too many clients already&quot; error\" class=\"wp-image-112911\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-compare-before-and-during-the-test\">Compare before <em>and<\/em> during the test<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"h-compare-before-and-during-the-test-compare-the-connections-we-see-the-following-result\">Comparing the connections, you&#8217;ll see that <em>before<\/em> the experiment, <code>pg_stat_activity<\/code> showed 10 PostgreSQL sessions. <em>During <\/em>the experiment, that increased to 28 sessions. And then, <em>after<\/em> the Python program finished, the number returned to 10 sessions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-stop-the-python-test-nbsp\">Stop the Python test&nbsp;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The code automatically stops after 300 seconds, closing all the active connections and terminating the program: <code>time.sleep(300)<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-check-what-happens-after-the-program-stops\">Check what happens after the program stops<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After the program stops, check the active connections again, using the same command as earlier:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>SELECT count(*)<br>FROM pg_stat_activity;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You should see that the number of connections decreased once the Python program terminated and closed all its connections. The active connection count is now back to 10 (from 28):<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"206\" height=\"116\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-7.gif\" alt=\"active connection count is now back down to 10, from 28\" class=\"wp-image-112910\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Run this SQL query to inspect the connections:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>S<code>ELECT<br>&nbsp;&nbsp;&nbsp; pid,<br>&nbsp;&nbsp;&nbsp; usename,<br>&nbsp;&nbsp;&nbsp; datname,<br>&nbsp;&nbsp;&nbsp; application_name,<br>&nbsp;&nbsp;&nbsp; client_addr,<br>&nbsp;&nbsp;&nbsp; state<br>FROM pg_stat_activity<br>ORDER BY pid;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The query displays the active PostgreSQL sessions. Since you now have 10 active sessions, it shows the process ID, username, database, application name, client address, and connection state for all of them:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"902\" height=\"448\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-18.gif\" alt=\"Image showing the query output:  displaying the active PostgreSQL sessions. Since you now have 10 active sessions, it shows the process ID, username, database, application name, client address, and connection state for all of them.\" class=\"wp-image-112922\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">After closing the Python program, all of the extra sessions will be closed as well.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This experiment\/recreation showed that we can only open a limited number of connections. Once that limit is reached, new connections fail, triggering the &#8220;too many connections&#8221; error. <\/p>\n\n\n\n<section id=\"my-first-block-block_f735b9c6d1c1ac4ade3313fccfe6e98d\" class=\"my-first-block alignwide\">\n    <div class=\"bg-brand-600 text-base-white py-5xl px-4xl rounded-sm bg-gradient-to-r from-brand-600 to-brand-500 red\">\n        <div class=\"gap-4xl items-start md:items-center flex flex-col md:flex-row justify-between\">\n            <div class=\"flex-1 col-span-10 lg:col-span-7\">\n                <h3 class=\"mt-0 font-display mb-2 text-display-sm\">Free desktop tool for fast PostgreSQL monitoring and diagnostics<\/h3>\n                <div class=\"child:last-of-type:mb-0\">\n                                            Stay in control of PostgreSQL performance with Redgate pgNow \u2013 a free desktop tool for fast, focused diagnostics. No agents, no setup, just actionable insights when you need them.                                    <\/div>\n            <\/div>\n                                            <a href=\"https:\/\/www.red-gate.com\/products\/redgate-pgnow\/\" class=\"btn btn--secondary btn--lg\" aria-label=\"Learn more &amp; download now: Free desktop tool for fast PostgreSQL monitoring and diagnostics\">Learn more &amp; download now<\/a>\n                    <\/div>\n    <\/div>\n<\/section>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-restore-your-postgresql-configuration\">Restore your PostgreSQL configuration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With the recreation complete, don&#8217;t forget to change your PostgreSQL server&#8217;s configuration back to the default <code>max_connections<\/code> value of 100, using the <code>ALTER SYSTEM SET max_connections = 100;<\/code> command.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>It&#8217;s now time to fix the <em>&#8220;too many connections&#8221;<\/em> error &#8211; and there are a few different ways to do so.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-fix-the-too-many-connections-error-in-postgresql\">How to fix the &#8220;too many connections&#8221; error in PostgreSQL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Here are a few ways to fix (and prevent) the <em>&#8220;too many connections&#8221;<\/em> error in PostgreSQL<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-close-database-connections-properly\">Close database connections properly<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Your application code may create connections and then forget to close them. So, check that <em>you<\/em> always close database connections after completing <em>your<\/em> work. It sounds simple, but it&#8217;s a common mistake. To close the connections, run the <code>time.sleep(300)<\/code> command.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-use-database-connection-pooling\">Use database connection pooling<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.cockroachlabs.com\/blog\/what-is-connection-pooling\/\" target=\"_blank\" rel=\"noreferrer noopener\">Database connection pooling<\/a> is another good approach to fix and prevent <em>&#8220;too many connections&#8221;<\/em>. Instead of establishing a new connection for each request, connection pooling lets you reuse a finite number of database connections. Applications then <em>reuse<\/em> these connections, so far fewer connections are used overall.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-avoid-blindly-increasing-max-connections\">Avoid blindly increasing <code>max_connections<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>max_connections<\/code> is increased, more client sessions may be able to connect, but PostgreSQL will also use more resources. Only increase or decrease this setting once you understand the program&#8217;s real connection needs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-investigate-and-clean-up-idle-connections\">Investigate and clean up idle connections<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To find connections that have been inactive for an extended period, use <code>pg_stat_activity<\/code>. This generates any idle connection &#8211; one that is no longer active in the PostgreSQL database. An idle connection could be a sign of improper connection release by apps, or ineffective connection handling. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, some idle connections may be maintained intentionally by connection pooling, so do<em> not<\/em> blindly close them! Investigate each idle connection &#8211; especially long-lived ones &#8211; <em>before<\/em> taking action.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following query to raise a list of idle connections no longer in use:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>SELECT<br>&nbsp;&nbsp;&nbsp; pid,<br>&nbsp;&nbsp;&nbsp; usename,<br>&nbsp;&nbsp;&nbsp; datname,<br>&nbsp;&nbsp;&nbsp; application_name,<br>&nbsp;&nbsp;&nbsp; client_addr,<br>&nbsp;&nbsp;&nbsp; state<br>FROM pg_stat_activity<br>WHERE state = 'idle'<br>ORDER BY pid;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The summary of an idle connection will look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"96\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-15.gif\" alt=\"the output showing all idle sessions\" class=\"wp-image-112918\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-monitor-connection-usage-regularly\">Monitor connection usage regularly<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s recommended to use <code>pg_stat_activity<\/code> to regularly monitor the number (and state) of PostgreSQL connections. Tracking every connection helps to identify unusual increases before they result in errors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To see the current number of sessions, run the following query:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>SELECT count(*)<br>FROM pg_stat_activity;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"218\" height=\"129\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-10.gif\" alt=\"An image showing the output: 12 active sessions in your database.\" class=\"wp-image-112913\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The output shows that there are currently 12 active sessions in your database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For troubleshooting, you can also <em>individually<\/em> inspect sessions by running this query:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>SELECT<br>&nbsp;&nbsp;&nbsp; datname,<br>&nbsp;&nbsp;&nbsp; count(*)<br>FROM pg_stat_activity<br>GROUP BY datname<br>ORDER BY count(*) DESC;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The query groups all PostgreSQL connections by the database they&#8217;re associated with, and returns a count for each. It also identifies which database has the most connections.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"393\" height=\"263\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-16.gif\" alt=\"image showing the results of the query: it groups all PostgreSQL connections by the database they're associated with, and returns a count for each. It also identifies which database has the most connections.\" class=\"wp-image-112919\"\/><\/figure>\n\n\n\n<section id=\"faq\" class=\"faq-block my-5xl\">\n    <h2>FAQs<\/h2>\n\n                        <h3 class=\"mt-4xl\">1. What causes the &quot;too many connections&quot; error in PostgreSQL?<\/h3>\n            <div class=\"faq-answer\">\n                <p dir=\"ltr\">It happens when the number of active database connections reaches the <code><strong>max_connections<\/strong><\/code> limit (100 by default). Common causes include <strong>unclosed connections<\/strong>, misconfigured connection pools, ghost connections from terminated programs, and sudden traffic spikes.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">2. How do I check my current PostgreSQL connection limit?<\/h3>\n            <div class=\"faq-answer\">\n                <p dir=\"ltr\">Run <code>SHOW max_connections;<\/code> in your query tool. To see how many connections are currently active, run <code>SELECT count(*) FROM pg_stat_activity;<\/code><\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">3. Should I just increase max_connections to fix the error?<\/h3>\n            <div class=\"faq-answer\">\n                <p>Not necessarily. <strong>Increasing <code>max_connections<\/code><\/strong> <em>without<\/em> addressing the root cause uses more server resources and can mask deeper issues like connection leaks or missing pooling. Fix the underlying problem first.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">4. What is connection pooling, and does it help?<\/h3>\n            <div class=\"faq-answer\">\n                <p dir=\"ltr\">Yes \u2014 <strong>connection pooling<\/strong> lets your application reuse a fixed set of database connections instead of opening a new one for every request, drastically reducing total connection usage.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">5. How can I find idle or &quot;ghost&quot; connections?<\/h3>\n            <div class=\"faq-answer\">\n                <p>Query <code>pg_stat_activity<\/code> filtered by <code>state = 'idle'<\/code>. This reveals connections that are open but inactive \u2014 though some idle connections are intentional (e.g., pooled), so <strong>investigate before closing<\/strong> them.<\/p>\n            <\/div>\n            <\/section>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong><em>&#8220;too many connections&#8221;<\/em> error<\/strong> is usually a sign of unclosed connections, missing pooling, or unmonitored traffic growth. By reproducing the issue locally, you can see exactly how connection limits behave under pressure and apply the right fix: <strong>closing connections properly<\/strong>, <strong>implementing pooling<\/strong>, and <strong>regularly monitoring<\/strong> <code>pg_stat_activity<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Get these fundamentals right, and you&#8217;ll rarely see the PostgreSQL <em>&#8220;too many connections&#8221;<\/em> error in production again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getting PostgreSQL&#8217;s &#8220;too many connections&#8221; error? Learn what causes it, how to reproduce it safely, and 5 proven fixes \u2014 including connection pooling and monitoring with pg_stat_activity.&hellip;<\/p>\n","protected":false},"author":347206,"featured_media":105920,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143523,53,143534],"tags":[4168,4170,158978,4150],"coauthors":[159414],"class_list":["post-112897","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-databases","category-featured","category-postgresql","tag-database","tag-database-administration","tag-postgresql","tag-sql"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/112897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/347206"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=112897"}],"version-history":[{"count":13,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/112897\/revisions"}],"predecessor-version":[{"id":112954,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/112897\/revisions\/112954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media\/105920"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=112897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=112897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=112897"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=112897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}