Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7b02ba6

Browse files
committed
Allow multiple hostaddrs to go with multiple hostnames.
Also fix two other issues, while we're at it: * In error message on connection failure, if multiple network addresses were given as the host option, as in "host=127.0.0.1,127.0.0.2", the error message printed the address twice. * If there were many more ports than hostnames, the error message would always claim that there was one port too many, even if there was more than one. For example, if you gave 2 hostnames and 5 ports, the error message claimed that you gave 2 hostnames and 3 ports. Discussion: https://www.postgresql.org/message-id/10badbc6-4d5a-a769-623a-f7ada43e14dd@iki.fi
1 parent 260ba85 commit 7b02ba6

File tree

3 files changed

+219
-97
lines changed

3 files changed

+219
-97
lines changed

doc/src/sgml/libpq.sgml

+51-4
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,42 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
887887
host will be tried in turn until a connection is successfully established.
888888
</para>
889889
</sect3>
890+
891+
<sect3 id="libpq-multiple-hosts">
892+
<title>Specifying Multiple Hosts</title>
893+
894+
<para>
895+
It is possible to specify multiple hosts to connect to, so that they are
896+
tried in the given order. In the Keyword/Value format, the <literal>host</>,
897+
<literal>hostaddr</>, and <literal>port</> options accept a comma-separated
898+
list of values. The same number of elements must be given in each option, such
899+
that e.g. the first <literal>hostaddr</> corresponds to the first host name,
900+
the second <literal>hostaddr</> corresponds to the second host name, and so
901+
forth. As an exception, if only one <literal>port</literal> is specified, it
902+
applies to all the hosts.
903+
</para>
904+
905+
<para>
906+
In the connection URI format, you can list multiple <literal>host:port</> pairs
907+
separated by commas, in the <literal>host</> component of the URI. In either
908+
format, a single hostname can also translate to multiple network addresses. A
909+
common example of this is a host that has both an IPv4 and an IPv6 address.
910+
</para>
911+
912+
<para>
913+
When multiple hosts are specified, or when a single hostname is
914+
translated to multiple addresses, all the hosts and addresses will be
915+
tried in order, until one succeeds. If none of the hosts can be reached,
916+
the connection fails. If a connection is established successfully, but
917+
authentication fails, the remaining hosts in the list are not tried.
918+
</para>
919+
920+
<para>
921+
If a password file is used, you can have different passwords for
922+
different hosts. All the other connection options are the same for every
923+
host, it is not possible to e.g. specify a different username for
924+
different hosts.
925+
</para>
890926
</sect2>
891927

892928
<sect2 id="libpq-paramkeywords">
@@ -900,7 +936,7 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
900936
<term><literal>host</literal></term>
901937
<listitem>
902938
<para>
903-
Comma-separated list of host names.<indexterm><primary>host name</></>
939+
Name of host to connect to.<indexterm><primary>host name</></>
904940
If a host name begins with a slash, it specifies Unix-domain
905941
communication rather than TCP/IP communication; the value is the
906942
name of the directory in which the socket file is stored. If
@@ -912,6 +948,11 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
912948
when <productname>PostgreSQL</> was built). On machines without
913949
Unix-domain sockets, the default is to connect to <literal>localhost</>.
914950
</para>
951+
<para>
952+
A comma-separated list of host names is also accepted, in which case
953+
each host name in the list is tried in order. See
954+
<xref linkend="libpq-multiple-hosts"> for details.
955+
</para>
915956
</listitem>
916957
</varlistentry>
917958

@@ -965,6 +1006,11 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
9651006
<xref linkend="libpq-pgpass">).
9661007
</para>
9671008

1009+
<para>
1010+
A comma-separated list of <literal>hostaddrs</> is also accepted, in
1011+
which case each host in the list is tried in order. See
1012+
<xref linkend="libpq-multiple-hosts"> for details.
1013+
</para>
9681014
<para>
9691015
Without either a host name or host address,
9701016
<application>libpq</application> will connect using a
@@ -981,9 +1027,10 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
9811027
Port number to connect to at the server host, or socket file
9821028
name extension for Unix-domain
9831029
connections.<indexterm><primary>port</></>
984-
If the <literal>host</> parameter included multiple, comma-separated
985-
hosts, this parameter may specify a list of ports of equal length,
986-
or it may specify a single port number to be used for all hosts.
1030+
If multiple hosts were given in the <literal>host</literal> or
1031+
<literal>hostaddr</> parameters, this parameter may specify a list
1032+
of ports of equal length, or it may specify a single port number to
1033+
be used for all hosts.
9871034
</para>
9881035
</listitem>
9891036
</varlistentry>

0 commit comments

Comments
 (0)