Postgresql – .psqlrc – setting keepalives_idle connection parameter

postgresqlpsql

Is it possible to set keepalives_idle parameter in .psqlrc so that every connection I open uses it?

I've finally found this option and it's a remedy for my connection dropping problems.

I would really like to set it globally for all sessions I open with psql.

Best Answer

With TCP Keepalives, it doesn't matter who sends it and who receives it. It just matters that it happened so the route isn't lost by a NAT device or closed at either end. I don't know why you'd ever want to set this on the client, as libpq offers, both methods accomplish the same thing.

How to set keepalive transmissions from the server

This only sets it for your session

To set the tcp_keepalives_idle from the server to the client, use

SET tcp_keepalives_idle = 42;

You can put anything you want in your ~/.psqlrc and it gets executed by psql (except under -c and -x). So append this command to the end if you want it to occur for ever psql session.

echo "SET tcp_keepalives_idle = 42;" >> ~/.psqlrc

How do set keepalive transmissions from the client

According to this mailing list post, you can set it on the client by typing

psql "host=127.0.0.1 dbname=myDB keepalives=1"