question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: connect ECONNREFUSED

See original GitHub issue

Hi,

Not sure if this is a bug or unsupported, but I’m seeing the following error:

Error: connect ECONNREFUSED DBX:5342
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)

I’m calling pg on this basis:

        const pool = new Pool ({
            user: pgUser,
            password:pgPass,
            database:pgDb,
            host:pgHost,
            port:pgPort
        });

psql -U pgUser -d pgDb -h pgHost -W works fine and my pg_hba.conf is correctly configured: host pgDb pgUser 10.0.0.0/8 md5

So I don’t think its an issue with my postgres setup ?

Node v10.14.1 Postgres 9.6.11 Pg 7.7.1

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
leventovcommented, Feb 21, 2020

If anyone stumbles upon this problem, I use the following wrapper as a workaround:

const pgPool = new Pool(pgConfig);
const pgPoolWrapper = {
    async connect() {
        for (let nRetry = 1; ; nRetry++) {
            try {
                const client = await pgPool.connect();
                if (nRetry > 1) {
                    console.info('Now successfully connected to Postgres');
                }
                return client;
            } catch (e) {
                if (e.toString().includes('ECONNREFUSED') && nRetry < 5) {
                    console.info('ECONNREFUSED connecting to Postgres, ' +
                        'maybe container is not ready yet, will retry ' + nRetry);
                    // Wait 1 second
                    await new Promise(resolve => setTimeout(resolve, 1000));
                } else {
                    throw e;
                }
            }
        }
    }
};

But I think this is a library’s job.

3reactions
leventovcommented, Feb 25, 2020

pg-pool promises to reconnect on connection failures. In the case of ECONNREFUSED, it doesn’t fulfil this promise.

To me, it feels that adding this functionality directly to the pg module is practical and user-friendly. You can also perhaps call this behavior stable (in the sense “reliable”), which you mentioned as the focus.

It’s not useful in every situation

Could you please give an example of a situation when this addition is not useful and is harmful in any way?

By default, I think,

  • “How to report errors?” - the same way as PgPool reports other errors now (which it promises to reconnect upon), that is, not reporting them, just consuming silently (or is there reporting on debug level which I don’t see? I don’t know.)
  • “Do we do exponential backoff?” - I believe the simple scheme with a few attempts to reconnect with a fixed interval should fix 99% cases where this problem arises. If anyone needs anything different, e. g. infinite backoff, they could add it on top of PgPool still, just like in the code that I posted above.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js Error: connect ECONNREFUSED - Stack Overflow
ECONNREFUSED error means that connection could not be made with the target service (in your case localhost:8080 ).
Read more >
Error: connect ECONNREFUSED 127.0.0.1:80 - Postman
It looks like you're sending a request to localhost:3000 , but your Proxy settings seem to be set to 127.0.0.1: 8080 Is your...
Read more >
How to Fix ECONNREFUSED – connection refused by server ...
One of the possible reasons for this error is that the firewall and anti-virus software on your computer is preventing FileZilla from making...
Read more >
Connect ECONNREFUSED 127.0.0.1:27017 in Mongodb ...
Means that NO mongod instance is running at the given host 127.0.0.1 and port 27017. Yes the solution is to start mongod. Mah_Neh:...
Read more >
Node.js Error: connect ECONNREFUSED - DEV Community ‍ ‍
The solution: · 1. Create an user with password, and grant all provileges. CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'; GRANT ALL ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found