I have 2 db hosts: host1 and host2.
I connect to db using multi host connection string: postgresql://host1:port1,host2:port2/
. According to libpq documentation, connection is established with first successful host in the list given. When both hosts are running fine, connection with host1 is established and all queries are served from host1. When host1 becomes down, connection with host2 is established and queries are served from host2. Now, when host1 is recovered and running back again, I want to the connection to be established with host1 again and I want queries to be served from host1 again. But it doesn't happen.
I am using golang's pgx
library as driver which supports multi-host connection string feature. Is there mechanism which checks if host1 is up or not and establish connection with it once it is up?
Edit: more context
host1 is read-only replica db and host2 is read-write primary db. I have this multi-connection string setup only for read queries. I want those read queries to be served primarily by read-only db. So I want to switch connection back to host1.