Score:2

Set connection timeout using psql command line interface

mx flag

Is there a way to set the connection timeout using psql cli? How can I set a connection timeout to something low like 3 seconds using the following:

PGPASSWORD=passwordhere psql -h 10.0.0.144 -U myuser -c "select 1" -d mydatabase

As far as I can tell, psql does not support a connection timeout parameter. In the example above 10.0.0.144 is invalid and this command hangs for a long time. I need the command to fail fast if the host is incorrect.

Score:2
us flag

Assuming psql uses libpq, I think the environment variable PGCONNECT_TIMEOUT might work for setting connect timeout.

You can check the libpq chapter of the official documentation for more details (do not forget to select the version you are using):

https://www.postgresql.org/docs/current/libpq-envars.html

Tom Harrison Jr avatar
gu flag
Confirmed: `PGCONNECT_TIMEOUT` works
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.