Postgresql – Set connection timeout using psql command line interface

postgresqlpsql

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.

Best Answer

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