Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jRKm8-00069t-J9 for pgsql-general@arkaria.postgresql.org; Wed, 22 Apr 2020 19:11:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jRKm7-0007gY-FF for pgsql-general@arkaria.postgresql.org; Wed, 22 Apr 2020 19:11:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jRKm7-0007gO-7a for pgsql-general@lists.postgresql.org; Wed, 22 Apr 2020 19:11:11 +0000 Received: from [87.230.15.127] (helo=lvps87-230-15-127.dedicated.hosteurope.de) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jRKm4-0003MI-MS for pgsql-general@postgresql.org; Wed, 22 Apr 2020 19:11:10 +0000 Received: from snob.fritz.box (174.54.150.83.ftth.ewwwb.as8758.net [83.150.54.174]) by lvps87-230-15-127.dedicated.hosteurope.de (Postfix) with ESMTPSA id DBEC21D670A for ; Wed, 22 Apr 2020 21:10:36 +0200 (CEST) To: pgsql-general@postgresql.org From: Christian Ramseyer Subject: Can I tell libpq to connect to the primary? Message-ID: Date: Wed, 22 Apr 2020 21:10:35 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 87.230.15.127 (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi I'm setting up a new environment with a primary/hot standby replication pair. For read-only clients, I have found the host=host1,host2 connection string[1] which allows a connection when any one of the servers is up. However I'm unsure how to achieve something similar for read/write clients. If I'm providing this list, the client will connect to the first host, which might have become the standby in the meantime. I see that in pgjdbc there are additional options for targetServerType = any, primary, secondary, preferSlave and preferSecondary[2]. However this seems to be java-specific and not implemented in libpq? Is there a way to get this behaviour in PHP/Psycopg/Perl DBI? If not, what is the best alternative to achieve this? My primary & standby are not in the same L3 network, so moving around and IP address with Linux OS clustering is not an option. I'm tending to scripting the REST API of our DNS service to point a CNAME at the new primary during promotion, but maybe there is an easier way I haven't found yet? Maybe something in pgpool/pgbouncer etc? Cheers Christian [1] https://www.postgresql.org/docs/current/libpq-connect.html [2] https://jdbc.postgresql.org/documentation/head/connect.html