Skip to content

ECONNREFUSED inconsistency #641

Closed
Closed

Description

@mattbishop

sql client instances handle ECONNREFUSED errors inconsistently. This error is thrown when the database is unreachable. In the steps below, I am in control of the postgres process. I would think the same steps work with network access as well.

The Good

  1. Launch Postgres.
  2. Launch Node app and create client instance: const sql = postgres(opts).
  3. Shut down the Postgres process.
  4. Client uses sql instance to run queries. These fail, as expected.
  5. Relaunch Postgres.
  6. Client uses sql instance to run queries. They succeed.

The Not-So-Good

  1. Shut down Postgres, or ensure it is not running.
  2. Launch Node app and create client instance: const sql = postgres(opts).
  3. Client uses sql instance to run queries. These fail, as expected. Node throws this error, ONCE:
Error: connect ECONNREFUSED 127.0.0.1:5432
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1592:16)
    at cachedError (file:///Users/matt/Projects/pg-test/node_modules/postgres/src/query.js:171:23)
    at new Query (file:///Users/matt/Projects/pg-test/node_modules/postgres/src/query.js:36:24)
    at sql (file:///Users/matt/Projects/pg-test/node_modules/postgres/src/index.js:111:11)
    at Function.listen (file:///Users/matt/Projects/pg-test/node_modules/postgres/src/index.js:179:35)
  1. Launch Postgres.
  2. Client uses sql instance to run queries. These fail. This is unlike the first set of steps, where the connection is re-established and queries proceed as normal.

So should they behave the same? If this is expected behaviour, what is the right pattern to use to keep the sql instance valid?

Activity

  1. porsager commented on Jul 15, 2023

    @porsager
    Owner

    Very nice description ! That is definitely not intended, I'll see if I can find some time to repro and look into it.

  2. added
    bug
    on Jul 15, 2023
  3. mattbishop commented on Oct 29, 2023

    @mattbishop
    Author

    I test this issue with every release, and it is still present in v3.4.2

  4. micalevisk commented on Dec 5, 2025

    @micalevisk

    @mattbishop does v3.4.7 fixed it?

  5. mattbishop commented on Dec 5, 2025

    @mattbishop
    Author

    So this issue appears to be resolved now with 3.4.7. Thanks for the reminder to check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions