Home Explore Blog CI



postgresql

15th chunk of `doc/src/sgml/libpq.sgml`
84a91bb8be817b041c602152da4ecb76b9a0e821fcdad0530000000100000fa5
 hierarchical part of
    the <acronym>URI</acronym> can alternatively be given as named
    parameters.  For example:
<programlisting>
postgresql:///mydb?host=localhost&amp;port=5433
</programlisting>
    All named parameters must match key words listed in
    <xref linkend="libpq-paramkeywords"/>, except that for compatibility
    with JDBC connection <acronym>URI</acronym>s, instances
    of <literal>ssl=true</literal> are translated into
    <literal>sslmode=require</literal>.
   </para>

   <para>
    The connection <acronym>URI</acronym> needs to be encoded with <ulink
    url="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1">percent-encoding</ulink>
    if it includes symbols with special meaning in any of its parts.  Here is
    an example where the equal sign (<literal>=</literal>) is replaced with
    <literal>%3D</literal> and the space character with
    <literal>%20</literal>:
<programlisting>
postgresql://user@localhost:5433/mydb?options=-c%20synchronous_commit%3Doff
</programlisting>
   </para>

   <para>
    The host part may be either a host name or an IP address.  To specify an
    IPv6 address, enclose it in square brackets:
<synopsis>
postgresql://[2001:db8::1234]/database
</synopsis>
   </para>

   <para>
    The host part is interpreted as described for the parameter <xref
    linkend="libpq-connect-host"/>.  In particular, a Unix-domain socket
    connection is chosen if the host part is either empty or looks like an
    absolute path name,
    otherwise a TCP/IP connection is initiated.  Note, however, that the
    slash is a reserved character in the hierarchical part of the URI.  So, to
    specify a non-standard Unix-domain socket directory, either omit the host
    part of the URI and specify the host as a named parameter, or
    percent-encode the path in the host part of the URI:
<programlisting>
postgresql:///dbname?host=/var/lib/postgresql
postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
</programlisting>
   </para>

   <para>
    It is possible to specify multiple host components, each with an optional
    port component, in a single URI.  A URI of the form
    <literal>postgresql://host1:port1,host2:port2,host3:port3/</literal>
    is equivalent to a connection string of the form
    <literal>host=host1,host2,host3 port=port1,port2,port3</literal>.
    As further described below, each
    host will be tried in turn until a connection is successfully established.
   </para>
   </sect3>

   <sect3 id="libpq-multiple-hosts">
     <title>Specifying Multiple Hosts</title>

     <para>
       It is possible to specify multiple hosts to connect to, so that they are
       tried in the given order. In the Keyword/Value format, the <literal>host</literal>,
       <literal>hostaddr</literal>, and <literal>port</literal> options accept comma-separated
       lists of values. The same number of elements must be given in each
       option that is specified, such
       that e.g., the first <literal>hostaddr</literal> corresponds to the first host name,
       the second <literal>hostaddr</literal> corresponds to the second host name, and so
       forth. As an exception, if only one <literal>port</literal> is specified, it
       applies to all the hosts.
     </para>

     <para>
       In the connection URI format, you can list multiple <literal>host:port</literal> pairs
       separated by commas in the <literal>host</literal> component of the URI.
     </para>

     <para>
       In either format, a single host name can translate to multiple network
       addresses. A common example of this is a host that has both an IPv4 and
       an IPv6 address.
     </para>

     <para>
       When multiple hosts are specified, or when a single host name is
       translated to multiple addresses,  all the hosts and addresses will be
       tried in order, until one succeeds. If none of the hosts can be reached,
       the connection fails. If a connection is established successfully, but
       authentication

Title: Connection URI Details: Named Parameters, Encoding, and Multiple Hosts
Summary
This section describes how to use named parameters in connection URIs, including JDBC compatibility for SSL settings. It explains the importance of percent-encoding special characters within the URI and illustrates how to specify IPv6 addresses and Unix-domain socket directories. It also details how to specify multiple hosts in a single URI, which allows libpq to attempt connections to each host in order until a successful connection is established.