]> granicus.if.org Git - postgresql/blob - doc/src/sgml/client-auth.sgml
Trim trailing whitespace
[postgresql] / doc / src / sgml / client-auth.sgml
1 <!-- doc/src/sgml/client-auth.sgml -->
2
3 <chapter id="client-authentication">
4  <title>Client Authentication</title>
5
6  <indexterm zone="client-authentication">
7   <primary>client authentication</primary>
8  </indexterm>
9
10  <para>
11   When a client application connects to the database server, it
12   specifies which <productname>PostgreSQL</productname> database user name it
13   wants to connect as, much the same way one logs into a Unix computer
14   as a particular user. Within the SQL environment the active database
15   user name determines access privileges to database objects &mdash; see
16   <xref linkend="user-manag"> for more information. Therefore, it is
17   essential to restrict which database users can connect.
18  </para>
19
20  <note>
21   <para>
22    As explained in <xref linkend="user-manag">,
23    <productname>PostgreSQL</productname> actually does privilege
24    management in terms of <quote>roles</>.  In this chapter, we
25    consistently use <firstterm>database user</> to mean <quote>role with the
26    <literal>LOGIN</> privilege</quote>.
27   </para>
28  </note>
29
30  <para>
31   <firstterm>Authentication</firstterm> is the process by which the
32   database server establishes the identity of the client, and by
33   extension determines whether the client application (or the user
34   who runs the client application) is permitted to connect with the
35   database user name that was requested.
36  </para>
37
38  <para>
39   <productname>PostgreSQL</productname> offers a number of different
40   client authentication methods. The method used to authenticate a
41   particular client connection can be selected on the basis of
42   (client) host address, database, and user.
43  </para>
44
45  <para>
46   <productname>PostgreSQL</productname> database user names are logically
47   separate from user names of the operating system in which the server
48   runs. If all the users of a particular server also have accounts on
49   the server's machine, it makes sense to assign database user names
50   that match their operating system user names. However, a server that
51   accepts remote connections might have many database users who have no local
52   operating system
53   account, and in such cases there need be no connection between
54   database user names and OS user names.
55  </para>
56
57  <sect1 id="auth-pg-hba-conf">
58   <title>The <filename>pg_hba.conf</filename> File</title>
59
60   <indexterm zone="auth-pg-hba-conf">
61    <primary>pg_hba.conf</primary>
62   </indexterm>
63
64   <para>
65    Client authentication is controlled by a configuration file,
66    which traditionally is named
67    <filename>pg_hba.conf</filename> and is stored in the database
68    cluster's data directory.
69    (<acronym>HBA</> stands for host-based authentication.) A default
70    <filename>pg_hba.conf</filename> file is installed when the data
71    directory is initialized by <command>initdb</command>.  It is
72    possible to place the authentication configuration file elsewhere,
73    however; see the <xref linkend="guc-hba-file"> configuration parameter.
74   </para>
75
76   <para>
77    The general format of the <filename>pg_hba.conf</filename> file is
78    a set of records, one per line. Blank lines are ignored, as is any
79    text after the <literal>#</literal> comment character.
80    Records cannot be continued across lines.
81    A record is made
82    up of a number of fields which are separated by spaces and/or tabs.
83    Fields can contain white space if the field value is double-quoted.
84    Quoting one of the keywords in a database, user, or address field (e.g.,
85    <literal>all</> or <literal>replication</>) makes the word lose its special
86    meaning, and just match a database, user, or host with that name.
87   </para>
88
89   <para>
90    Each record specifies a connection type, a client IP address range
91    (if relevant for the connection type), a database name, a user name,
92    and the authentication method to be used for connections matching
93    these parameters. The first record with a matching connection type,
94    client address, requested database, and user name is used to perform
95    authentication. There is no <quote>fall-through</> or
96    <quote>backup</>: if one record is chosen and the authentication
97    fails, subsequent records are not considered. If no record matches,
98    access is denied.
99   </para>
100
101   <para>
102    A record can have one of the seven formats
103 <synopsis>
104 local      <replaceable>database</replaceable>  <replaceable>user</replaceable>  <replaceable>auth-method</replaceable>  <optional><replaceable>auth-options</replaceable></optional>
105 host       <replaceable>database</replaceable>  <replaceable>user</replaceable>  <replaceable>address</replaceable>  <replaceable>auth-method</replaceable>  <optional><replaceable>auth-options</replaceable></optional>
106 hostssl    <replaceable>database</replaceable>  <replaceable>user</replaceable>  <replaceable>address</replaceable>  <replaceable>auth-method</replaceable>  <optional><replaceable>auth-options</replaceable></optional>
107 hostnossl  <replaceable>database</replaceable>  <replaceable>user</replaceable>  <replaceable>address</replaceable>  <replaceable>auth-method</replaceable>  <optional><replaceable>auth-options</replaceable></optional>
108 host       <replaceable>database</replaceable>  <replaceable>user</replaceable>  <replaceable>IP-address</replaceable>  <replaceable>IP-mask</replaceable>  <replaceable>auth-method</replaceable>  <optional><replaceable>auth-options</replaceable></optional>
109 hostssl    <replaceable>database</replaceable>  <replaceable>user</replaceable>  <replaceable>IP-address</replaceable>  <replaceable>IP-mask</replaceable>  <replaceable>auth-method</replaceable>  <optional><replaceable>auth-options</replaceable></optional>
110 hostnossl  <replaceable>database</replaceable>  <replaceable>user</replaceable>  <replaceable>IP-address</replaceable>  <replaceable>IP-mask</replaceable>  <replaceable>auth-method</replaceable>  <optional><replaceable>auth-options</replaceable></optional>
111 </synopsis>
112    The meaning of the fields is as follows:
113
114    <variablelist>
115     <varlistentry>
116      <term><literal>local</literal></term>
117      <listitem>
118       <para>
119        This record matches connection attempts using Unix-domain
120        sockets.  Without a record of this type, Unix-domain socket
121        connections are disallowed.
122       </para>
123      </listitem>
124     </varlistentry>
125
126     <varlistentry>
127      <term><literal>host</literal></term>
128      <listitem>
129       <para>
130        This record matches connection attempts made using TCP/IP.
131        <literal>host</literal> records match either
132        <acronym>SSL</acronym> or non-<acronym>SSL</acronym> connection
133        attempts.
134       </para>
135      <note>
136       <para>
137        Remote TCP/IP connections will not be possible unless
138        the server is started with an appropriate value for the
139        <xref linkend="guc-listen-addresses"> configuration parameter,
140        since the default behavior is to listen for TCP/IP connections
141        only on the local loopback address <literal>localhost</>.
142       </para>
143      </note>
144      </listitem>
145     </varlistentry>
146
147     <varlistentry>
148      <term><literal>hostssl</literal></term>
149      <listitem>
150       <para>
151        This record matches connection attempts made using TCP/IP,
152        but only when the connection is made with <acronym>SSL</acronym>
153        encryption.
154       </para>
155
156       <para>
157        To make use of this option the server must be built with
158        <acronym>SSL</acronym> support. Furthermore,
159        <acronym>SSL</acronym> must be enabled
160        by setting the <xref linkend="guc-ssl"> configuration parameter (see
161        <xref linkend="ssl-tcp"> for more information).
162        Otherwise, the <literal>hostssl</literal> record is ignored except for
163        logging a warning that it cannot match any connections.
164       </para>
165      </listitem>
166     </varlistentry>
167
168     <varlistentry>
169      <term><literal>hostnossl</literal></term>
170      <listitem>
171       <para>
172        This record type has the opposite behavior of <literal>hostssl</>;
173        it only matches connection attempts made over
174        TCP/IP that do not use <acronym>SSL</acronym>.
175       </para>
176      </listitem>
177     </varlistentry>
178
179     <varlistentry>
180      <term><replaceable>database</replaceable></term>
181      <listitem>
182       <para>
183        Specifies which database name(s) this record matches.  The value
184        <literal>all</literal> specifies that it matches all databases.
185        The value <literal>sameuser</> specifies that the record
186        matches if the requested database has the same name as the
187        requested user.  The value <literal>samerole</> specifies that
188        the requested user must be a member of the role with the same
189        name as the requested database.  (<literal>samegroup</> is an
190        obsolete but still accepted spelling of <literal>samerole</>.)
191        Superusers are not considered to be members of a role for the
192        purposes of <literal>samerole</> unless they are explicitly
193        members of the role, directly or indirectly, and not just by
194        virtue of being a superuser.
195        The value <literal>replication</> specifies that the record
196        matches if a physical replication connection is requested (note that
197        replication connections do not specify any particular database).
198        Otherwise, this is the name of
199        a specific <productname>PostgreSQL</productname> database.
200        Multiple database names can be supplied by separating them with
201        commas.  A separate file containing database names can be specified by
202        preceding the file name with <literal>@</>.
203       </para>
204      </listitem>
205     </varlistentry>
206
207     <varlistentry>
208      <term><replaceable>user</replaceable></term>
209      <listitem>
210       <para>
211        Specifies which database user name(s) this record
212        matches. The value <literal>all</literal> specifies that it
213        matches all users.  Otherwise, this is either the name of a specific
214        database user, or a group name preceded by <literal>+</>.
215        (Recall that there is no real distinction between users and groups
216        in <productname>PostgreSQL</>; a <literal>+</> mark really means
217        <quote>match any of the roles that are directly or indirectly members
218        of this role</>, while a name without a <literal>+</> mark matches
219        only that specific role.) For this purpose, a superuser is only
220        considered to be a member of a role if they are explicitly a member
221        of the role, directly or indirectly, and not just by virtue of
222        being a superuser.
223        Multiple user names can be supplied by separating them with commas.
224        A separate file containing user names can be specified by preceding the
225        file name with <literal>@</>.
226       </para>
227      </listitem>
228     </varlistentry>
229
230     <varlistentry>
231      <term><replaceable>address</replaceable></term>
232      <listitem>
233       <para>
234        Specifies the client machine address(es) that this record
235        matches.  This field can contain either a host name, an IP
236        address range, or one of the special key words mentioned below.
237       </para>
238
239       <para>
240        An IP address range is specified using standard numeric notation
241        for the range's starting address, then a slash (<literal>/</literal>)
242        and a <acronym>CIDR</> mask length.  The mask
243        length indicates the number of high-order bits of the client
244        IP address that must match.  Bits to the right of this should
245        be zero in the given IP address.
246        There must not be any white space between the IP address, the
247        <literal>/</literal>, and the CIDR mask length.
248       </para>
249
250       <para>
251        Typical examples of an IPv4 address range specified this way are
252        <literal>172.20.143.89/32</literal> for a single host, or
253        <literal>172.20.143.0/24</literal> for a small network, or
254        <literal>10.6.0.0/16</literal> for a larger one.
255        An IPv6 address range might look like <literal>::1/128</literal>
256        for a single host (in this case the IPv6 loopback address) or
257        <literal>fe80::7a31:c1ff:0000:0000/96</literal> for a small
258        network.
259        <literal>0.0.0.0/0</literal> represents all
260        IPv4 addresses, and <literal>::0/0</literal> represents
261        all IPv6 addresses.
262        To specify a single host, use a mask length of 32 for IPv4 or
263        128 for IPv6.  In a network address, do not omit trailing zeroes.
264       </para>
265
266       <para>
267        An entry given in IPv4 format will match only IPv4 connections,
268        and an entry given in IPv6 format will match only IPv6 connections,
269        even if the represented address is in the IPv4-in-IPv6 range.
270        Note that entries in IPv6 format will be rejected if the system's
271        C library does not have support for IPv6 addresses.
272       </para>
273
274       <para>
275        You can also write <literal>all</literal> to match any IP address,
276        <literal>samehost</literal> to match any of the server's own IP
277        addresses, or <literal>samenet</literal> to match any address in any
278        subnet that the server is directly connected to.
279       </para>
280
281       <para>
282        If a host name is specified (anything that is not an IP address
283        range or a special key word is treated as a host name),
284        that name is compared with the result of a reverse name
285        resolution of the client's IP address (e.g., reverse DNS
286        lookup, if DNS is used).  Host name comparisons are case
287        insensitive.  If there is a match, then a forward name
288        resolution (e.g., forward DNS lookup) is performed on the host
289        name to check whether any of the addresses it resolves to are
290        equal to the client's IP address.  If both directions match,
291        then the entry is considered to match.  (The host name that is
292        used in <filename>pg_hba.conf</filename> should be the one that
293        address-to-name resolution of the client's IP address returns,
294        otherwise the line won't be matched.  Some host name databases
295        allow associating an IP address with multiple host names, but
296        the operating system will only return one host name when asked
297        to resolve an IP address.)
298       </para>
299
300       <para>
301        A host name specification that starts with a dot
302        (<literal>.</literal>) matches a suffix of the actual host
303        name.  So <literal>.example.com</literal> would match
304        <literal>foo.example.com</literal> (but not just
305        <literal>example.com</literal>).
306       </para>
307
308       <para>
309        When host names are specified
310        in <filename>pg_hba.conf</filename>, you should make sure that
311        name resolution is reasonably fast.  It can be of advantage to
312        set up a local name resolution cache such
313        as <command>nscd</command>.  Also, you may wish to enable the
314        configuration parameter <varname>log_hostname</varname> to see
315        the client's host name instead of the IP address in the log.
316       </para>
317
318       <para>
319        This field only applies to <literal>host</literal>,
320        <literal>hostssl</literal>, and <literal>hostnossl</> records.
321       </para>
322
323       <sidebar>
324        <para>
325         Users sometimes wonder why host names are handled
326         in this seemingly complicated way, with two name resolutions
327         including a reverse lookup of the client's IP address.  This
328         complicates use of the feature in case the client's reverse DNS
329         entry is not set up or yields some undesirable host name.
330         It is done primarily for efficiency: this way, a connection attempt
331         requires at most two resolver lookups, one reverse and one forward.
332         If there is a resolver problem with some address, it becomes only
333         that client's problem.  A hypothetical alternative
334         implementation that only did forward lookups would have to
335         resolve every host name mentioned in
336         <filename>pg_hba.conf</filename> during every connection attempt.
337         That could be quite slow if many names are listed.
338         And if there is a resolver problem with one of the host names,
339         it becomes everyone's problem.
340        </para>
341
342        <para>
343         Also, a reverse lookup is necessary to implement the suffix
344         matching feature, because the actual client host name needs to
345         be known in order to match it against the pattern.
346        </para>
347
348        <para>
349         Note that this behavior is consistent with other popular
350         implementations of host name-based access control, such as the
351         Apache HTTP Server and TCP Wrappers.
352        </para>
353       </sidebar>
354      </listitem>
355     </varlistentry>
356
357     <varlistentry>
358      <term><replaceable>IP-address</replaceable></term>
359      <term><replaceable>IP-mask</replaceable></term>
360      <listitem>
361       <para>
362        These two fields can be used as an alternative to the
363        <replaceable>IP-address</><literal>/</><replaceable>mask-length</>
364        notation.  Instead of
365        specifying the mask length, the actual mask is specified in a
366        separate column. For example, <literal>255.0.0.0</> represents an IPv4
367        CIDR mask length of 8, and <literal>255.255.255.255</> represents a
368        CIDR mask length of 32.
369       </para>
370
371       <para>
372        These fields only apply to <literal>host</literal>,
373        <literal>hostssl</literal>, and <literal>hostnossl</> records.
374       </para>
375      </listitem>
376     </varlistentry>
377
378     <varlistentry>
379      <term><replaceable>auth-method</replaceable></term>
380      <listitem>
381       <para>
382        Specifies the authentication method to use when a connection matches
383        this record. The possible choices are summarized here; details
384        are in <xref linkend="auth-methods">.
385
386        <variablelist>
387         <varlistentry>
388          <term><literal>trust</></term>
389          <listitem>
390          <para>
391           Allow the connection unconditionally. This method
392           allows anyone that can connect to the
393           <productname>PostgreSQL</productname> database server to login as
394           any <productname>PostgreSQL</productname> user they wish,
395           without the need for a password or any other authentication.  See <xref
396           linkend="auth-trust"> for details.
397          </para>
398         </listitem>
399        </varlistentry>
400
401        <varlistentry>
402         <term><literal>reject</></term>
403         <listitem>
404          <para>
405           Reject the connection unconditionally. This is useful for
406           <quote>filtering out</> certain hosts from a group, for example a
407           <literal>reject</> line could block a specific host from connecting,
408           while a later line allows the remaining hosts in a specific
409           network to connect.
410          </para>
411         </listitem>
412        </varlistentry>
413
414        <varlistentry>
415         <term><literal>scram-sha-256</></term>
416         <listitem>
417          <para>
418           Perform SCRAM-SHA-256 authentication to verify the user's
419           password. See <xref linkend="auth-password"> for details.
420          </para>
421         </listitem>
422        </varlistentry>
423
424        <varlistentry>
425         <term><literal>md5</></term>
426         <listitem>
427          <para>
428           Perform SCRAM-SHA-256 or MD5 authentication to verify the
429           user's password. See <xref linkend="auth-password">
430           for details.
431          </para>
432         </listitem>
433        </varlistentry>
434
435        <varlistentry>
436         <term><literal>password</></term>
437         <listitem>
438          <para>
439           Require the client to supply an unencrypted password for
440           authentication.
441           Since the password is sent in clear text over the
442           network, this should not be used on untrusted networks.
443           See <xref linkend="auth-password"> for details.
444          </para>
445         </listitem>
446        </varlistentry>
447
448        <varlistentry>
449         <term><literal>gss</></term>
450         <listitem>
451          <para>
452           Use GSSAPI to authenticate the user. This is only
453           available for TCP/IP connections. See <xref
454           linkend="gssapi-auth"> for details.
455          </para>
456         </listitem>
457        </varlistentry>
458
459        <varlistentry>
460         <term><literal>sspi</></term>
461         <listitem>
462          <para>
463           Use SSPI to authenticate the user. This is only
464           available on Windows. See <xref
465           linkend="sspi-auth"> for details.
466          </para>
467         </listitem>
468        </varlistentry>
469
470        <varlistentry>
471         <term><literal>ident</></term>
472         <listitem>
473          <para>
474           Obtain the operating system user name of the client
475           by contacting the ident server on the client
476           and check if it matches the requested database user name.
477           Ident authentication can only be used on TCP/IP
478           connections. When specified for local connections, peer
479           authentication will be used instead.
480           See <xref linkend="auth-ident"> for details.
481          </para>
482         </listitem>
483        </varlistentry>
484
485        <varlistentry>
486         <term><literal>peer</></term>
487         <listitem>
488          <para>
489           Obtain the client's operating system user name from the operating
490           system and check if it matches the requested database user name.
491           This is only available for local connections.
492           See <xref linkend="auth-peer"> for details.
493          </para>
494         </listitem>
495        </varlistentry>
496
497        <varlistentry>
498         <term><literal>ldap</></term>
499         <listitem>
500          <para>
501           Authenticate using an <acronym>LDAP</> server. See <xref
502           linkend="auth-ldap"> for details.
503          </para>
504         </listitem>
505        </varlistentry>
506
507        <varlistentry>
508         <term><literal>radius</></term>
509         <listitem>
510          <para>
511           Authenticate using a RADIUS server. See <xref
512           linkend="auth-radius"> for details.
513          </para>
514         </listitem>
515        </varlistentry>
516
517        <varlistentry>
518         <term><literal>cert</></term>
519         <listitem>
520          <para>
521           Authenticate using SSL client certificates. See
522           <xref linkend="auth-cert"> for details.
523          </para>
524         </listitem>
525        </varlistentry>
526
527        <varlistentry>
528         <term><literal>pam</></term>
529         <listitem>
530          <para>
531           Authenticate using the Pluggable Authentication Modules
532           (PAM) service provided by the operating system.  See <xref
533           linkend="auth-pam"> for details.
534          </para>
535         </listitem>
536        </varlistentry>
537
538        <varlistentry>
539         <term><literal>bsd</></term>
540         <listitem>
541          <para>
542           Authenticate using the BSD Authentication service provided by the
543           operating system. See <xref linkend="auth-bsd"> for details.
544          </para>
545         </listitem>
546        </varlistentry>
547       </variablelist>
548
549       </para>
550      </listitem>
551     </varlistentry>
552
553     <varlistentry>
554      <term><replaceable>auth-options</replaceable></term>
555      <listitem>
556       <para>
557        After the <replaceable>auth-method</> field, there can be field(s) of
558        the form <replaceable>name</><literal>=</><replaceable>value</> that
559        specify options for the authentication method. Details about which
560        options are available for which authentication methods appear below.
561       </para>
562
563       <para>
564        In addition to the method-specific options listed below, there is one
565        method-independent authentication option <literal>clientcert</>, which
566        can be specified in any <literal>hostssl</> record.  When set
567        to <literal>1</>, this option requires the client to present a valid
568        (trusted) SSL certificate, in addition to the other requirements of the
569        authentication method.
570       </para>
571      </listitem>
572     </varlistentry>
573    </variablelist>
574   </para>
575
576   <para>
577    Files included by <literal>@</> constructs are read as lists of names,
578    which can be separated by either whitespace or commas.  Comments are
579    introduced by <literal>#</literal>, just as in
580    <filename>pg_hba.conf</filename>, and nested <literal>@</> constructs are
581    allowed.  Unless the file name following <literal>@</> is an absolute
582    path, it is taken to be relative to the directory containing the
583    referencing file.
584   </para>
585
586   <para>
587    Since the <filename>pg_hba.conf</filename> records are examined
588    sequentially for each connection attempt, the order of the records is
589    significant. Typically, earlier records will have tight connection
590    match parameters and weaker authentication methods, while later
591    records will have looser match parameters and stronger authentication
592    methods. For example, one might wish to use <literal>trust</>
593    authentication for local TCP/IP connections but require a password for
594    remote TCP/IP connections. In this case a record specifying
595    <literal>trust</> authentication for connections from 127.0.0.1 would
596    appear before a record specifying password authentication for a wider
597    range of allowed client IP addresses.
598   </para>
599
600   <para>
601    The <filename>pg_hba.conf</filename> file is read on start-up and when
602    the main server process receives a
603    <systemitem>SIGHUP</systemitem><indexterm><primary>SIGHUP</primary></indexterm>
604    signal. If you edit the file on an
605    active system, you will need to signal the postmaster
606    (using <literal>pg_ctl reload</> or <literal>kill -HUP</>) to make it
607    re-read the file.
608   </para>
609
610   <note>
611    <para>
612     The preceding statement is not true on Microsoft Windows: there, any
613     changes in the <filename>pg_hba.conf</filename> file are immediately
614     applied by subsequent new connections.
615    </para>
616   </note>
617
618   <para>
619    The system view
620    <link linkend="view-pg-hba-file-rules"><structname>pg_hba_file_rules</structname></link>
621    can be helpful for pre-testing changes to the <filename>pg_hba.conf</>
622    file, or for diagnosing problems if loading of the file did not have the
623    desired effects.  Rows in the view with
624    non-null <structfield>error</structfield> fields indicate problems in the
625    corresponding lines of the file.
626   </para>
627  
628   <tip>
629    <para>
630     To connect to a particular database, a user must not only pass the
631     <filename>pg_hba.conf</filename> checks, but must have the
632     <literal>CONNECT</> privilege for the database.  If you wish to
633     restrict which users can connect to which databases, it's usually
634     easier to control this by granting/revoking <literal>CONNECT</> privilege
635     than to put the rules in <filename>pg_hba.conf</filename> entries.
636    </para>
637   </tip>
638
639   <para>
640    Some examples of <filename>pg_hba.conf</filename> entries are shown in
641    <xref linkend="example-pg-hba.conf">. See the next section for details on the
642    different authentication methods.
643   </para>
644
645    <example id="example-pg-hba.conf">
646     <title>Example <filename>pg_hba.conf</filename> Entries</title>
647 <programlisting>
648 # Allow any user on the local system to connect to any database with
649 # any database user name using Unix-domain sockets (the default for local
650 # connections).
651 #
652 # TYPE  DATABASE        USER            ADDRESS                 METHOD
653 local   all             all                                     trust
654
655 # The same using local loopback TCP/IP connections.
656 #
657 # TYPE  DATABASE        USER            ADDRESS                 METHOD
658 host    all             all             127.0.0.1/32            trust
659
660 # The same as the previous line, but using a separate netmask column
661 #
662 # TYPE  DATABASE        USER            IP-ADDRESS      IP-MASK             METHOD
663 host    all             all             127.0.0.1       255.255.255.255     trust
664
665 # The same over IPv6.
666 #
667 # TYPE  DATABASE        USER            ADDRESS                 METHOD
668 host    all             all             ::1/128                 trust
669
670 # The same using a host name (would typically cover both IPv4 and IPv6).
671 #
672 # TYPE  DATABASE        USER            ADDRESS                 METHOD
673 host    all             all             localhost               trust
674
675 # Allow any user from any host with IP address 192.168.93.x to connect
676 # to database "postgres" as the same user name that ident reports for
677 # the connection (typically the operating system user name).
678 #
679 # TYPE  DATABASE        USER            ADDRESS                 METHOD
680 host    postgres        all             192.168.93.0/24         ident
681
682 # Allow any user from host 192.168.12.10 to connect to database
683 # "postgres" if the user's password is correctly supplied.
684 #
685 # TYPE  DATABASE        USER            ADDRESS                 METHOD
686 host    postgres        all             192.168.12.10/32        scram-sha-256
687
688 # Allow any user from hosts in the example.com domain to connect to
689 # any database if the user's password is correctly supplied.
690 #
691 # Require SCRAM authentication for most users, but make an exception
692 # for user 'mike', who uses an older client that doesn't support SCRAM
693 # authentication.
694 #
695 # TYPE  DATABASE        USER            ADDRESS                 METHOD
696 host    all             mike            .example.com            md5
697 host    all             all             .example.com            scram-sha-256
698
699 # In the absence of preceding "host" lines, these two lines will
700 # reject all connections from 192.168.54.1 (since that entry will be
701 # matched first), but allow GSSAPI connections from anywhere else
702 # on the Internet.  The zero mask causes no bits of the host IP
703 # address to be considered, so it matches any host.
704 #
705 # TYPE  DATABASE        USER            ADDRESS                 METHOD
706 host    all             all             192.168.54.1/32         reject
707 host    all             all             0.0.0.0/0               gss
708
709 # Allow users from 192.168.x.x hosts to connect to any database, if
710 # they pass the ident check.  If, for example, ident says the user is
711 # "bryanh" and he requests to connect as PostgreSQL user "guest1", the
712 # connection is allowed if there is an entry in pg_ident.conf for map
713 # "omicron" that says "bryanh" is allowed to connect as "guest1".
714 #
715 # TYPE  DATABASE        USER            ADDRESS                 METHOD
716 host    all             all             192.168.0.0/16          ident map=omicron
717
718 # If these are the only three lines for local connections, they will
719 # allow local users to connect only to their own databases (databases
720 # with the same name as their database user name) except for administrators
721 # and members of role "support", who can connect to all databases.  The file
722 # $PGDATA/admins contains a list of names of administrators.  Passwords
723 # are required in all cases.
724 #
725 # TYPE  DATABASE        USER            ADDRESS                 METHOD
726 local   sameuser        all                                     md5
727 local   all             @admins                                 md5
728 local   all             +support                                md5
729
730 # The last two lines above can be combined into a single line:
731 local   all             @admins,+support                        md5
732
733 # The database column can also use lists and file names:
734 local   db1,db2,@demodbs  all                                   md5
735 </programlisting>
736    </example>
737  </sect1>
738
739  <sect1 id="auth-username-maps">
740   <title>User Name Maps</title>
741
742   <indexterm zone="auth-username-maps">
743    <primary>User name maps</primary>
744   </indexterm>
745
746   <para>
747    When using an external authentication system such as Ident or GSSAPI,
748    the name of the operating system user that initiated the connection
749    might not be the same as the database user (role) that is to be used.
750    In this case, a user name map can be applied to map the operating system
751    user name to a database user.  To use user name mapping, specify
752    <literal>map</literal>=<replaceable>map-name</replaceable>
753    in the options field in <filename>pg_hba.conf</filename>. This option is
754    supported for all authentication methods that receive external user names.
755    Since different mappings might be needed for different connections,
756    the name of the map to be used is specified in the
757    <replaceable>map-name</replaceable> parameter in <filename>pg_hba.conf</filename>
758    to indicate which map to use for each individual connection.
759   </para>
760
761   <para>
762    User name maps are defined in the ident map file, which by default is named
763    <filename>pg_ident.conf</><indexterm><primary>pg_ident.conf</primary></indexterm>
764    and is stored in the
765    cluster's data directory.  (It is possible to place the map file
766    elsewhere, however; see the <xref linkend="guc-ident-file">
767    configuration parameter.)
768    The ident map file contains lines of the general form:
769 <synopsis>
770 <replaceable>map-name</> <replaceable>system-username</> <replaceable>database-username</>
771 </synopsis>
772    Comments and whitespace are handled in the same way as in
773    <filename>pg_hba.conf</>.  The
774    <replaceable>map-name</> is an arbitrary name that will be used to
775    refer to this mapping in <filename>pg_hba.conf</filename>. The other
776    two fields specify an operating system user name and a matching
777    database user name. The same <replaceable>map-name</> can be
778    used repeatedly to specify multiple user-mappings within a single map.
779   </para>
780   <para>
781    There is no restriction regarding how many database users a given
782    operating system user can correspond to, nor vice versa.  Thus, entries
783    in a map should be thought of as meaning <quote>this operating system
784    user is allowed to connect as this database user</quote>, rather than
785    implying that they are equivalent.  The connection will be allowed if
786    there is any map entry that pairs the user name obtained from the
787    external authentication system with the database user name that the
788    user has requested to connect as.
789   </para>
790   <para>
791    If the <replaceable>system-username</> field starts with a slash (<literal>/</>),
792    the remainder of the field is treated as a regular expression.
793    (See <xref linkend="posix-syntax-details"> for details of
794    <productname>PostgreSQL</>'s regular expression syntax.)  The regular
795    expression can include a single capture, or parenthesized subexpression,
796    which can then be referenced in the <replaceable>database-username</>
797    field as <literal>\1</> (backslash-one).  This allows the mapping of
798    multiple user names in a single line, which is particularly useful for
799    simple syntax substitutions.  For example, these entries
800 <programlisting>
801 mymap   /^(.*)@mydomain\.com$      \1
802 mymap   /^(.*)@otherdomain\.com$   guest
803 </programlisting>
804    will remove the domain part for users with system user names that end with
805    <literal>@mydomain.com</>, and allow any user whose system name ends with
806    <literal>@otherdomain.com</> to log in as <literal>guest</>.
807   </para>
808
809   <tip>
810    <para>
811     Keep in mind that by default, a regular expression can match just part of
812     a string.  It's usually wise to use <literal>^</> and <literal>$</>, as
813     shown in the above example, to force the match to be to the entire
814     system user name.
815    </para>
816   </tip>
817
818   <para>
819    The <filename>pg_ident.conf</filename> file is read on start-up and
820    when the main server process receives a
821    <systemitem>SIGHUP</systemitem><indexterm><primary>SIGHUP</primary></indexterm>
822    signal. If you edit the file on an
823    active system, you will need to signal the postmaster
824    (using <literal>pg_ctl reload</> or <literal>kill -HUP</>) to make it
825    re-read the file.
826   </para>
827
828   <para>
829    A <filename>pg_ident.conf</filename> file that could be used in
830    conjunction with the <filename>pg_hba.conf</> file in <xref
831    linkend="example-pg-hba.conf"> is shown in <xref
832    linkend="example-pg-ident.conf">. In this example, anyone
833    logged in to a machine on the 192.168 network that does not have the
834    operating system user name <literal>bryanh</>, <literal>ann</>, or
835    <literal>robert</> would not be granted access. Unix user
836    <literal>robert</> would only be allowed access when he tries to
837    connect as <productname>PostgreSQL</> user <literal>bob</>, not
838    as <literal>robert</> or anyone else. <literal>ann</> would
839    only be allowed to connect as <literal>ann</>. User
840    <literal>bryanh</> would be allowed to connect as either
841    <literal>bryanh</> or as <literal>guest1</>.
842   </para>
843
844   <example id="example-pg-ident.conf">
845    <title>An Example <filename>pg_ident.conf</> File</title>
846 <programlisting>
847 # MAPNAME       SYSTEM-USERNAME         PG-USERNAME
848
849 omicron         bryanh                  bryanh
850 omicron         ann                     ann
851 # bob has user name robert on these machines
852 omicron         robert                  bob
853 # bryanh can also connect as guest1
854 omicron         bryanh                  guest1
855 </programlisting>
856   </example>
857  </sect1>
858
859  <sect1 id="auth-methods">
860   <title>Authentication Methods</title>
861   <para>
862    The following subsections describe the authentication methods in more detail.
863   </para>
864
865   <sect2 id="auth-trust">
866    <title>Trust Authentication</title>
867
868    <para>
869     When <literal>trust</> authentication is specified,
870     <productname>PostgreSQL</productname> assumes that anyone who can
871     connect to the server is authorized to access the database with
872     whatever database user name they specify (even superuser names).
873     Of course, restrictions made in the <literal>database</> and
874     <literal>user</> columns still apply.
875     This method should only be used when there is adequate
876     operating-system-level protection on connections to the server.
877    </para>
878
879    <para>
880     <literal>trust</> authentication is appropriate and very
881     convenient for local connections on a single-user workstation.  It
882     is usually <emphasis>not</> appropriate by itself on a multiuser
883     machine.  However, you might be able to use <literal>trust</> even
884     on a multiuser machine, if you restrict access to the server's
885     Unix-domain socket file using file-system permissions.  To do this, set the
886     <varname>unix_socket_permissions</varname> (and possibly
887     <varname>unix_socket_group</varname>) configuration parameters as
888     described in <xref linkend="runtime-config-connection">.  Or you
889     could set the <varname>unix_socket_directories</varname>
890     configuration parameter to place the socket file in a suitably
891     restricted directory.
892    </para>
893
894    <para>
895     Setting file-system permissions only helps for Unix-socket connections.
896     Local TCP/IP connections are not restricted by file-system permissions.
897     Therefore, if you want to use file-system permissions for local security,
898     remove the <literal>host ... 127.0.0.1 ...</> line from
899     <filename>pg_hba.conf</>, or change it to a
900     non-<literal>trust</> authentication method.
901    </para>
902
903    <para>
904     <literal>trust</> authentication is only suitable for TCP/IP connections
905     if you trust every user on every machine that is allowed to connect
906     to the server by the <filename>pg_hba.conf</> lines that specify
907     <literal>trust</>.  It is seldom reasonable to use <literal>trust</>
908     for any TCP/IP connections other than those from <systemitem>localhost</> (127.0.0.1).
909    </para>
910
911   </sect2>
912
913   <sect2 id="auth-password">
914    <title>Password Authentication</title>
915
916    <indexterm>
917     <primary>MD5</>
918    </indexterm>
919    <indexterm>
920     <primary>password</primary>
921     <secondary>authentication</secondary>
922    </indexterm>
923
924    <para>
925     The password-based authentication methods are <literal>scram-sha-256</>,
926     <literal>md5</>, and <literal>password</>. These methods operate
927     similarly except for the way that the password is sent across the
928     connection.
929    </para>
930
931    <para>
932     Plain <literal>password</> sends the password in clear-text, and is
933     therefore vulnerable to password <quote>sniffing</> attacks. It should
934     always be avoided if possible. If the connection is protected by SSL
935     encryption then <literal>password</> can be used safely, though.
936     (Though SSL certificate authentication might be a better choice if one
937     is depending on using SSL).
938    </para>
939
940
941    <para>
942     <literal>scram-sha-256</> performs SCRAM-SHA-256 authentication, as
943     described in
944     <ulink url="https://tools.ietf.org/html/rfc5802">RFC5802</ulink>. It
945     is a challenge-response scheme, that prevents password sniffing on
946     untrusted connections. It is more secure than the <literal>md5</>
947     method, but might not be supported by older clients.
948    </para>
949
950    <para>
951     <literal>md5</> allows falling back to a less secure challenge-response
952     mechanism for those users with an MD5 hashed password.
953     The fallback mechanism also prevents password sniffing, but provides no
954     protection if an attacker manages to steal the password hash from the
955     server, and it cannot be used with the <xref
956     linkend="guc-db-user-namespace"> feature. For all other users,
957     <literal>md5</> works the same as <literal>scram-sha-256</>.
958    </para>
959
960    <para>
961     <productname>PostgreSQL</productname> database passwords are
962     separate from operating system user passwords. The password for
963     each database user is stored in the <literal>pg_authid</> system
964     catalog. Passwords can be managed with the SQL commands
965     <xref linkend="sql-createuser"> and
966     <xref linkend="sql-alterrole">,
967     e.g., <userinput>CREATE USER foo WITH PASSWORD 'secret'</userinput>.
968     If no password has been set up for a user, the stored password
969     is null and password authentication will always fail for that user.
970    </para>
971
972   </sect2>
973
974   <sect2 id="gssapi-auth">
975    <title>GSSAPI Authentication</title>
976
977    <indexterm zone="gssapi-auth">
978     <primary>GSSAPI</primary>
979    </indexterm>
980
981    <para>
982     <productname>GSSAPI</productname> is an industry-standard protocol
983     for secure authentication defined in RFC 2743.
984     <productname>PostgreSQL</productname> supports
985     <productname>GSSAPI</productname> with <productname>Kerberos</productname>
986     authentication according to RFC 1964. <productname>GSSAPI</productname>
987     provides automatic authentication (single sign-on) for systems
988     that support it. The authentication itself is secure, but the
989     data sent over the database connection will be sent unencrypted unless
990     <acronym>SSL</acronym> is used.
991    </para>
992
993    <para>
994     GSSAPI support has to be enabled when <productname>PostgreSQL</> is built;
995     see <xref linkend="installation"> for more information.
996    </para>
997
998    <para>
999     When <productname>GSSAPI</productname> uses
1000     <productname>Kerberos</productname>, it uses a standard principal
1001     in the format
1002     <literal><replaceable>servicename</>/<replaceable>hostname</>@<replaceable>realm</></literal>.
1003     The PostgreSQL server will accept any principal that is included in the keytab used by
1004     the server, but care needs to be taken to specify the correct principal details when
1005     making the connection from the client using the <literal>krbsrvname</> connection parameter. (See
1006     also <xref linkend="libpq-paramkeywords">.) The installation default can be
1007     changed from the default <literal>postgres</literal> at build time using
1008     <literal>./configure --with-krb-srvnam=</><replaceable>whatever</>.
1009     In most environments,
1010     this parameter never needs to be changed.
1011     Some Kerberos implementations might require a different service name,
1012     such as Microsoft Active Directory which requires the service name
1013     to be in upper case (<literal>POSTGRES</literal>).
1014    </para>
1015    <para>
1016     <replaceable>hostname</> is the fully qualified host name of the
1017     server machine. The service principal's realm is the preferred realm
1018     of the server machine.
1019    </para>
1020
1021    <para>
1022     Client principals can be mapped to different <productname>PostgreSQL</>
1023     database user names with <filename>pg_ident.conf</>.  For example,
1024     <literal>pgusername@realm</> could be mapped to just <literal>pgusername</>.
1025     Alternatively, you can use the full <literal>username@realm</> principal as
1026     the role name in <productname>PostgreSQL</> without any mapping.
1027    </para>
1028
1029    <para>
1030     <productname>PostgreSQL</> also supports a parameter to strip the realm from
1031     the principal.  This method is supported for backwards compatibility and is
1032     strongly discouraged as it is then impossible to distinguish different users
1033     with the same user name but coming from different realms.  To enable this,
1034     set <literal>include_realm</> to 0.  For simple single-realm
1035     installations, doing that combined with setting the
1036     <literal>krb_realm</> parameter (which checks that the principal's realm
1037     matches exactly what is in the <literal>krb_realm</literal> parameter)
1038     is still secure; but this is a
1039     less capable approach compared to specifying an explicit mapping in
1040     <filename>pg_ident.conf</>.
1041    </para>
1042
1043    <para>
1044     Make sure that your server keytab file is readable (and preferably
1045     only readable, not writable) by the <productname>PostgreSQL</productname>
1046     server account.  (See also <xref linkend="postgres-user">.) The location
1047     of the key file is specified by the <xref
1048     linkend="guc-krb-server-keyfile"> configuration
1049     parameter. The default is
1050     <filename>/usr/local/pgsql/etc/krb5.keytab</> (or whatever
1051     directory was specified as <varname>sysconfdir</> at build time).
1052     For security reasons, it is recommended to use a separate keytab
1053     just for the <productname>PostgreSQL</productname> server rather
1054     than opening up permissions on the system keytab file.
1055    </para>
1056    <para>
1057     The keytab file is generated by the Kerberos software; see the
1058     Kerberos documentation for details. The following example is
1059    for MIT-compatible Kerberos 5 implementations:
1060 <screen>
1061 <prompt>kadmin% </><userinput>ank -randkey postgres/server.my.domain.org</>
1062 <prompt>kadmin% </><userinput>ktadd -k krb5.keytab postgres/server.my.domain.org</>
1063 </screen>
1064    </para>
1065
1066    <para>
1067     When connecting to the database make sure you have a ticket for a
1068     principal matching the requested database user name. For example, for
1069     database user name <literal>fred</>, principal
1070     <literal>fred@EXAMPLE.COM</> would be able to connect. To also allow
1071     principal <literal>fred/users.example.com@EXAMPLE.COM</>, use a user name
1072     map, as described in <xref linkend="auth-username-maps">.
1073    </para>
1074
1075    <para>
1076     The following configuration options are supported for <productname>GSSAPI</productname>:
1077     <variablelist>
1078      <varlistentry>
1079       <term><literal>include_realm</literal></term>
1080       <listitem>
1081        <para>
1082         If set to 0, the realm name from the authenticated user principal is
1083         stripped off before being passed through the user name mapping
1084         (<xref linkend="auth-username-maps">). This is discouraged and is
1085         primarily available for backwards compatibility, as it is not secure
1086         in multi-realm environments unless <literal>krb_realm</literal> is
1087         also used.  It is recommended to
1088         leave <literal>include_realm</literal> set to the default (1) and to
1089         provide an explicit mapping in <filename>pg_ident.conf</> to convert
1090         principal names to <productname>PostgreSQL</> user names.
1091        </para>
1092       </listitem>
1093      </varlistentry>
1094
1095      <varlistentry>
1096       <term><literal>map</literal></term>
1097       <listitem>
1098        <para>
1099         Allows for mapping between system and database user names. See
1100         <xref linkend="auth-username-maps"> for details.  For a GSSAPI/Kerberos
1101         principal, such as <literal>username@EXAMPLE.COM</literal> (or, less
1102         commonly, <literal>username/hostbased@EXAMPLE.COM</literal>), the
1103         user name used for mapping is
1104         <literal>username@EXAMPLE.COM</literal> (or
1105         <literal>username/hostbased@EXAMPLE.COM</literal>, respectively),
1106         unless <literal>include_realm</literal> has been set to 0, in which case
1107         <literal>username</literal> (or <literal>username/hostbased</literal>)
1108         is what is seen as the system user name when mapping.
1109        </para>
1110       </listitem>
1111      </varlistentry>
1112
1113      <varlistentry>
1114       <term><literal>krb_realm</literal></term>
1115       <listitem>
1116        <para>
1117         Sets the realm to match user principal names against. If this parameter
1118         is set, only users of that realm will be accepted.  If it is not set,
1119         users of any realm can connect, subject to whatever user name mapping
1120         is done.
1121        </para>
1122       </listitem>
1123      </varlistentry>
1124     </variablelist>
1125    </para>
1126   </sect2>
1127
1128   <sect2 id="sspi-auth">
1129    <title>SSPI Authentication</title>
1130
1131    <indexterm zone="sspi-auth">
1132     <primary>SSPI</primary>
1133    </indexterm>
1134
1135    <para>
1136     <productname>SSPI</productname> is a <productname>Windows</productname>
1137     technology for secure authentication with single sign-on.
1138     <productname>PostgreSQL</productname> will use SSPI in
1139     <literal>negotiate</literal> mode, which will use
1140     <productname>Kerberos</productname> when possible and automatically
1141     fall back to <productname>NTLM</productname> in other cases.
1142     <productname>SSPI</productname> authentication only works when both
1143     server and client are running <productname>Windows</productname>,
1144     or, on non-Windows platforms, when <productname>GSSAPI</productname>
1145     is available.
1146    </para>
1147
1148    <para>
1149     When using <productname>Kerberos</productname> authentication,
1150     <productname>SSPI</productname> works the same way
1151     <productname>GSSAPI</productname> does; see <xref linkend="gssapi-auth">
1152     for details.
1153    </para>
1154
1155    <para>
1156     The following configuration options are supported for <productname>SSPI</productname>:
1157     <variablelist>
1158
1159      <varlistentry>
1160       <term><literal>include_realm</literal></term>
1161       <listitem>
1162        <para>
1163         If set to 0, the realm name from the authenticated user principal is
1164         stripped off before being passed through the user name mapping
1165         (<xref linkend="auth-username-maps">). This is discouraged and is
1166         primarily available for backwards compatibility, as it is not secure
1167         in multi-realm environments unless <literal>krb_realm</literal> is
1168         also used.  It is recommended to
1169         leave <literal>include_realm</literal> set to the default (1) and to
1170         provide an explicit mapping in <filename>pg_ident.conf</> to convert
1171         principal names to <productname>PostgreSQL</> user names.
1172        </para>
1173       </listitem>
1174      </varlistentry>
1175
1176      <varlistentry>
1177       <term><literal>compat_realm</literal></term>
1178       <listitem>
1179        <para>
1180         If set to 1, the domain's SAM-compatible name (also known as the
1181         NetBIOS name) is used for the <literal>include_realm</literal>
1182         option. This is the default. If set to 0, the true realm name from
1183         the Kerberos user principal name is used.
1184        </para>
1185        <para>
1186         Do not disable this option unless your server runs under a domain
1187         account (this includes virtual service accounts on a domain member
1188         system) and all clients authenticating through SSPI are also using
1189         domain accounts, or authentication will fail.
1190        </para>
1191       </listitem>
1192      </varlistentry>
1193
1194      <varlistentry>
1195       <term><literal>upn_username</literal></term>
1196       <listitem>
1197        <para>
1198         If this option is enabled along with <literal>compat_realm</literal>,
1199         the user name from the Kerberos UPN is used for authentication. If
1200         it is disabled (the default), the SAM-compatible user name is used.
1201         By default, these two names are identical for new user accounts.
1202        </para>
1203        <para>
1204         Note that <application>libpq</> uses the SAM-compatible name if no
1205         explicit user name is specified. If you use
1206         <application>libpq</> or a driver based on it, you should
1207         leave this option disabled or explicitly specify user name in the
1208         connection string.
1209        </para>
1210       </listitem>
1211      </varlistentry>
1212
1213      <varlistentry>
1214       <term><literal>map</literal></term>
1215       <listitem>
1216        <para>
1217         Allows for mapping between system and database user names. See
1218         <xref linkend="auth-username-maps"> for details.  For a SSPI/Kerberos
1219         principal, such as <literal>username@EXAMPLE.COM</literal> (or, less
1220         commonly, <literal>username/hostbased@EXAMPLE.COM</literal>), the
1221         user name used for mapping is
1222         <literal>username@EXAMPLE.COM</literal> (or
1223         <literal>username/hostbased@EXAMPLE.COM</literal>, respectively),
1224         unless <literal>include_realm</literal> has been set to 0, in which case
1225         <literal>username</literal> (or <literal>username/hostbased</literal>)
1226         is what is seen as the system user name when mapping.
1227        </para>
1228       </listitem>
1229      </varlistentry>
1230
1231      <varlistentry>
1232       <term><literal>krb_realm</literal></term>
1233       <listitem>
1234        <para>
1235         Sets the realm to match user principal names against. If this parameter
1236         is set, only users of that realm will be accepted.  If it is not set,
1237         users of any realm can connect, subject to whatever user name mapping
1238         is done.
1239        </para>
1240       </listitem>
1241      </varlistentry>
1242     </variablelist>
1243    </para>
1244   </sect2>
1245
1246   <sect2 id="auth-ident">
1247    <title>Ident Authentication</title>
1248
1249    <indexterm>
1250     <primary>ident</primary>
1251    </indexterm>
1252
1253    <para>
1254     The ident authentication method works by obtaining the client's
1255     operating system user name from an ident server and using it as
1256     the allowed database user name (with an optional user name mapping).
1257     This is only supported on TCP/IP connections.
1258    </para>
1259
1260    <note>
1261     <para>
1262      When ident is specified for a local (non-TCP/IP) connection,
1263      peer authentication (see <xref linkend="auth-peer">) will be
1264      used instead.
1265     </para>
1266    </note>
1267
1268    <para>
1269     The following configuration options are supported for <productname>ident</productname>:
1270     <variablelist>
1271      <varlistentry>
1272       <term><literal>map</literal></term>
1273       <listitem>
1274        <para>
1275         Allows for mapping between system and database user names. See
1276         <xref linkend="auth-username-maps"> for details.
1277        </para>
1278       </listitem>
1279      </varlistentry>
1280     </variablelist>
1281    </para>
1282
1283    <para>
1284     The <quote>Identification Protocol</quote> is described in
1285     RFC 1413. Virtually every Unix-like
1286     operating system ships with an ident server that listens on TCP
1287     port 113 by default. The basic functionality of an ident server
1288     is to answer questions like <quote>What user initiated the
1289     connection that goes out of your port <replaceable>X</replaceable>
1290     and connects to my port <replaceable>Y</replaceable>?</quote>.
1291     Since <productname>PostgreSQL</> knows both <replaceable>X</> and
1292     <replaceable>Y</> when a physical connection is established, it
1293     can interrogate the ident server on the host of the connecting
1294     client and can theoretically determine the operating system user
1295     for any given connection.
1296    </para>
1297
1298    <para>
1299     The drawback of this procedure is that it depends on the integrity
1300     of the client: if the client machine is untrusted or compromised,
1301     an attacker could run just about any program on port 113 and
1302     return any user name they choose. This authentication method is
1303     therefore only appropriate for closed networks where each client
1304     machine is under tight control and where the database and system
1305     administrators operate in close contact. In other words, you must
1306     trust the machine running the ident server.
1307     Heed the warning:
1308     <blockquote>
1309      <attribution>RFC 1413</attribution>
1310      <para>
1311       The Identification Protocol is not intended as an authorization
1312       or access control protocol.
1313      </para>
1314     </blockquote>
1315    </para>
1316
1317    <para>
1318     Some ident servers have a nonstandard option that causes the returned
1319     user name to be encrypted, using a key that only the originating
1320     machine's administrator knows.  This option <emphasis>must not</> be
1321     used when using the ident server with <productname>PostgreSQL</>,
1322     since <productname>PostgreSQL</> does not have any way to decrypt the
1323     returned string to determine the actual user name.
1324    </para>
1325   </sect2>
1326
1327   <sect2 id="auth-peer">
1328    <title>Peer Authentication</title>
1329
1330    <indexterm>
1331     <primary>peer</primary>
1332    </indexterm>
1333
1334    <para>
1335     The peer authentication method works by obtaining the client's
1336     operating system user name from the kernel and using it as the
1337     allowed database user name (with optional user name mapping). This
1338     method is only supported on local connections.
1339    </para>
1340
1341    <para>
1342     The following configuration options are supported for <productname>peer</productname>:
1343     <variablelist>
1344      <varlistentry>
1345       <term><literal>map</literal></term>
1346       <listitem>
1347        <para>
1348         Allows for mapping between system and database user names. See
1349         <xref linkend="auth-username-maps"> for details.
1350        </para>
1351       </listitem>
1352      </varlistentry>
1353     </variablelist>
1354    </para>
1355
1356    <para>
1357     Peer authentication is only available on operating systems providing
1358     the <function>getpeereid()</> function, the <symbol>SO_PEERCRED</symbol>
1359     socket parameter, or similar mechanisms.  Currently that includes
1360     <systemitem class="osname">Linux</>,
1361     most flavors of <systemitem class="osname">BSD</> including
1362     <systemitem class="osname">macOS</>,
1363     and <systemitem class="osname">Solaris</systemitem>.
1364    </para>
1365
1366   </sect2>
1367
1368   <sect2 id="auth-ldap">
1369    <title>LDAP Authentication</title>
1370
1371    <indexterm zone="auth-ldap">
1372     <primary>LDAP</primary>
1373    </indexterm>
1374
1375    <para>
1376     This authentication method operates similarly to
1377     <literal>password</literal> except that it uses LDAP
1378     as the password verification method. LDAP is used only to validate
1379     the user name/password pairs. Therefore the user must already
1380     exist in the database before LDAP can be used for
1381     authentication.
1382    </para>
1383
1384    <para>
1385     LDAP authentication can operate in two modes. In the first mode,
1386     which we will call the simple bind mode,
1387     the server will bind to the distinguished name constructed as
1388     <replaceable>prefix</> <replaceable>username</> <replaceable>suffix</>.
1389     Typically, the <replaceable>prefix</> parameter is used to specify
1390     <literal>cn=</>, or <replaceable>DOMAIN</><literal>\</> in an Active
1391     Directory environment.  <replaceable>suffix</> is used to specify the
1392     remaining part of the DN in a non-Active Directory environment.
1393    </para>
1394
1395    <para>
1396     In the second mode, which we will call the search+bind mode,
1397     the server first binds to the LDAP directory with
1398     a fixed user name and password, specified with <replaceable>ldapbinddn</>
1399     and <replaceable>ldapbindpasswd</>, and performs a search for the user trying
1400     to log in to the database. If no user and password is configured, an
1401     anonymous bind will be attempted to the directory. The search will be
1402     performed over the subtree at <replaceable>ldapbasedn</>, and will try to
1403     do an exact match of the attribute specified in
1404     <replaceable>ldapsearchattribute</>.
1405     Once the user has been found in
1406     this search, the server disconnects and re-binds to the directory as
1407     this user, using the password specified by the client, to verify that the
1408     login is correct. This mode is the same as that used by LDAP authentication
1409     schemes in other software, such as Apache <literal>mod_authnz_ldap</literal> and <literal>pam_ldap</literal>.
1410     This method allows for significantly more flexibility
1411     in where the user objects are located in the directory, but will cause
1412     two separate connections to the LDAP server to be made.
1413    </para>
1414
1415    <para>
1416     The following configuration options are used in both modes:
1417     <variablelist>
1418      <varlistentry>
1419       <term><literal>ldapserver</literal></term>
1420       <listitem>
1421        <para>
1422         Names or IP addresses of LDAP servers to connect to. Multiple
1423         servers may be specified, separated by spaces.
1424        </para>
1425       </listitem>
1426      </varlistentry>
1427      <varlistentry>
1428       <term><literal>ldapport</literal></term>
1429       <listitem>
1430        <para>
1431         Port number on LDAP server to connect to. If no port is specified,
1432         the LDAP library's default port setting will be used.
1433        </para>
1434       </listitem>
1435      </varlistentry>
1436      <varlistentry>
1437       <term><literal>ldaptls</literal></term>
1438       <listitem>
1439        <para>
1440         Set to 1 to make the connection between PostgreSQL and the
1441         LDAP server use TLS encryption. Note that this only encrypts
1442         the traffic to the LDAP server &mdash; the connection to the client
1443         will still be unencrypted unless SSL is used.
1444        </para>
1445       </listitem>
1446      </varlistentry>
1447     </variablelist>
1448
1449     The following options are used in simple bind mode only:
1450     <variablelist>
1451      <varlistentry>
1452       <term><literal>ldapprefix</literal></term>
1453       <listitem>
1454        <para>
1455         String to prepend to the user name when forming the DN to bind as,
1456         when doing simple bind authentication.
1457        </para>
1458       </listitem>
1459      </varlistentry>
1460      <varlistentry>
1461       <term><literal>ldapsuffix</literal></term>
1462       <listitem>
1463        <para>
1464         String to append to the user name when forming the DN to bind as,
1465         when doing simple bind authentication.
1466        </para>
1467       </listitem>
1468      </varlistentry>
1469     </variablelist>
1470
1471     The following options are used in search+bind mode only:
1472     <variablelist>
1473      <varlistentry>
1474       <term><literal>ldapbasedn</literal></term>
1475       <listitem>
1476        <para>
1477         Root DN to begin the search for the user in, when doing search+bind
1478         authentication.
1479        </para>
1480       </listitem>
1481      </varlistentry>
1482      <varlistentry>
1483       <term><literal>ldapbinddn</literal></term>
1484       <listitem>
1485        <para>
1486         DN of user to bind to the directory with to perform the search when
1487         doing search+bind authentication.
1488        </para>
1489       </listitem>
1490      </varlistentry>
1491      <varlistentry>
1492       <term><literal>ldapbindpasswd</literal></term>
1493       <listitem>
1494        <para>
1495         Password for user to bind to the directory with to perform the search
1496         when doing search+bind authentication.
1497        </para>
1498       </listitem>
1499       </varlistentry>
1500       <varlistentry>
1501        <term><literal>ldapsearchattribute</literal></term>
1502        <listitem>
1503         <para>
1504          Attribute to match against the user name in the search when doing
1505          search+bind authentication.  If no attribute is specified, the
1506          <literal>uid</> attribute will be used.
1507         </para>
1508        </listitem>
1509       </varlistentry>
1510       <varlistentry>
1511        <term><literal>ldapurl</literal></term>
1512        <listitem>
1513         <para>
1514          An RFC 4516 LDAP URL.  This is an alternative way to write some of the
1515          other LDAP options in a more compact and standard form.  The format is
1516 <synopsis>
1517 ldap://<replaceable>host</replaceable>[:<replaceable>port</replaceable>]/<replaceable>basedn</replaceable>[?[<replaceable>attribute</replaceable>][?[<replaceable>scope</replaceable>]]]
1518 </synopsis>
1519          <replaceable>scope</replaceable> must be one
1520          of <literal>base</literal>, <literal>one</literal>, <literal>sub</literal>,
1521          typically the latter.  Only one attribute is used, and some other
1522          components of standard LDAP URLs such as filters and extensions are
1523          not supported.
1524         </para>
1525
1526         <para>
1527          For non-anonymous binds, <literal>ldapbinddn</literal>
1528          and <literal>ldapbindpasswd</literal> must be specified as separate
1529          options.
1530         </para>
1531
1532         <para>
1533          To use encrypted LDAP connections, the <literal>ldaptls</literal>
1534          option has to be used in addition to <literal>ldapurl</literal>.
1535          The <literal>ldaps</literal> URL scheme (direct SSL connection) is not
1536          supported.
1537         </para>
1538
1539         <para>
1540          LDAP URLs are currently only supported with OpenLDAP, not on Windows.
1541         </para>
1542        </listitem>
1543       </varlistentry>
1544     </variablelist>
1545    </para>
1546
1547    <para>
1548     It is an error to mix configuration options for simple bind with options
1549     for search+bind.
1550    </para>
1551
1552    <para>
1553     Here is an example for a simple-bind LDAP configuration:
1554 <programlisting>
1555 host ... ldap ldapserver=ldap.example.net ldapprefix="cn=" ldapsuffix=", dc=example, dc=net"
1556 </programlisting>
1557     When a connection to the database server as database
1558     user <literal>someuser</literal> is requested, PostgreSQL will attempt to
1559     bind to the LDAP server using the DN <literal>cn=someuser, dc=example,
1560     dc=net</literal> and the password provided by the client.  If that connection
1561     succeeds, the database access is granted.
1562    </para>
1563
1564    <para>
1565     Here is an example for a search+bind configuration:
1566 <programlisting>
1567 host ... ldap ldapserver=ldap.example.net ldapbasedn="dc=example, dc=net" ldapsearchattribute=uid
1568 </programlisting>
1569     When a connection to the database server as database
1570     user <literal>someuser</literal> is requested, PostgreSQL will attempt to
1571     bind anonymously (since <literal>ldapbinddn</literal> was not specified) to
1572     the LDAP server, perform a search for <literal>(uid=someuser)</literal>
1573     under the specified base DN.  If an entry is found, it will then attempt to
1574     bind using that found information and the password supplied by the client.
1575     If that second connection succeeds, the database access is granted.
1576    </para>
1577
1578    <para>
1579     Here is the same search+bind configuration written as a URL:
1580 <programlisting>
1581 host ... ldap ldapurl="ldap://ldap.example.net/dc=example,dc=net?uid?sub"
1582 </programlisting>
1583     Some other software that supports authentication against LDAP uses the
1584     same URL format, so it will be easier to share the configuration.
1585    </para>
1586
1587    <tip>
1588     <para>
1589      Since LDAP often uses commas and spaces to separate the different
1590      parts of a DN, it is often necessary to use double-quoted parameter
1591      values when configuring LDAP options, as shown in the examples.
1592     </para>
1593    </tip>
1594
1595   </sect2>
1596
1597   <sect2 id="auth-radius">
1598    <title>RADIUS Authentication</title>
1599
1600    <indexterm zone="auth-radius">
1601     <primary>RADIUS</primary>
1602    </indexterm>
1603
1604    <para>
1605     This authentication method operates similarly to
1606     <literal>password</literal> except that it uses RADIUS
1607     as the password verification method. RADIUS is used only to validate
1608     the user name/password pairs. Therefore the user must already
1609     exist in the database before RADIUS can be used for
1610     authentication.
1611    </para>
1612
1613    <para>
1614     When using RADIUS authentication, an Access Request message will be sent
1615     to the configured RADIUS server. This request will be of type
1616     <literal>Authenticate Only</literal>, and include parameters for
1617     <literal>user name</>, <literal>password</> (encrypted) and
1618     <literal>NAS Identifier</>. The request will be encrypted using
1619     a secret shared with the server. The RADIUS server will respond to
1620     this server with either <literal>Access Accept</> or
1621     <literal>Access Reject</>. There is no support for RADIUS accounting.
1622    </para>
1623
1624    <para>
1625     Multiple RADIUS servers can be specified, in which case they will
1626     be tried sequentially. If a negative response is received from
1627     a server, the authentication will fail. If no response is received,
1628     the next server in the list will be tried. To specify multiple
1629     servers, put the names within quotes and separate the server names
1630     with a comma. If multiple servers are specified, all other RADIUS
1631     options can also be given as a comma separate list, to apply
1632     individual values to each server. They can also be specified as
1633     a single value, in which case this value will apply to all servers.
1634    </para>
1635
1636    <para>
1637     The following configuration options are supported for RADIUS:
1638      <variablelist>
1639       <varlistentry>
1640        <term><literal>radiusservers</literal></term>
1641        <listitem>
1642         <para>
1643          The name or IP addresses of the RADIUS servers to connect to.
1644          This parameter is required.
1645         </para>
1646        </listitem>
1647       </varlistentry>
1648
1649       <varlistentry>
1650        <term><literal>radiussecrets</literal></term>
1651        <listitem>
1652         <para>
1653          The shared secrets used when talking securely to the RADIUS
1654          server. This must have exactly the same value on the PostgreSQL
1655          and RADIUS servers. It is recommended that this be a string of
1656          at least 16 characters. This parameter is required.
1657          <note>
1658          <para>
1659           The encryption vector used will only be cryptographically
1660           strong if <productname>PostgreSQL</> is built with support for
1661           <productname>OpenSSL</>. In other cases, the transmission to the
1662           RADIUS server should only be considered obfuscated, not secured, and
1663           external security measures should be applied if necessary.
1664          </para>
1665          </note>
1666         </para>
1667        </listitem>
1668       </varlistentry>
1669
1670       <varlistentry>
1671        <term><literal>radiusports</literal></term>
1672        <listitem>
1673         <para>
1674          The port number on the RADIUS servers to connect to. If no port
1675          is specified, the default port <literal>1812</> will be used.
1676         </para>
1677        </listitem>
1678       </varlistentry>
1679
1680       <varlistentry>
1681        <term><literal>radiusidentifiers</literal></term>
1682        <listitem>
1683         <para>
1684          The string used as <literal>NAS Identifier</> in the RADIUS
1685          requests. This parameter can be used as a second parameter
1686          identifying for example which database user the user is attempting
1687          to authenticate as, which can be used for policy matching on
1688          the RADIUS server. If no identifier is specified, the default
1689          <literal>postgresql</> will be used.
1690         </para>
1691        </listitem>
1692       </varlistentry>
1693
1694      </variablelist>
1695    </para>
1696   </sect2>
1697
1698   <sect2 id="auth-cert">
1699    <title>Certificate Authentication</title>
1700
1701    <indexterm zone="auth-cert">
1702     <primary>Certificate</primary>
1703    </indexterm>
1704
1705    <para>
1706     This authentication method uses SSL client certificates to perform
1707     authentication. It is therefore only available for SSL connections.
1708     When using this authentication method, the server will require that
1709     the client provide a valid, trusted certificate.  No password prompt
1710     will be sent to the client.  The <literal>cn</literal> (Common Name)
1711     attribute of the certificate
1712     will be compared to the requested database user name, and if they match
1713     the login will be allowed.  User name mapping can be used to allow
1714     <literal>cn</literal> to be different from the database user name.
1715    </para>
1716
1717    <para>
1718     The following configuration options are supported for SSL certificate
1719     authentication:
1720     <variablelist>
1721      <varlistentry>
1722       <term><literal>map</literal></term>
1723       <listitem>
1724        <para>
1725         Allows for mapping between system and database user names. See
1726         <xref linkend="auth-username-maps"> for details.
1727        </para>
1728       </listitem>
1729      </varlistentry>
1730     </variablelist>
1731    </para>
1732
1733    <para>
1734     In a <filename>pg_hba.conf</> record specifying certificate
1735     authentication, the authentication option <literal>clientcert</> is
1736     assumed to be <literal>1</>, and it cannot be turned off since a client
1737     certificate is necessary for this method.  What the <literal>cert</>
1738     method adds to the basic <literal>clientcert</> certificate validity test
1739     is a check that the <literal>cn</literal> attribute matches the database
1740     user name.
1741    </para>
1742   </sect2>
1743
1744   <sect2 id="auth-pam">
1745    <title>PAM Authentication</title>
1746
1747    <indexterm zone="auth-pam">
1748     <primary>PAM</primary>
1749    </indexterm>
1750
1751    <para>
1752     This authentication method operates similarly to
1753     <literal>password</literal> except that it uses PAM (Pluggable
1754     Authentication Modules) as the authentication mechanism. The
1755     default PAM service name is <literal>postgresql</literal>.
1756     PAM is used only to validate user name/password pairs and optionally the
1757     connected remote host name or IP address. Therefore the user must already
1758     exist in the database before PAM can be used for authentication.  For more
1759     information about PAM, please read the
1760     <ulink url="http://www.kernel.org/pub/linux/libs/pam/">
1761     <productname>Linux-PAM</> Page</ulink>.
1762    </para>
1763
1764    <para>
1765     The following configuration options are supported for PAM:
1766     <variablelist>
1767      <varlistentry>
1768       <term><literal>pamservice</literal></term>
1769       <listitem>
1770        <para>
1771         PAM service name.
1772        </para>
1773       </listitem>
1774      </varlistentry>
1775      <varlistentry>
1776       <term><literal>pam_use_hostname</literal></term>
1777       <listitem>
1778        <para>
1779         Determines whether the remote IP address or the host name is provided
1780         to PAM modules through the <symbol>PAM_RHOST</symbol> item.  By
1781         default, the IP address is used.  Set this option to 1 to use the
1782         resolved host name instead.  Host name resolution can lead to login
1783         delays.  (Most PAM configurations don't use this information, so it is
1784         only necessary to consider this setting if a PAM configuration was
1785         specifically created to make use of it.)
1786        </para>
1787       </listitem>
1788      </varlistentry>
1789     </variablelist>
1790    </para>
1791
1792    <note>
1793     <para>
1794      If PAM is set up to read <filename>/etc/shadow</>, authentication
1795      will fail because the PostgreSQL server is started by a non-root
1796      user.  However, this is not an issue when PAM is configured to use
1797      LDAP or other authentication methods.
1798     </para>
1799    </note>
1800   </sect2>
1801
1802   <sect2 id="auth-bsd">
1803    <title>BSD Authentication</title>
1804
1805    <indexterm zone="auth-bsd">
1806     <primary>BSD Authentication</primary>
1807    </indexterm>
1808
1809    <para>
1810     This authentication method operates similarly to
1811     <literal>password</literal> except that it uses BSD Authentication
1812     to verify the password. BSD Authentication is used only
1813     to validate user name/password pairs. Therefore the user's role must
1814     already exist in the database before BSD Authentication can be used
1815     for authentication. The BSD Authentication framework is currently
1816     only available on OpenBSD.
1817    </para>
1818
1819    <para>
1820     BSD Authentication in <productname>PostgreSQL</> uses
1821     the <literal>auth-postgresql</literal> login type and authenticates with
1822     the <literal>postgresql</literal> login class if that's defined
1823     in <filename>login.conf</filename>. By default that login class does not
1824     exist, and <productname>PostgreSQL</> will use the default login class.
1825    </para>
1826
1827    <note>
1828     <para>
1829      To use BSD Authentication, the PostgreSQL user account (that is, the
1830      operating system user running the server) must first be added to
1831      the <literal>auth</literal> group.  The <literal>auth</literal> group
1832      exists by default on OpenBSD systems.
1833     </para>
1834    </note>
1835   </sect2>
1836  </sect1>
1837
1838   <sect1 id="client-authentication-problems">
1839    <title>Authentication Problems</title>
1840
1841    <para>
1842     Authentication failures and related problems generally
1843     manifest themselves through error messages like the following:
1844    </para>
1845
1846    <para>
1847 <programlisting>
1848 FATAL:  no pg_hba.conf entry for host "123.123.123.123", user "andym", database "testdb"
1849 </programlisting>
1850     This is what you are most likely to get if you succeed in contacting
1851     the server, but it does not want to talk to you. As the message
1852     suggests, the server refused the connection request because it found
1853     no matching entry in its <filename>pg_hba.conf</filename>
1854     configuration file.
1855    </para>
1856
1857    <para>
1858 <programlisting>
1859 FATAL:  password authentication failed for user "andym"
1860 </programlisting>
1861     Messages like this indicate that you contacted the server, and it is
1862     willing to talk to you, but not until you pass the authorization
1863     method specified in the <filename>pg_hba.conf</filename> file. Check
1864     the password you are providing, or check your Kerberos or ident
1865     software if the complaint mentions one of those authentication
1866     types.
1867    </para>
1868
1869    <para>
1870 <programlisting>
1871 FATAL:  user "andym" does not exist
1872 </programlisting>
1873     The indicated database user name was not found.
1874    </para>
1875
1876    <para>
1877 <programlisting>
1878 FATAL:  database "testdb" does not exist
1879 </programlisting>
1880     The database you are trying to connect to does not exist. Note that
1881     if you do not specify a database name, it defaults to the database
1882     user name, which might or might not be the right thing.
1883    </para>
1884
1885    <tip>
1886    <para>
1887     The server log might contain more information about an
1888     authentication failure than is reported to the client. If you are
1889     confused about the reason for a failure, check the server log.
1890    </para>
1891    </tip>
1892   </sect1>
1893
1894  </chapter>