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 set a maximum number of connections to the database, some connections are reserved to super users for maintaining 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 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 size of the connection. 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 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 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 in a working state, or we can say the connection is active.
- idle: This state is used to show the connection is in idle condition, and we can track the connection by using time.
- idle in transaction: Idle in transaction means there is no input received from the end-user, so it is in waiting for the state.
- idle in transaction (aborted): This state is the same as idle in transaction; it shows transaction is in idle state and transaction is caused by error. 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 completion of down, copy this JAR file and paste it in 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. In which we can add or edit max connection property that means max_connection=value.
For example, we follow the path from your system to set the maximum size of the connection. C:\Program Files\PostgreSQL\12\data\ postgresql.conf.
Open postgresql.conf file, then add or edit 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 size of connection, or we can say this is a bad habit. So a better way is to increase the size of the shared buffer as well as the kernel.
Max_connection property in PostgreSQL determines concurrent connection to the database server. Before increasing the size of the connection, you just 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 size of the connection.
Example of PostgreSQL max connections
By using postgresql.conf, we can change the database connection size by using the following statement.
We can simply change the max_connection size as follows:
Code:
max_connection= 200
Explanation:
- In the above statement, we use the max_connection command with the size of the connection, as shown in the statement.
- Outline of the above statement or we can say the final output of illustrated by using the following snapshot.
Output:
In the above snapshot, we show max_connactions size is 100, and this 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 database connection size, we just 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 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 try to see how we can increase the shared buffer size by 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 just change the value of size as follows Outline of the above statement, or we can say the final output of 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 is used to show how much memory is used to PostgreSQL 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 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:
- In the above statement, we use the show command with the config_file parameter.
- Outline of the above statement or we can say the final output of 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 we can implement them in PostgreSQL with specific examples of each and every technique. From this article, we have seen how we can handle max connection in PostgreSQL.
Recommended Articles
This is a guide to PostgreSQL max connections. Here we discuss the introduction, maximum limit and how to increase connection in PostgreSQL? Example. You may also have a look at the following articles to learn more –