Updated May 25, 2023
Introduction to PostgreSQL max connections
The following article provides an outline for PostgreSQL max connections. PostgreSQL has a property to set a maximum number of connections; postgreSQL uses different resources at that time; it is important to set how many connections we require when we tune deployment performance. PostgreSQL uses the max_connection setting to set a maximum number of connections to deploy the resources. We can arrange a maximum number of connections to the database, some connections are reserved for super users to maintain the integrity of the database, and some connections of PostgreSQL are reserved for our application, or we can say system application. When we reach the limit of the database connection, then a new connection fails, and it returns an error.
Syntax:
alter system set max connections = size of connection.
Explanation:
- In the above syntax, we use the alter command to set a maximum connection to the system database with any connection size. The size of the connection should be integer only.
- The max_conncection determines the number of concurrent connections to the database server. Typically the default size of a database connection is 100, but we can change it as per our requirement.
Maximum Limit of Connection in PostgreSQL?
- PostgreSQL has a different provision to set maximum connection.
- PostgreSQL has a maximum of 115 connections to the database, 15 connections are reserved to the super user to maintain the integrity of the PostgreSQL database, and 100 PostgreSQL connections are reserved for system applications.
- When we exceed the limit of the database connection, then it shows an error message.
How to Increase connection in PostgreSQL?
There are four different states of database connection in PostgreSQL.
- active: This state is used to show the connection is working, or we can say the connection is active.
- idle: This state shows the connection is in idle condition, and we can track the connection by using time.
- idle in the transaction: Idle in transaction means there is no input received from the end-user, so it is waiting for the state.
- idle in transaction (aborted): This state is the same as idle in the transaction; it shows the transaction is in an idle state, and an error causes the transaction. So we need to monitor transactions by using time.
Let’s see how we can increase connections in PostgreSQL by using the following steps as follows:
1. We can install the PostgreSQL database connector from the official website of PostgreSQL. After completing down, copy this JAR file and paste it into the Lib directory.
For example, we are supposed to use the Tomcat server; then, we paste the JAR file into the Tomcat home/ Lib.
2. We can increase connection size by using postgresql.conf file. We can add or edit the max connection property, which means max_connection=value.
For example, we follow the path from your system to set the maximum connection size. C:\Program Files\PostgreSQL\12\data\ PostgreSQL. conf.
Open the postgresql.conf file, then add or edit the max_connection property manually.
Such as max_connection=150.
3. After that, we will need to restart your database.
But this is not the right way to increase the maximum connection size, or we can say this is a bad habit. So a better way is to increase the size of the shared buffer and the kernel.
Max_connection property in PostgreSQL determines concurrent connection to the database server. Before increasing the size of the connection, you need to scale up system deployment. Every PostgreSQL connection uses system RAM to maintain connection; if we have more connections, we require more RAM to maintain them. If we have a well-developed app, then we do not need to increase the connection size.
Example of PostgreSQL max connections
By using postgresql.conf, we can change the database connection size using the following statement.
We can change the max_connection size as follows:
Code:
max_connection= 200
Explanation:
- In the above statement, we use the max_connection command with the connection size, as shown in the statement.
- Outline of the above statement, or the final output illustrated by using the following snapshot.
Output:
In the above snapshot, we show max_connactions size is 100, which is, by default, the size of a database connection. The below snapshot shows the modified max_connection size of the database as follows.
Output:
After modification of the database connection size, we need to restart the database. We can also create new databases with the same size of database connection with username and password, but every time might be checked; really, we need a max connection to the database because it directly affects database performance.
We already saw that increasing the max connection size is not the right way, so a better way we can increase the size of the shared buffer by using the abovementioned path of the postgresql.conf file. Let’s see how we can improve the shared buffer size using the following statement.
Code:
shared_buffers = 128MB
Explanation:
- In the above statement, we use shared_buffers with default size, and we can easily change the size of shared_buffers manually by changing the value of size as follows Outline of the above statement, or we can say the final output illustrated by using the following snapshot.
Output:
The above snapshot shows the default size of shared_buffers, and the below snapshot shows after modification of shared_buffers size as follows.
Output:
The shared_buffers attribute shows how much memory PostgreSQL uses to cache the data. If we have a system with 1GB or more RAM, then shared_bffers uses one-fourth the size of RAM. Another important point about shared_buffers is that if we use a 32-bit system or PostgreSQL, it is impossible to increase the size of shared_buffers; we can increase it up to 2 GB.
If anyone is not able to find a postgresql.conf file, then we can use the following statement as follows.
Code:
show config_file;
Explanation:
- We use the show command with the config_file parameter in the above statement.
- Outline of the above statement, or the final output illustrated by using the following snapshot.
Output:
Conclusion
From the above article, we have seen the basic syntax of how to set the max connection in PostgreSQL. Moreover, we have determined how to implement them in PostgreSQL with specific examples of every technique. From this article, we have seen how we can handle max connection in PostgreSQL.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL max connections” was beneficial to you. You can view EDUCBA’s recommended articles for more information.