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.

Set a timeout on pg.connect

See original GitHub issue

Hi, Thanks for this library, it’s great.

Currently if I call pg.connect but my pool is full, node-postgres will block until a connection becomes available. Is there a way to set a timeout on a connection attempt? I would rather not block indefinitely if the pool is full.

Something like pg.connect({timeout: 500}, function(err) { console.log(err); }

I can wrap it in a setTimeout, but do I need to do cleanup/is there a way to cancel a connection attempt?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
charmandercommented, Aug 6, 2017

Connection timeouts have been implemented as connectionTimeoutMillis; for applying that to pool size limits, see #1390.

0reactions
davepachecocommented, Jul 22, 2016

We’ve run into a similar problem with a command-line tool that queries several different PostgreSQL instances. When we attempt to query an instance that whose server is down, or which is behind a network partition, the program ends up hanging for a long time because the client is still trying to connect to the down server. The CLI tool actually handles this by applying its own timeout and reporting the problem. The problem is that there’s no way for the program to cancel the connection (i.e., close the TCP socket), and as a result, the Node program keeps running until the connection ultimately fails. (I know we can call process.exit(), but this would just be papering over the problem.)

If the Client class provided a close() method that just closed the underlying socket (and stopped emitting other events), that would allow callers to implement their own connection timeout.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a timeout for idle PostgreSQL connections?
There is a timeout on broken connections (i.e. due to network errors), which relies on the OS' TCP ...
Read more >
How to set connection timeout on PostgreSQL ...
You can set the connection timeout using the PDO::ATTR_TIMEOUT options: $dbh = new PDO($dsn, $username, $password, array(PDO::ATTR_TIMEOUT => $ ...
Read more >
Documentation: 15: 20.11. Client Connection Defaults
The timeout is measured from the time a command arrives at the server until it is completed by the server. If multiple SQL...
Read more >
Connecting to PostgreSQL - Data Sources (Connectors)
The default connection timeout limit to PostgreSQL data sources is 30 seconds. You can modify this limit by editing the Sisense.Connectors.
Read more >
Set connection timeout using psql command line interface
Assuming psql uses libpq , I think the environment variable PGCONNECT_TIMEOUT might work for setting connect timeout.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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