Question

Connection Error

I am using only one machine for one at a time & facing below mentioned error multiple times in a single day

original: error: remaining connection slots are reserved for non-replication superuser connections
at Parser.parseErrorMessage (C:\Users\ravi\Hezky_2.0\hezky_uat_node_2.0\node_modules\pg-protocol\dist\parser.js:287:98)
at Parser.handlePacket (C:\Users\ravi\Hezky_2.0\hezky_uat_node_2.0\node_modules\pg-protocol\dist\parser.js:126:29)
at Parser.parse (C:\Users\ravi\Hezky_2.0\hezky_uat_node_2.0\node_modules\pg-protocol\dist\parser.js:39:38)
readable:390:5)
at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
length: 140,
severity: 'FATAL',
code: '53300',
detail: undefined,
hint: undefined,
position: undefined,
at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
length: 140,
severity: 'FATAL',
code: '53300',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'postinit.c',
line: '813',
routine: 'InitPostgres'
}
}

can someone please help to know what exactly I need to do? my plan: 1 GB RAM / 1vCPU / 10 GB Disk / Primary only / NYC3 - PostgreSQL 16


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
July 8, 2024

Heya,

The error message you are seeing, remaining connection slots are reserved for non-replication superuser connections, indicates that your PostgreSQL server has reached the maximum number of allowed connections and has reserved the remaining slots for superuser connections. This can prevent regular users from establishing new connections to the database.

The error message you are seeing, remaining connection slots are reserved for non-replication superuser connections, indicates that your PostgreSQL server has reached the maximum number of allowed connections and has reserved the remaining slots for superuser connections. This can prevent regular users from establishing new connections to the database.

Steps to Resolve the Issue

1. Check and Increase the Max Connections Setting

You can increase the max_connections setting in your PostgreSQL configuration to allow more connections. Here’s how:

  1. Edit the PostgreSQL Configuration File:

    Locate and edit the postgresql.conf file. This file is typically found in the PostgreSQL data directory (e.g., /etc/postgresql/[version]/main/postgresql.conf on Ubuntu or /var/lib/pgsql/[version]/data/postgresql.conf on CentOS).

sudo nano /etc/postgresql/[version]/main/postgresql.conf
  1. Increase the max_connections Parameter:

    Find the line that sets max_connections and increase its value. For example, to increase the maximum connections to 200:

max_connections = 200
  1. Restart PostgreSQL:

    After making changes to the configuration file, restart the PostgreSQL service to apply the changes.

sudo systemctl restart postgresql
Bobby Iliev
Site Moderator
Site Moderator badge
July 5, 2024

Hi there,

As far as I can tell based on the output that you’ve shared, the error suggests that your PostgreSQL database has reached its maximum number of allowed connections.

The message “remaining connection slots are reserved for non-replication superuser connections” indicates that all available connection slots are in use, and the system is reserving the last few for superuser connections.

The potential causes for this could be:

  • Too many simultaneous connections to the database
  • Connections not being properly closed after use
  • Connection pooling not configured or not working correctly

What you could do here in order resolve the error is:

a. Check current connections:

  • Connect to your database and run:
    SELECT count(*) FROM pg_stat_activity;
    

This will show you how many active connections you currently have.

c. Implement connection pooling:

d. Optimize your code:

  • The most important thing here is to make sure that you’re closing connections when they’re no longer needed
  • Use connection pooling to reuse connections instead of creating new ones for each query

e. Consider upgrading your plan: Your current plan (1 GB RAM / 1vCPU) might be limiting the number of connections. Upgrading could allow for more simultaneous connections.

On the connection pooling implementation, here is a quick example:

const { Pool } = require('pg');

const pool = new Pool({
   user: 'your_username',
   host: 'your_host',
   database: 'your_database',
   password: 'your_password',
   port: 5432,
   max: 20, // set to a reasonable number based on your needs
   idleTimeoutMillis: 30000,
   connectionTimeoutMillis: 2000,
});

// Use the pool in your queries
pool.query('SELECT NOW()', (err, res) => {
   console.log(err, res);
   pool.end();
});

After implementing these changes, monitor your application’s performance and database connection usage.

Let me know how it goes!

- Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel