At update of non-LP_NORMAL TID, fail instead of corrupting page header.
[pgsql.git] / doc / src / sgml / client-auth.sgml
blob782b49c85ac5689562b6810a0ece16793ecd8da6
1 <!-- doc/src/sgml/client-auth.sgml -->
3 <chapter id="client-authentication">
4 <title>Client Authentication</title>
6 <indexterm zone="client-authentication">
7 <primary>client authentication</primary>
8 </indexterm>
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>
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</quote>. In this chapter, we
25 consistently use <firstterm>database user</firstterm> to mean <quote>role with the
26 <literal>LOGIN</literal> privilege</quote>.
27 </para>
28 </note>
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>
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>
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>
57 <sect1 id="auth-pg-hba-conf">
58 <title>The <filename>pg_hba.conf</filename> File</title>
60 <indexterm zone="auth-pg-hba-conf">
61 <primary>pg_hba.conf</primary>
62 </indexterm>
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</acronym> stands for host-based authentication.) A default
70 <filename>pg_hba.conf</filename> file is installed when the data
71 directory is initialized by <xref linkend="app-initdb"/>. It is
72 possible to place the authentication configuration file elsewhere,
73 however; see the <xref linkend="guc-hba-file"/> configuration parameter.
74 </para>
76 <para>
77 The <filename>pg_hba.conf</filename> file is read on start-up and when
78 the main server process receives a
79 <systemitem>SIGHUP</systemitem><indexterm><primary>SIGHUP</primary></indexterm>
80 signal. If you edit the file on an
81 active system, you will need to signal the postmaster
82 (using <literal>pg_ctl reload</literal>, calling the SQL function
83 <function>pg_reload_conf()</function>, or using <literal>kill
84 -HUP</literal>) to make it re-read the file.
85 </para>
87 <note>
88 <para>
89 The preceding statement is not true on Microsoft Windows: there, any
90 changes in the <filename>pg_hba.conf</filename> file are immediately
91 applied by subsequent new connections.
92 </para>
93 </note>
95 <para>
96 The system view
97 <link linkend="view-pg-hba-file-rules"><structname>pg_hba_file_rules</structname></link>
98 can be helpful for pre-testing changes to the <filename>pg_hba.conf</filename>
99 file, or for diagnosing problems if loading of the file did not have the
100 desired effects. Rows in the view with
101 non-null <structfield>error</structfield> fields indicate problems in the
102 corresponding lines of the file.
103 </para>
105 <para>
106 The general format of the <filename>pg_hba.conf</filename> file is
107 a set of records, one per line. Blank lines are ignored, as is any
108 text after the <literal>#</literal> comment character.
109 A record can be continued onto the next line by ending the line with
110 a backslash. (Backslashes are not special except at the end of a line.)
111 A record is made
112 up of a number of fields which are separated by spaces and/or tabs.
113 Fields can contain white space if the field value is double-quoted.
114 Quoting one of the keywords in a database, user, or address field (e.g.,
115 <literal>all</literal> or <literal>replication</literal>) makes the word lose its special
116 meaning, and just match a database, user, or host with that name.
117 Backslash line continuation applies even within quoted text or comments.
118 </para>
120 <para>
121 Each authentication record specifies a connection type, a client IP address
122 range (if relevant for the connection type), a database name, a user name,
123 and the authentication method to be used for connections matching
124 these parameters. The first record with a matching connection type,
125 client address, requested database, and user name is used to perform
126 authentication. There is no <quote>fall-through</quote> or
127 <quote>backup</quote>: if one record is chosen and the authentication
128 fails, subsequent records are not considered. If no record matches,
129 access is denied.
130 </para>
132 <para>
133 Each record can be an include directive or an authentication record.
134 Include directives specify files that can be included, that contain
135 additional records. The records will be inserted in place of the
136 include directives. Include directives only contain two fields:
137 <literal>include</literal>, <literal>include_if_exists</literal> or
138 <literal>include_dir</literal> directive and the file or directory to be
139 included. The file or directory can be a relative or absolute path, and can
140 be double-quoted. For the <literal>include_dir</literal> form, all files
141 not starting with a <literal>.</literal> and ending with
142 <literal>.conf</literal> will be included. Multiple files within an include
143 directory are processed in file name order (according to C locale rules,
144 i.e., numbers before letters, and uppercase letters before lowercase ones).
145 </para>
147 <para>
148 A record can have several formats:
149 <synopsis>
150 local <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>auth-method</replaceable> <optional><replaceable>auth-options</replaceable></optional>
151 host <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>address</replaceable> <replaceable>auth-method</replaceable> <optional><replaceable>auth-options</replaceable></optional>
152 hostssl <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>address</replaceable> <replaceable>auth-method</replaceable> <optional><replaceable>auth-options</replaceable></optional>
153 hostnossl <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>address</replaceable> <replaceable>auth-method</replaceable> <optional><replaceable>auth-options</replaceable></optional>
154 hostgssenc <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>address</replaceable> <replaceable>auth-method</replaceable> <optional><replaceable>auth-options</replaceable></optional>
155 hostnogssenc <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>address</replaceable> <replaceable>auth-method</replaceable> <optional><replaceable>auth-options</replaceable></optional>
156 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>
157 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>
158 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>
159 hostgssenc <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>IP-address</replaceable> <replaceable>IP-mask</replaceable> <replaceable>auth-method</replaceable> <optional><replaceable>auth-options</replaceable></optional>
160 hostnogssenc <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>IP-address</replaceable> <replaceable>IP-mask</replaceable> <replaceable>auth-method</replaceable> <optional><replaceable>auth-options</replaceable></optional>
161 include <replaceable>file</replaceable>
162 include_if_exists <replaceable>file</replaceable>
163 include_dir <replaceable>directory</replaceable>
164 </synopsis>
165 The meaning of the fields is as follows:
167 <variablelist>
168 <varlistentry>
169 <term><literal>local</literal></term>
170 <listitem>
171 <para>
172 This record matches connection attempts using Unix-domain
173 sockets. Without a record of this type, Unix-domain socket
174 connections are disallowed.
175 </para>
176 </listitem>
177 </varlistentry>
179 <varlistentry>
180 <term><literal>host</literal></term>
181 <listitem>
182 <para>
183 This record matches connection attempts made using TCP/IP.
184 <literal>host</literal> records match
185 <acronym>SSL</acronym> or non-<acronym>SSL</acronym> connection
186 attempts as well as <acronym>GSSAPI</acronym> encrypted or
187 non-<acronym>GSSAPI</acronym> encrypted connection attempts.
188 </para>
189 <note>
190 <para>
191 Remote TCP/IP connections will not be possible unless
192 the server is started with an appropriate value for the
193 <xref linkend="guc-listen-addresses"/> configuration parameter,
194 since the default behavior is to listen for TCP/IP connections
195 only on the local loopback address <literal>localhost</literal>.
196 </para>
197 </note>
198 </listitem>
199 </varlistentry>
201 <varlistentry>
202 <term><literal>hostssl</literal></term>
203 <listitem>
204 <para>
205 This record matches connection attempts made using TCP/IP,
206 but only when the connection is made with <acronym>SSL</acronym>
207 encryption.
208 </para>
210 <para>
211 To make use of this option the server must be built with
212 <acronym>SSL</acronym> support. Furthermore,
213 <acronym>SSL</acronym> must be enabled
214 by setting the <xref linkend="guc-ssl"/> configuration parameter (see
215 <xref linkend="ssl-tcp"/> for more information).
216 Otherwise, the <literal>hostssl</literal> record is ignored except for
217 logging a warning that it cannot match any connections.
218 </para>
219 </listitem>
220 </varlistentry>
222 <varlistentry>
223 <term><literal>hostnossl</literal></term>
224 <listitem>
225 <para>
226 This record type has the opposite behavior of <literal>hostssl</literal>;
227 it only matches connection attempts made over
228 TCP/IP that do not use <acronym>SSL</acronym>.
229 </para>
230 </listitem>
231 </varlistentry>
233 <varlistentry>
234 <term><literal>hostgssenc</literal></term>
235 <listitem>
236 <para>
237 This record matches connection attempts made using TCP/IP,
238 but only when the connection is made with <acronym>GSSAPI</acronym>
239 encryption.
240 </para>
242 <para>
243 To make use of this option the server must be built with
244 <acronym>GSSAPI</acronym> support. Otherwise,
245 the <literal>hostgssenc</literal> record is ignored except for logging
246 a warning that it cannot match any connections.
247 </para>
248 </listitem>
249 </varlistentry>
251 <varlistentry>
252 <term><literal>hostnogssenc</literal></term>
253 <listitem>
254 <para>
255 This record type has the opposite behavior of <literal>hostgssenc</literal>;
256 it only matches connection attempts made over
257 TCP/IP that do not use <acronym>GSSAPI</acronym> encryption.
258 </para>
259 </listitem>
260 </varlistentry>
262 <varlistentry>
263 <term><replaceable>database</replaceable></term>
264 <listitem>
265 <para>
266 Specifies which database name(s) this record matches. The value
267 <literal>all</literal> specifies that it matches all databases.
268 The value <literal>sameuser</literal> specifies that the record
269 matches if the requested database has the same name as the
270 requested user. The value <literal>samerole</literal> specifies that
271 the requested user must be a member of the role with the same
272 name as the requested database. (<literal>samegroup</literal> is an
273 obsolete but still accepted spelling of <literal>samerole</literal>.)
274 Superusers are not considered to be members of a role for the
275 purposes of <literal>samerole</literal> unless they are explicitly
276 members of the role, directly or indirectly, and not just by
277 virtue of being a superuser.
278 The value <literal>replication</literal> specifies that the record
279 matches if a physical replication connection is requested, however, it
280 doesn't match with logical replication connections. Note that physical
281 replication connections do not specify any particular database whereas
282 logical replication connections do specify it.
283 Otherwise, this is the name of a specific
284 <productname>PostgreSQL</productname> database or a regular expression.
285 Multiple database names and/or regular expressions can be supplied by
286 separating them with commas.
287 </para>
288 <para>
289 If the database name starts with a slash (<literal>/</literal>), the
290 remainder of the name is treated as a regular expression.
291 (See <xref linkend="posix-syntax-details"/> for details of
292 <productname>PostgreSQL</productname>'s regular expression syntax.)
293 </para>
294 <para>
295 A separate file containing database names and/or regular expressions
296 can be specified by preceding the file name with <literal>@</literal>.
297 </para>
298 </listitem>
299 </varlistentry>
301 <varlistentry>
302 <term><replaceable>user</replaceable></term>
303 <listitem>
304 <para>
305 Specifies which database user name(s) this record
306 matches. The value <literal>all</literal> specifies that it
307 matches all users. Otherwise, this is either the name of a specific
308 database user, a regular expression (when starting with a slash
309 (<literal>/</literal>), or a group name preceded by <literal>+</literal>.
310 (Recall that there is no real distinction between users and groups
311 in <productname>PostgreSQL</productname>; a <literal>+</literal> mark really means
312 <quote>match any of the roles that are directly or indirectly members
313 of this role</quote>, while a name without a <literal>+</literal> mark matches
314 only that specific role.) For this purpose, a superuser is only
315 considered to be a member of a role if they are explicitly a member
316 of the role, directly or indirectly, and not just by virtue of
317 being a superuser.
318 Multiple user names and/or regular expressions can be supplied by
319 separating them with commas.
320 </para>
321 <para>
322 If the user name starts with a slash (<literal>/</literal>), the
323 remainder of the name is treated as a regular expression.
324 (See <xref linkend="posix-syntax-details"/> for details of
325 <productname>PostgreSQL</productname>'s regular expression syntax.)
326 </para>
327 <para>
328 A separate file containing user names and/or regular expressions can
329 be specified by preceding the file name with <literal>@</literal>.
330 </para>
331 </listitem>
332 </varlistentry>
334 <varlistentry>
335 <term><replaceable>address</replaceable></term>
336 <listitem>
337 <para>
338 Specifies the client machine address(es) that this record
339 matches. This field can contain either a host name, an IP
340 address range, or one of the special key words mentioned below.
341 </para>
343 <para>
344 An IP address range is specified using standard numeric notation
345 for the range's starting address, then a slash (<literal>/</literal>)
346 and a <acronym>CIDR</acronym> mask length. The mask
347 length indicates the number of high-order bits of the client
348 IP address that must match. Bits to the right of this should
349 be zero in the given IP address.
350 There must not be any white space between the IP address, the
351 <literal>/</literal>, and the CIDR mask length.
352 </para>
354 <para>
355 Typical examples of an IPv4 address range specified this way are
356 <literal>172.20.143.89/32</literal> for a single host, or
357 <literal>172.20.143.0/24</literal> for a small network, or
358 <literal>10.6.0.0/16</literal> for a larger one.
359 An IPv6 address range might look like <literal>::1/128</literal>
360 for a single host (in this case the IPv6 loopback address) or
361 <literal>fe80::7a31:c1ff:0000:0000/96</literal> for a small
362 network.
363 <literal>0.0.0.0/0</literal> represents all
364 IPv4 addresses, and <literal>::0/0</literal> represents
365 all IPv6 addresses.
366 To specify a single host, use a mask length of 32 for IPv4 or
367 128 for IPv6. In a network address, do not omit trailing zeroes.
368 </para>
370 <para>
371 An entry given in IPv4 format will match only IPv4 connections,
372 and an entry given in IPv6 format will match only IPv6 connections,
373 even if the represented address is in the IPv4-in-IPv6 range.
374 </para>
376 <para>
377 You can also write <literal>all</literal> to match any IP address,
378 <literal>samehost</literal> to match any of the server's own IP
379 addresses, or <literal>samenet</literal> to match any address in any
380 subnet that the server is directly connected to.
381 </para>
383 <para>
384 If a host name is specified (anything that is not an IP address
385 range or a special key word is treated as a host name),
386 that name is compared with the result of a reverse name
387 resolution of the client's IP address (e.g., reverse DNS
388 lookup, if DNS is used). Host name comparisons are case
389 insensitive. If there is a match, then a forward name
390 resolution (e.g., forward DNS lookup) is performed on the host
391 name to check whether any of the addresses it resolves to are
392 equal to the client's IP address. If both directions match,
393 then the entry is considered to match. (The host name that is
394 used in <filename>pg_hba.conf</filename> should be the one that
395 address-to-name resolution of the client's IP address returns,
396 otherwise the line won't be matched. Some host name databases
397 allow associating an IP address with multiple host names, but
398 the operating system will only return one host name when asked
399 to resolve an IP address.)
400 </para>
402 <para>
403 A host name specification that starts with a dot
404 (<literal>.</literal>) matches a suffix of the actual host
405 name. So <literal>.example.com</literal> would match
406 <literal>foo.example.com</literal> (but not just
407 <literal>example.com</literal>).
408 </para>
410 <para>
411 When host names are specified
412 in <filename>pg_hba.conf</filename>, you should make sure that
413 name resolution is reasonably fast. It can be of advantage to
414 set up a local name resolution cache such
415 as <command>nscd</command>. Also, you may wish to enable the
416 configuration parameter <varname>log_hostname</varname> to see
417 the client's host name instead of the IP address in the log.
418 </para>
420 <para>
421 These fields do not apply to <literal>local</literal> records.
422 </para>
424 <note>
425 <para>
426 Users sometimes wonder why host names are handled
427 in this seemingly complicated way, with two name resolutions
428 including a reverse lookup of the client's IP address. This
429 complicates use of the feature in case the client's reverse DNS
430 entry is not set up or yields some undesirable host name.
431 It is done primarily for efficiency: this way, a connection attempt
432 requires at most two resolver lookups, one reverse and one forward.
433 If there is a resolver problem with some address, it becomes only
434 that client's problem. A hypothetical alternative
435 implementation that only did forward lookups would have to
436 resolve every host name mentioned in
437 <filename>pg_hba.conf</filename> during every connection attempt.
438 That could be quite slow if many names are listed.
439 And if there is a resolver problem with one of the host names,
440 it becomes everyone's problem.
441 </para>
443 <para>
444 Also, a reverse lookup is necessary to implement the suffix
445 matching feature, because the actual client host name needs to
446 be known in order to match it against the pattern.
447 </para>
449 <para>
450 Note that this behavior is consistent with other popular
451 implementations of host name-based access control, such as the
452 Apache HTTP Server and TCP Wrappers.
453 </para>
454 </note>
455 </listitem>
456 </varlistentry>
458 <varlistentry>
459 <term><replaceable>IP-address</replaceable></term>
460 <term><replaceable>IP-mask</replaceable></term>
461 <listitem>
462 <para>
463 These two fields can be used as an alternative to the
464 <replaceable>IP-address</replaceable><literal>/</literal><replaceable>mask-length</replaceable>
465 notation. Instead of
466 specifying the mask length, the actual mask is specified in a
467 separate column. For example, <literal>255.0.0.0</literal> represents an IPv4
468 CIDR mask length of 8, and <literal>255.255.255.255</literal> represents a
469 CIDR mask length of 32.
470 </para>
472 <para>
473 These fields do not apply to <literal>local</literal> records.
474 </para>
475 </listitem>
476 </varlistentry>
478 <varlistentry>
479 <term><replaceable>auth-method</replaceable></term>
480 <listitem>
481 <para>
482 Specifies the authentication method to use when a connection matches
483 this record. The possible choices are summarized here; details
484 are in <xref linkend="auth-methods"/>. All the options
485 are lower case and treated case sensitively, so even acronyms like
486 <literal>ldap</literal> must be specified as lower case.
488 <variablelist>
489 <varlistentry>
490 <term><literal>trust</literal></term>
491 <listitem>
492 <para>
493 Allow the connection unconditionally. This method
494 allows anyone that can connect to the
495 <productname>PostgreSQL</productname> database server to login as
496 any <productname>PostgreSQL</productname> user they wish,
497 without the need for a password or any other authentication. See <xref
498 linkend="auth-trust"/> for details.
499 </para>
500 </listitem>
501 </varlistentry>
503 <varlistentry>
504 <term><literal>reject</literal></term>
505 <listitem>
506 <para>
507 Reject the connection unconditionally. This is useful for
508 <quote>filtering out</quote> certain hosts from a group, for example a
509 <literal>reject</literal> line could block a specific host from connecting,
510 while a later line allows the remaining hosts in a specific
511 network to connect.
512 </para>
513 </listitem>
514 </varlistentry>
516 <varlistentry>
517 <term><literal>scram-sha-256</literal></term>
518 <listitem>
519 <para>
520 Perform SCRAM-SHA-256 authentication to verify the user's
521 password. See <xref linkend="auth-password"/> for details.
522 </para>
523 </listitem>
524 </varlistentry>
526 <varlistentry>
527 <term><literal>md5</literal></term>
528 <listitem>
529 <para>
530 Perform SCRAM-SHA-256 or MD5 authentication to verify the
531 user's password. See <xref linkend="auth-password"/>
532 for details.
533 </para>
534 <warning>
535 <para>
536 Support for MD5-encrypted passwords is deprecated and will be
537 removed in a future release of
538 <productname>PostgreSQL</productname>. Refer to
539 <xref linkend="auth-password"/> for details about migrating to
540 another password type.
541 </para>
542 </warning>
543 </listitem>
544 </varlistentry>
546 <varlistentry>
547 <term><literal>password</literal></term>
548 <listitem>
549 <para>
550 Require the client to supply an unencrypted password for
551 authentication.
552 Since the password is sent in clear text over the
553 network, this should not be used on untrusted networks.
554 See <xref linkend="auth-password"/> for details.
555 </para>
556 </listitem>
557 </varlistentry>
559 <varlistentry>
560 <term><literal>gss</literal></term>
561 <listitem>
562 <para>
563 Use GSSAPI to authenticate the user. This is only
564 available for TCP/IP connections. See <xref
565 linkend="gssapi-auth"/> for details. It can be used in conjunction
566 with GSSAPI encryption.
567 </para>
568 </listitem>
569 </varlistentry>
571 <varlistentry>
572 <term><literal>sspi</literal></term>
573 <listitem>
574 <para>
575 Use SSPI to authenticate the user. This is only
576 available on Windows. See <xref
577 linkend="sspi-auth"/> for details.
578 </para>
579 </listitem>
580 </varlistentry>
582 <varlistentry>
583 <term><literal>ident</literal></term>
584 <listitem>
585 <para>
586 Obtain the operating system user name of the client
587 by contacting the ident server on the client
588 and check if it matches the requested database user name.
589 Ident authentication can only be used on TCP/IP
590 connections. When specified for local connections, peer
591 authentication will be used instead.
592 See <xref linkend="auth-ident"/> for details.
593 </para>
594 </listitem>
595 </varlistentry>
597 <varlistentry>
598 <term><literal>peer</literal></term>
599 <listitem>
600 <para>
601 Obtain the client's operating system user name from the operating
602 system and check if it matches the requested database user name.
603 This is only available for local connections.
604 See <xref linkend="auth-peer"/> for details.
605 </para>
606 </listitem>
607 </varlistentry>
609 <varlistentry>
610 <term><literal>ldap</literal></term>
611 <listitem>
612 <para>
613 Authenticate using an <acronym>LDAP</acronym> server. See <xref
614 linkend="auth-ldap"/> for details.
615 </para>
616 </listitem>
617 </varlistentry>
619 <varlistentry>
620 <term><literal>radius</literal></term>
621 <listitem>
622 <para>
623 Authenticate using a RADIUS server. See <xref
624 linkend="auth-radius"/> for details.
625 </para>
626 </listitem>
627 </varlistentry>
629 <varlistentry>
630 <term><literal>cert</literal></term>
631 <listitem>
632 <para>
633 Authenticate using SSL client certificates. See
634 <xref linkend="auth-cert"/> for details.
635 </para>
636 </listitem>
637 </varlistentry>
639 <varlistentry>
640 <term><literal>pam</literal></term>
641 <listitem>
642 <para>
643 Authenticate using the Pluggable Authentication Modules
644 (PAM) service provided by the operating system. See <xref
645 linkend="auth-pam"/> for details.
646 </para>
647 </listitem>
648 </varlistentry>
650 <varlistentry>
651 <term><literal>bsd</literal></term>
652 <listitem>
653 <para>
654 Authenticate using the BSD Authentication service provided by the
655 operating system. See <xref linkend="auth-bsd"/> for details.
656 </para>
657 </listitem>
658 </varlistentry>
659 </variablelist>
661 </para>
662 </listitem>
663 </varlistentry>
665 <varlistentry>
666 <term><replaceable>auth-options</replaceable></term>
667 <listitem>
668 <para>
669 After the <replaceable>auth-method</replaceable> field, there can be field(s) of
670 the form <replaceable>name</replaceable><literal>=</literal><replaceable>value</replaceable> that
671 specify options for the authentication method. Details about which
672 options are available for which authentication methods appear below.
673 </para>
675 <para>
676 In addition to the method-specific options listed below, there is a
677 method-independent authentication option <literal>clientcert</literal>, which
678 can be specified in any <literal>hostssl</literal> record.
679 This option can be set to <literal>verify-ca</literal> or
680 <literal>verify-full</literal>. Both options require the client
681 to present a valid (trusted) SSL certificate, while
682 <literal>verify-full</literal> additionally enforces that the
683 <literal>cn</literal> (Common Name) in the certificate matches
684 the username or an applicable mapping.
685 This behavior is similar to the <literal>cert</literal> authentication
686 method (see <xref linkend="auth-cert"/>) but enables pairing
687 the verification of client certificates with any authentication
688 method that supports <literal>hostssl</literal> entries.
689 </para>
690 <para>
691 On any record using client certificate authentication (i.e. one
692 using the <literal>cert</literal> authentication method or one
693 using the <literal>clientcert</literal> option), you can specify
694 which part of the client certificate credentials to match using
695 the <literal>clientname</literal> option. This option can have one
696 of two values. If you specify <literal>clientname=CN</literal>, which
697 is the default, the username is matched against the certificate's
698 <literal>Common Name (CN)</literal>. If instead you specify
699 <literal>clientname=DN</literal> the username is matched against the
700 entire <literal>Distinguished Name (DN)</literal> of the certificate.
701 This option is probably best used in conjunction with a username map.
702 The comparison is done with the <literal>DN</literal> in
703 <ulink url="https://datatracker.ietf.org/doc/html/rfc2253">RFC 2253</ulink>
704 format. To see the <literal>DN</literal> of a client certificate
705 in this format, do
706 <programlisting>
707 openssl x509 -in myclient.crt -noout -subject -nameopt RFC2253 | sed "s/^subject=//"
708 </programlisting>
709 Care needs to be taken when using this option, especially when using
710 regular expression matching against the <literal>DN</literal>.
711 </para>
712 </listitem>
713 </varlistentry>
715 <varlistentry>
716 <term><literal>include</literal></term>
717 <listitem>
718 <para>
719 This line will be replaced by the contents of the given file.
720 </para>
721 </listitem>
722 </varlistentry>
724 <varlistentry>
725 <term><literal>include_if_exists</literal></term>
726 <listitem>
727 <para>
728 This line will be replaced by the content of the given file if the
729 file exists. Otherwise, a message is logged to indicate that the file
730 has been skipped.
731 </para>
732 </listitem>
733 </varlistentry>
735 <varlistentry>
736 <term><literal>include_dir</literal></term>
737 <listitem>
738 <para>
739 This line will be replaced by the contents of all the files found in
740 the directory, if they don't start with a <literal>.</literal> and end
741 with <literal>.conf</literal>, processed in file name order (according
742 to C locale rules, i.e., numbers before letters, and uppercase letters
743 before lowercase ones).
744 </para>
745 </listitem>
746 </varlistentry>
747 </variablelist>
748 </para>
750 <para>
751 Files included by <literal>@</literal> constructs are read as lists of names,
752 which can be separated by either whitespace or commas. Comments are
753 introduced by <literal>#</literal>, just as in
754 <filename>pg_hba.conf</filename>, and nested <literal>@</literal> constructs are
755 allowed. Unless the file name following <literal>@</literal> is an absolute
756 path, it is taken to be relative to the directory containing the
757 referencing file.
758 </para>
760 <para>
761 Since the <filename>pg_hba.conf</filename> records are examined
762 sequentially for each connection attempt, the order of the records is
763 significant. Typically, earlier records will have tight connection
764 match parameters and weaker authentication methods, while later
765 records will have looser match parameters and stronger authentication
766 methods. For example, one might wish to use <literal>trust</literal>
767 authentication for local TCP/IP connections but require a password for
768 remote TCP/IP connections. In this case a record specifying
769 <literal>trust</literal> authentication for connections from 127.0.0.1 would
770 appear before a record specifying password authentication for a wider
771 range of allowed client IP addresses.
772 </para>
774 <tip>
775 <para>
776 To connect to a particular database, a user must not only pass the
777 <filename>pg_hba.conf</filename> checks, but must have the
778 <literal>CONNECT</literal> privilege for the database. If you wish to
779 restrict which users can connect to which databases, it's usually
780 easier to control this by granting/revoking <literal>CONNECT</literal> privilege
781 than to put the rules in <filename>pg_hba.conf</filename> entries.
782 </para>
783 </tip>
785 <para>
786 Some examples of <filename>pg_hba.conf</filename> entries are shown in
787 <xref linkend="example-pg-hba.conf"/>. See the next section for details on the
788 different authentication methods.
789 </para>
791 <example id="example-pg-hba.conf">
792 <title>Example <filename>pg_hba.conf</filename> Entries</title>
793 <programlisting>
794 # Allow any user on the local system to connect to any database with
795 # any database user name using Unix-domain sockets (the default for local
796 # connections).
798 # TYPE DATABASE USER ADDRESS METHOD
799 local all all trust
801 # The same using local loopback TCP/IP connections.
803 # TYPE DATABASE USER ADDRESS METHOD
804 host all all 127.0.0.1/32 trust
806 # The same as the previous line, but using a separate netmask column
808 # TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
809 host all all 127.0.0.1 255.255.255.255 trust
811 # The same over IPv6.
813 # TYPE DATABASE USER ADDRESS METHOD
814 host all all ::1/128 trust
816 # The same using a host name (would typically cover both IPv4 and IPv6).
818 # TYPE DATABASE USER ADDRESS METHOD
819 host all all localhost trust
821 # The same using a regular expression for DATABASE, that allows connection
822 # to any databases with a name beginning with "db" and finishing with a
823 # number using two to four digits (like "db1234" or "db12").
825 # TYPE DATABASE USER ADDRESS METHOD
826 host "/^db\d{2,4}$" all localhost trust
828 # Allow any user from any host with IP address 192.168.93.x to connect
829 # to database "postgres" as the same user name that ident reports for
830 # the connection (typically the operating system user name).
832 # TYPE DATABASE USER ADDRESS METHOD
833 host postgres all 192.168.93.0/24 ident
835 # Allow any user from host 192.168.12.10 to connect to database
836 # "postgres" if the user's password is correctly supplied.
838 # TYPE DATABASE USER ADDRESS METHOD
839 host postgres all 192.168.12.10/32 scram-sha-256
841 # Allow any user from hosts in the example.com domain to connect to
842 # any database if the user's password is correctly supplied.
844 # Require SCRAM authentication for most users, but make an exception
845 # for user 'mike', who uses an older client that doesn't support SCRAM
846 # authentication.
848 # TYPE DATABASE USER ADDRESS METHOD
849 host all mike .example.com md5
850 host all all .example.com scram-sha-256
852 # In the absence of preceding "host" lines, these three lines will
853 # reject all connections from 192.168.54.1 (since that entry will be
854 # matched first), but allow GSSAPI-encrypted connections from anywhere else
855 # on the Internet. The zero mask causes no bits of the host IP address to
856 # be considered, so it matches any host. Unencrypted GSSAPI connections
857 # (which "fall through" to the third line since "hostgssenc" only matches
858 # encrypted GSSAPI connections) are allowed, but only from 192.168.12.10.
860 # TYPE DATABASE USER ADDRESS METHOD
861 host all all 192.168.54.1/32 reject
862 hostgssenc all all 0.0.0.0/0 gss
863 host all all 192.168.12.10/32 gss
865 # Allow users from 192.168.x.x hosts to connect to any database, if
866 # they pass the ident check. If, for example, ident says the user is
867 # "bryanh" and he requests to connect as PostgreSQL user "guest1", the
868 # connection is allowed if there is an entry in pg_ident.conf for map
869 # "omicron" that says "bryanh" is allowed to connect as "guest1".
871 # TYPE DATABASE USER ADDRESS METHOD
872 host all all 192.168.0.0/16 ident map=omicron
874 # If these are the only four lines for local connections, they will
875 # allow local users to connect only to their own databases (databases
876 # with the same name as their database user name) except for users whose
877 # name end with "helpdesk", administrators and members of role "support",
878 # who can connect to all databases. The file $PGDATA/admins contains a
879 # list of names of administrators. Passwords are required in all cases.
881 # TYPE DATABASE USER ADDRESS METHOD
882 local sameuser all md5
883 local all /^.*helpdesk$ md5
884 local all @admins md5
885 local all +support md5
887 # The last two lines above can be combined into a single line:
888 local all @admins,+support md5
890 # The database column can also use lists and file names:
891 local db1,db2,@demodbs all md5
892 </programlisting>
893 </example>
894 </sect1>
896 <sect1 id="auth-username-maps">
897 <title>User Name Maps</title>
899 <indexterm zone="auth-username-maps">
900 <primary>User name maps</primary>
901 </indexterm>
903 <para>
904 When using an external authentication system such as Ident or GSSAPI,
905 the name of the operating system user that initiated the connection
906 might not be the same as the database user (role) that is to be used.
907 In this case, a user name map can be applied to map the operating system
908 user name to a database user. To use user name mapping, specify
909 <literal>map</literal>=<replaceable>map-name</replaceable>
910 in the options field in <filename>pg_hba.conf</filename>. This option is
911 supported for all authentication methods that receive external user names.
912 Since different mappings might be needed for different connections,
913 the name of the map to be used is specified in the
914 <replaceable>map-name</replaceable> parameter in <filename>pg_hba.conf</filename>
915 to indicate which map to use for each individual connection.
916 </para>
918 <para>
919 User name maps are defined in the ident map file, which by default is named
920 <filename>pg_ident.conf</filename><indexterm><primary>pg_ident.conf</primary></indexterm>
921 and is stored in the
922 cluster's data directory. (It is possible to place the map file
923 elsewhere, however; see the <xref linkend="guc-ident-file"/>
924 configuration parameter.)
925 The ident map file contains lines of the general forms:
926 <synopsis>
927 <replaceable>map-name</replaceable> <replaceable>system-username</replaceable> <replaceable>database-username</replaceable>
928 <replaceable>include</replaceable> <replaceable>file</replaceable>
929 <replaceable>include_if_exists</replaceable> <replaceable>file</replaceable>
930 <replaceable>include_dir</replaceable> <replaceable>directory</replaceable>
931 </synopsis>
932 Comments, whitespace and line continuations are handled in the same way as in
933 <filename>pg_hba.conf</filename>. The
934 <replaceable>map-name</replaceable> is an arbitrary name that will be used to
935 refer to this mapping in <filename>pg_hba.conf</filename>. The other
936 two fields specify an operating system user name and a matching
937 database user name. The same <replaceable>map-name</replaceable> can be
938 used repeatedly to specify multiple user-mappings within a single map.
939 </para>
940 <para>
941 As for <filename>pg_hba.conf</filename>, the lines in this file can
942 be include directives, following the same rules.
943 </para>
945 <para>
946 The <filename>pg_ident.conf</filename> file is read on start-up and
947 when the main server process receives a
948 <systemitem>SIGHUP</systemitem><indexterm><primary>SIGHUP</primary></indexterm>
949 signal. If you edit the file on an
950 active system, you will need to signal the postmaster
951 (using <literal>pg_ctl reload</literal>, calling the SQL function
952 <function>pg_reload_conf()</function>, or using <literal>kill
953 -HUP</literal>) to make it re-read the file.
954 </para>
956 <para>
957 The system view
958 <link linkend="view-pg-ident-file-mappings"><structname>pg_ident_file_mappings</structname></link>
959 can be helpful for pre-testing changes to the
960 <filename>pg_ident.conf</filename> file, or for diagnosing problems if
961 loading of the file did not have the desired effects. Rows in the view with
962 non-null <structfield>error</structfield> fields indicate problems in the
963 corresponding lines of the file.
964 </para>
966 <para>
967 There is no restriction regarding how many database users a given
968 operating system user can correspond to, nor vice versa. Thus, entries
969 in a map should be thought of as meaning <quote>this operating system
970 user is allowed to connect as this database user</quote>, rather than
971 implying that they are equivalent. The connection will be allowed if
972 there is any map entry that pairs the user name obtained from the
973 external authentication system with the database user name that the
974 user has requested to connect as. The value <literal>all</literal>
975 can be used as the <replaceable>database-username</replaceable> to specify
976 that if the <replaceable>system-username</replaceable> matches, then this
977 user is allowed to log in as any of the existing database users. Quoting
978 <literal>all</literal> makes the keyword lose its special meaning.
979 </para>
980 <para>
981 If the <replaceable>database-username</replaceable> begins with a
982 <literal>+</literal> character, then the operating system user can login as
983 any user belonging to that role, similarly to how user names beginning with
984 <literal>+</literal> are treated in <literal>pg_hba.conf</literal>.
985 Thus, a <literal>+</literal> mark means <quote>match any of the roles that
986 are directly or indirectly members of this role</quote>, while a name
987 without a <literal>+</literal> mark matches only that specific role. Quoting
988 a username starting with a <literal>+</literal> makes the
989 <literal>+</literal> lose its special meaning.
990 </para>
991 <para>
992 If the <replaceable>system-username</replaceable> field starts with a slash (<literal>/</literal>),
993 the remainder of the field is treated as a regular expression.
994 (See <xref linkend="posix-syntax-details"/> for details of
995 <productname>PostgreSQL</productname>'s regular expression syntax.) The regular
996 expression can include a single capture, or parenthesized subexpression,
997 which can then be referenced in the <replaceable>database-username</replaceable>
998 field as <literal>\1</literal> (backslash-one). This allows the mapping of
999 multiple user names in a single line, which is particularly useful for
1000 simple syntax substitutions. For example, these entries
1001 <programlisting>
1002 mymap /^(.*)@mydomain\.com$ \1
1003 mymap /^(.*)@otherdomain\.com$ guest
1004 </programlisting>
1005 will remove the domain part for users with system user names that end with
1006 <literal>@mydomain.com</literal>, and allow any user whose system name ends with
1007 <literal>@otherdomain.com</literal> to log in as <literal>guest</literal>.
1008 Quoting a <replaceable>database-username</replaceable> containing
1009 <literal>\1</literal> <emphasis>does not</emphasis> make
1010 <literal>\1</literal> lose its special meaning.
1011 </para>
1012 <para>
1013 If the <replaceable>database-username</replaceable> field starts with
1014 a slash (<literal>/</literal>), the remainder of the field is treated
1015 as a regular expression (see <xref linkend="posix-syntax-details"/>
1016 for details of <productname>PostgreSQL</productname>'s regular
1017 expression syntax). It is not possible to use <literal>\1</literal>
1018 to use a capture from regular expression on
1019 <replaceable>system-username</replaceable> for a regular expression
1020 on <replaceable>database-username</replaceable>.
1021 </para>
1023 <tip>
1024 <para>
1025 Keep in mind that by default, a regular expression can match just part of
1026 a string. It's usually wise to use <literal>^</literal> and <literal>$</literal>, as
1027 shown in the above example, to force the match to be to the entire
1028 system user name.
1029 </para>
1030 </tip>
1032 <para>
1033 A <filename>pg_ident.conf</filename> file that could be used in
1034 conjunction with the <filename>pg_hba.conf</filename> file in <xref
1035 linkend="example-pg-hba.conf"/> is shown in <xref
1036 linkend="example-pg-ident.conf"/>. In this example, anyone
1037 logged in to a machine on the 192.168 network that does not have the
1038 operating system user name <literal>bryanh</literal>, <literal>ann</literal>, or
1039 <literal>robert</literal> would not be granted access. Unix user
1040 <literal>robert</literal> would only be allowed access when he tries to
1041 connect as <productname>PostgreSQL</productname> user <literal>bob</literal>, not
1042 as <literal>robert</literal> or anyone else. <literal>ann</literal> would
1043 only be allowed to connect as <literal>ann</literal>. User
1044 <literal>bryanh</literal> would be allowed to connect as either
1045 <literal>bryanh</literal> or as <literal>guest1</literal>.
1046 </para>
1048 <example id="example-pg-ident.conf">
1049 <title>An Example <filename>pg_ident.conf</filename> File</title>
1050 <programlisting>
1051 # MAPNAME SYSTEM-USERNAME PG-USERNAME
1053 omicron bryanh bryanh
1054 omicron ann ann
1055 # bob has user name robert on these machines
1056 omicron robert bob
1057 # bryanh can also connect as guest1
1058 omicron bryanh guest1
1059 </programlisting>
1060 </example>
1061 </sect1>
1063 <sect1 id="auth-methods">
1064 <title>Authentication Methods</title>
1066 <para>
1067 <productname>PostgreSQL</productname> provides various methods for
1068 authenticating users:
1070 <itemizedlist>
1071 <listitem>
1072 <para>
1073 <link linkend="auth-trust">Trust authentication</link>, which
1074 simply trusts that users are who they say they are.
1075 </para>
1076 </listitem>
1077 <listitem>
1078 <para>
1079 <link linkend="auth-password">Password authentication</link>, which
1080 requires that users send a password.
1081 </para>
1082 </listitem>
1083 <listitem>
1084 <para>
1085 <link linkend="gssapi-auth">GSSAPI authentication</link>, which
1086 relies on a GSSAPI-compatible security library. Typically this is
1087 used to access an authentication server such as a Kerberos or
1088 Microsoft Active Directory server.
1089 </para>
1090 </listitem>
1091 <listitem>
1092 <para>
1093 <link linkend="sspi-auth">SSPI authentication</link>, which
1094 uses a Windows-specific protocol similar to GSSAPI.
1095 </para>
1096 </listitem>
1097 <listitem>
1098 <para>
1099 <link linkend="auth-ident">Ident authentication</link>, which
1100 relies on an <quote>Identification Protocol</quote>
1101 (<ulink url="https://datatracker.ietf.org/doc/html/rfc1413">RFC 1413</ulink>)
1102 service on the client's machine. (On local Unix-socket connections,
1103 this is treated as peer authentication.)
1104 </para>
1105 </listitem>
1106 <listitem>
1107 <para>
1108 <link linkend="auth-peer">Peer authentication</link>, which
1109 relies on operating system facilities to identify the process at the
1110 other end of a local connection. This is not supported for remote
1111 connections.
1112 </para>
1113 </listitem>
1114 <listitem>
1115 <para>
1116 <link linkend="auth-ldap">LDAP authentication</link>, which
1117 relies on an LDAP authentication server.
1118 </para>
1119 </listitem>
1120 <listitem>
1121 <para>
1122 <link linkend="auth-radius">RADIUS authentication</link>, which
1123 relies on a RADIUS authentication server.
1124 </para>
1125 </listitem>
1126 <listitem>
1127 <para>
1128 <link linkend="auth-cert">Certificate authentication</link>, which
1129 requires an SSL connection and authenticates users by checking the
1130 SSL certificate they send.
1131 </para>
1132 </listitem>
1133 <listitem>
1134 <para>
1135 <link linkend="auth-pam">PAM authentication</link>, which
1136 relies on a PAM (Pluggable Authentication Modules) library.
1137 </para>
1138 </listitem>
1139 <listitem>
1140 <para>
1141 <link linkend="auth-bsd">BSD authentication</link>, which
1142 relies on the BSD Authentication framework (currently available
1143 only on OpenBSD).
1144 </para>
1145 </listitem>
1146 </itemizedlist>
1147 </para>
1149 <para>
1150 Peer authentication is usually recommendable for local connections,
1151 though trust authentication might be sufficient in some circumstances.
1152 Password authentication is the easiest choice for remote connections.
1153 All the other options require some kind of external security
1154 infrastructure (usually an authentication server or a certificate
1155 authority for issuing SSL certificates), or are platform-specific.
1156 </para>
1158 <para>
1159 The following sections describe each of these authentication methods
1160 in more detail.
1161 </para>
1162 </sect1>
1164 <sect1 id="auth-trust">
1165 <title>Trust Authentication</title>
1167 <para>
1168 When <literal>trust</literal> authentication is specified,
1169 <productname>PostgreSQL</productname> assumes that anyone who can
1170 connect to the server is authorized to access the database with
1171 whatever database user name they specify (even superuser names).
1172 Of course, restrictions made in the <literal>database</literal> and
1173 <literal>user</literal> columns still apply.
1174 This method should only be used when there is adequate
1175 operating-system-level protection on connections to the server.
1176 </para>
1178 <para>
1179 <literal>trust</literal> authentication is appropriate and very
1180 convenient for local connections on a single-user workstation. It
1181 is usually <emphasis>not</emphasis> appropriate by itself on a multiuser
1182 machine. However, you might be able to use <literal>trust</literal> even
1183 on a multiuser machine, if you restrict access to the server's
1184 Unix-domain socket file using file-system permissions. To do this, set the
1185 <varname>unix_socket_permissions</varname> (and possibly
1186 <varname>unix_socket_group</varname>) configuration parameters as
1187 described in <xref linkend="runtime-config-connection"/>. Or you
1188 could set the <varname>unix_socket_directories</varname>
1189 configuration parameter to place the socket file in a suitably
1190 restricted directory.
1191 </para>
1193 <para>
1194 Setting file-system permissions only helps for Unix-socket connections.
1195 Local TCP/IP connections are not restricted by file-system permissions.
1196 Therefore, if you want to use file-system permissions for local security,
1197 remove the <literal>host ... 127.0.0.1 ...</literal> line from
1198 <filename>pg_hba.conf</filename>, or change it to a
1199 non-<literal>trust</literal> authentication method.
1200 </para>
1202 <para>
1203 <literal>trust</literal> authentication is only suitable for TCP/IP connections
1204 if you trust every user on every machine that is allowed to connect
1205 to the server by the <filename>pg_hba.conf</filename> lines that specify
1206 <literal>trust</literal>. It is seldom reasonable to use <literal>trust</literal>
1207 for any TCP/IP connections other than those from <systemitem>localhost</systemitem> (127.0.0.1).
1208 </para>
1210 </sect1>
1212 <sect1 id="auth-password">
1213 <title>Password Authentication</title>
1215 <indexterm>
1216 <primary>MD5</primary>
1217 </indexterm>
1218 <indexterm>
1219 <primary>SCRAM</primary>
1220 </indexterm>
1221 <indexterm>
1222 <primary>password</primary>
1223 <secondary>authentication</secondary>
1224 </indexterm>
1226 <para>
1227 There are several password-based authentication methods. These methods
1228 operate similarly but differ in how the users' passwords are stored on the
1229 server and how the password provided by a client is sent across the
1230 connection.
1231 </para>
1233 <variablelist>
1234 <varlistentry>
1235 <term><literal>scram-sha-256</literal></term>
1236 <listitem>
1237 <para>
1238 The method <literal>scram-sha-256</literal> performs SCRAM-SHA-256
1239 authentication, as described in
1240 <ulink url="https://datatracker.ietf.org/doc/html/rfc7677">RFC 7677</ulink>. It
1241 is a challenge-response scheme that prevents password sniffing on
1242 untrusted connections and supports storing passwords on the server in a
1243 cryptographically hashed form that is thought to be secure.
1244 </para>
1246 <para>
1247 This is the most secure of the currently provided methods, but it is
1248 not supported by older client libraries.
1249 </para>
1250 </listitem>
1251 </varlistentry>
1253 <varlistentry>
1254 <term><literal>md5</literal></term>
1255 <listitem>
1256 <para>
1257 The method <literal>md5</literal> uses a custom less secure challenge-response
1258 mechanism. It prevents password sniffing and avoids storing passwords
1259 on the server in plain text but provides no protection if an attacker
1260 manages to steal the password hash from the server. Also, the MD5 hash
1261 algorithm is nowadays no longer considered secure against determined
1262 attacks.
1263 </para>
1265 <para>
1266 To ease transition from the <literal>md5</literal> method to the newer
1267 SCRAM method, if <literal>md5</literal> is specified as a method
1268 in <filename>pg_hba.conf</filename> but the user's password on the
1269 server is encrypted for SCRAM (see below), then SCRAM-based
1270 authentication will automatically be chosen instead.
1271 </para>
1273 <warning>
1274 <para>
1275 Support for MD5-encrypted passwords is deprecated and will be removed
1276 in a future release of <productname>PostgreSQL</productname>. Refer to
1277 the text below for details about migrating to another password type.
1278 </para>
1279 </warning>
1280 </listitem>
1281 </varlistentry>
1283 <varlistentry>
1284 <term><literal>password</literal></term>
1285 <listitem>
1286 <para>
1287 The method <literal>password</literal> sends the password in clear-text and is
1288 therefore vulnerable to password <quote>sniffing</quote> attacks. It should
1289 always be avoided if possible. If the connection is protected by SSL
1290 encryption then <literal>password</literal> can be used safely, though.
1291 (Though SSL certificate authentication might be a better choice if one
1292 is depending on using SSL).
1293 </para>
1294 </listitem>
1295 </varlistentry>
1296 </variablelist>
1298 <para>
1299 <productname>PostgreSQL</productname> database passwords are
1300 separate from operating system user passwords. The password for
1301 each database user is stored in the <literal>pg_authid</literal> system
1302 catalog. Passwords can be managed with the SQL commands
1303 <xref linkend="sql-createrole"/> and
1304 <xref linkend="sql-alterrole"/>,
1305 e.g., <userinput>CREATE ROLE foo WITH LOGIN PASSWORD 'secret'</userinput>,
1306 or the <application>psql</application>
1307 command <literal>\password</literal>.
1308 If no password has been set up for a user, the stored password
1309 is null and password authentication will always fail for that user.
1310 </para>
1312 <para>
1313 The availability of the different password-based authentication methods
1314 depends on how a user's password on the server is encrypted (or hashed,
1315 more accurately). This is controlled by the configuration
1316 parameter <xref linkend="guc-password-encryption"/> at the time the
1317 password is set. If a password was encrypted using
1318 the <literal>scram-sha-256</literal> setting, then it can be used for the
1319 authentication methods <literal>scram-sha-256</literal>
1320 and <literal>password</literal> (but password transmission will be in
1321 plain text in the latter case). The authentication method
1322 specification <literal>md5</literal> will automatically switch to using
1323 the <literal>scram-sha-256</literal> method in this case, as explained
1324 above, so it will also work. If a password was encrypted using
1325 the <literal>md5</literal> setting, then it can be used only for
1326 the <literal>md5</literal> and <literal>password</literal> authentication
1327 method specifications (again, with the password transmitted in plain text
1328 in the latter case). (Previous PostgreSQL releases supported storing the
1329 password on the server in plain text. This is no longer possible.) To
1330 check the currently stored password hashes, see the system
1331 catalog <literal>pg_authid</literal>.
1332 </para>
1334 <para>
1335 To upgrade an existing installation from <literal>md5</literal>
1336 to <literal>scram-sha-256</literal>, after having ensured that all client
1337 libraries in use are new enough to support SCRAM,
1338 set <literal>password_encryption = 'scram-sha-256'</literal>
1339 in <filename>postgresql.conf</filename>, make all users set new passwords,
1340 and change the authentication method specifications
1341 in <filename>pg_hba.conf</filename> to <literal>scram-sha-256</literal>.
1342 </para>
1343 </sect1>
1345 <sect1 id="gssapi-auth">
1346 <title>GSSAPI Authentication</title>
1348 <indexterm zone="gssapi-auth">
1349 <primary>GSSAPI</primary>
1350 </indexterm>
1352 <para>
1353 <productname>GSSAPI</productname> is an industry-standard protocol
1354 for secure authentication defined in
1355 <ulink url="https://datatracker.ietf.org/doc/html/rfc2743">RFC 2743</ulink>.
1356 <productname>PostgreSQL</productname>
1357 supports <productname>GSSAPI</productname> for authentication,
1358 communications encryption, or both.
1359 <productname>GSSAPI</productname> provides automatic authentication
1360 (single sign-on) for systems that support it. The authentication itself is
1361 secure. If <productname>GSSAPI</productname> encryption
1362 or <acronym>SSL</acronym> encryption is
1363 used, the data sent along the database connection will be encrypted;
1364 otherwise, it will not.
1365 </para>
1367 <para>
1368 GSSAPI support has to be enabled when <productname>PostgreSQL</productname> is built;
1369 see <xref linkend="installation"/> for more information.
1370 </para>
1372 <para>
1373 When <productname>GSSAPI</productname> uses
1374 <productname>Kerberos</productname>, it uses a standard service
1375 principal (authentication identity) name in the format
1376 <literal><replaceable>servicename</replaceable>/<replaceable>hostname</replaceable>@<replaceable>realm</replaceable></literal>.
1377 The principal name used by a particular installation is not encoded in
1378 the <productname>PostgreSQL</productname> server in any way; rather it
1379 is specified in the <firstterm>keytab</firstterm> file that the server
1380 reads to determine its identity. If multiple principals are listed in
1381 the keytab file, the server will accept any one of them.
1382 The server's realm name is the preferred realm specified in the Kerberos
1383 configuration file(s) accessible to the server.
1384 </para>
1386 <para>
1387 When connecting, the client must know the principal name of the server
1388 it intends to connect to. The <replaceable>servicename</replaceable>
1389 part of the principal is ordinarily <literal>postgres</literal>,
1390 but another value can be selected via <application>libpq</application>'s
1391 <xref linkend="libpq-connect-krbsrvname"/> connection parameter.
1392 The <replaceable>hostname</replaceable> part is the fully qualified
1393 host name that <application>libpq</application> is told to connect to.
1394 The realm name is the preferred realm specified in the Kerberos
1395 configuration file(s) accessible to the client.
1396 </para>
1398 <para>
1399 The client will also have a principal name for its own identity
1400 (and it must have a valid ticket for this principal). To
1401 use <productname>GSSAPI</productname> for authentication, the client
1402 principal must be associated with
1403 a <productname>PostgreSQL</productname> database user name.
1404 The <filename>pg_ident.conf</filename> configuration file can be used
1405 to map principals to user names; for example,
1406 <literal>pgusername@realm</literal> could be mapped to just <literal>pgusername</literal>.
1407 Alternatively, you can use the full <literal>username@realm</literal> principal as
1408 the role name in <productname>PostgreSQL</productname> without any mapping.
1409 </para>
1411 <para>
1412 <productname>PostgreSQL</productname> also supports mapping
1413 client principals to user names by just stripping the realm from
1414 the principal. This method is supported for backwards compatibility and is
1415 strongly discouraged as it is then impossible to distinguish different users
1416 with the same user name but coming from different realms. To enable this,
1417 set <literal>include_realm</literal> to 0. For simple single-realm
1418 installations, doing that combined with setting the
1419 <literal>krb_realm</literal> parameter (which checks that the principal's realm
1420 matches exactly what is in the <literal>krb_realm</literal> parameter)
1421 is still secure; but this is a
1422 less capable approach compared to specifying an explicit mapping in
1423 <filename>pg_ident.conf</filename>.
1424 </para>
1426 <para>
1427 The location of the server's keytab file is specified by the <xref
1428 linkend="guc-krb-server-keyfile"/> configuration parameter.
1429 For security reasons, it is recommended to use a separate keytab
1430 just for the <productname>PostgreSQL</productname> server rather
1431 than allowing the server to read the system keytab file.
1432 Make sure that your server keytab file is readable (and preferably
1433 only readable, not writable) by the <productname>PostgreSQL</productname>
1434 server account. (See also <xref linkend="postgres-user"/>.)
1435 </para>
1437 <para>
1438 The keytab file is generated using the Kerberos software; see the
1439 Kerberos documentation for details. The following example shows
1440 doing this using the <application>kadmin</application> tool of
1441 MIT Kerberos:
1442 <screen>
1443 <prompt>kadmin% </prompt><userinput>addprinc -randkey postgres/server.my.domain.org</userinput>
1444 <prompt>kadmin% </prompt><userinput>ktadd -k krb5.keytab postgres/server.my.domain.org</userinput>
1445 </screen>
1446 </para>
1448 <para>
1449 The following authentication options are supported for
1450 the <productname>GSSAPI</productname> authentication method:
1451 <variablelist>
1452 <varlistentry>
1453 <term><literal>include_realm</literal></term>
1454 <listitem>
1455 <para>
1456 If set to 0, the realm name from the authenticated user principal is
1457 stripped off before being passed through the user name mapping
1458 (<xref linkend="auth-username-maps"/>). This is discouraged and is
1459 primarily available for backwards compatibility, as it is not secure
1460 in multi-realm environments unless <literal>krb_realm</literal> is
1461 also used. It is recommended to
1462 leave <literal>include_realm</literal> set to the default (1) and to
1463 provide an explicit mapping in <filename>pg_ident.conf</filename> to convert
1464 principal names to <productname>PostgreSQL</productname> user names.
1465 </para>
1466 </listitem>
1467 </varlistentry>
1469 <varlistentry>
1470 <term><literal>map</literal></term>
1471 <listitem>
1472 <para>
1473 Allows mapping from client principals to database user names. See
1474 <xref linkend="auth-username-maps"/> for details. For a GSSAPI/Kerberos
1475 principal, such as <literal>username@EXAMPLE.COM</literal> (or, less
1476 commonly, <literal>username/hostbased@EXAMPLE.COM</literal>), the
1477 user name used for mapping is
1478 <literal>username@EXAMPLE.COM</literal> (or
1479 <literal>username/hostbased@EXAMPLE.COM</literal>, respectively),
1480 unless <literal>include_realm</literal> has been set to 0, in which case
1481 <literal>username</literal> (or <literal>username/hostbased</literal>)
1482 is what is seen as the system user name when mapping.
1483 </para>
1484 </listitem>
1485 </varlistentry>
1487 <varlistentry>
1488 <term><literal>krb_realm</literal></term>
1489 <listitem>
1490 <para>
1491 Sets the realm to match user principal names against. If this parameter
1492 is set, only users of that realm will be accepted. If it is not set,
1493 users of any realm can connect, subject to whatever user name mapping
1494 is done.
1495 </para>
1496 </listitem>
1497 </varlistentry>
1498 </variablelist>
1499 </para>
1501 <para>
1502 In addition to these settings, which can be different for
1503 different <filename>pg_hba.conf</filename> entries, there is the
1504 server-wide <xref linkend="guc-krb-caseins-users"/> configuration
1505 parameter. If that is set to true, client principals are matched to
1506 user map entries case-insensitively. <literal>krb_realm</literal>, if
1507 set, is also matched case-insensitively.
1508 </para>
1509 </sect1>
1511 <sect1 id="sspi-auth">
1512 <title>SSPI Authentication</title>
1514 <indexterm zone="sspi-auth">
1515 <primary>SSPI</primary>
1516 </indexterm>
1518 <para>
1519 <productname>SSPI</productname> is a <productname>Windows</productname>
1520 technology for secure authentication with single sign-on.
1521 <productname>PostgreSQL</productname> will use SSPI in
1522 <literal>negotiate</literal> mode, which will use
1523 <productname>Kerberos</productname> when possible and automatically
1524 fall back to <productname>NTLM</productname> in other cases.
1525 <productname>SSPI</productname> and <productname>GSSAPI</productname>
1526 interoperate as clients and servers, e.g., an
1527 <productname>SSPI</productname> client can authenticate to an
1528 <productname>GSSAPI</productname> server. It is recommended to use
1529 <productname>SSPI</productname> on Windows clients and servers and
1530 <productname>GSSAPI</productname> on non-Windows platforms.
1531 </para>
1533 <para>
1534 When using <productname>Kerberos</productname> authentication,
1535 <productname>SSPI</productname> works the same way
1536 <productname>GSSAPI</productname> does; see <xref linkend="gssapi-auth"/>
1537 for details.
1538 </para>
1540 <para>
1541 The following configuration options are supported for <productname>SSPI</productname>:
1542 <variablelist>
1544 <varlistentry>
1545 <term><literal>include_realm</literal></term>
1546 <listitem>
1547 <para>
1548 If set to 0, the realm name from the authenticated user principal is
1549 stripped off before being passed through the user name mapping
1550 (<xref linkend="auth-username-maps"/>). This is discouraged and is
1551 primarily available for backwards compatibility, as it is not secure
1552 in multi-realm environments unless <literal>krb_realm</literal> is
1553 also used. It is recommended to
1554 leave <literal>include_realm</literal> set to the default (1) and to
1555 provide an explicit mapping in <filename>pg_ident.conf</filename> to convert
1556 principal names to <productname>PostgreSQL</productname> user names.
1557 </para>
1558 </listitem>
1559 </varlistentry>
1561 <varlistentry>
1562 <term><literal>compat_realm</literal></term>
1563 <listitem>
1564 <para>
1565 If set to 1, the domain's SAM-compatible name (also known as the
1566 NetBIOS name) is used for the <literal>include_realm</literal>
1567 option. This is the default. If set to 0, the true realm name from
1568 the Kerberos user principal name is used.
1569 </para>
1570 <para>
1571 Do not disable this option unless your server runs under a domain
1572 account (this includes virtual service accounts on a domain member
1573 system) and all clients authenticating through SSPI are also using
1574 domain accounts, or authentication will fail.
1575 </para>
1576 </listitem>
1577 </varlistentry>
1579 <varlistentry>
1580 <term><literal>upn_username</literal></term>
1581 <listitem>
1582 <para>
1583 If this option is enabled along with <literal>compat_realm</literal>,
1584 the user name from the Kerberos UPN is used for authentication. If
1585 it is disabled (the default), the SAM-compatible user name is used.
1586 By default, these two names are identical for new user accounts.
1587 </para>
1588 <para>
1589 Note that <application>libpq</application> uses the SAM-compatible name if no
1590 explicit user name is specified. If you use
1591 <application>libpq</application> or a driver based on it, you should
1592 leave this option disabled or explicitly specify user name in the
1593 connection string.
1594 </para>
1595 </listitem>
1596 </varlistentry>
1598 <varlistentry>
1599 <term><literal>map</literal></term>
1600 <listitem>
1601 <para>
1602 Allows for mapping between system and database user names. See
1603 <xref linkend="auth-username-maps"/> for details. For an SSPI/Kerberos
1604 principal, such as <literal>username@EXAMPLE.COM</literal> (or, less
1605 commonly, <literal>username/hostbased@EXAMPLE.COM</literal>), the
1606 user name used for mapping is
1607 <literal>username@EXAMPLE.COM</literal> (or
1608 <literal>username/hostbased@EXAMPLE.COM</literal>, respectively),
1609 unless <literal>include_realm</literal> has been set to 0, in which case
1610 <literal>username</literal> (or <literal>username/hostbased</literal>)
1611 is what is seen as the system user name when mapping.
1612 </para>
1613 </listitem>
1614 </varlistentry>
1616 <varlistentry>
1617 <term><literal>krb_realm</literal></term>
1618 <listitem>
1619 <para>
1620 Sets the realm to match user principal names against. If this parameter
1621 is set, only users of that realm will be accepted. If it is not set,
1622 users of any realm can connect, subject to whatever user name mapping
1623 is done.
1624 </para>
1625 </listitem>
1626 </varlistentry>
1627 </variablelist>
1628 </para>
1629 </sect1>
1631 <sect1 id="auth-ident">
1632 <title>Ident Authentication</title>
1634 <indexterm>
1635 <primary>ident</primary>
1636 </indexterm>
1638 <para>
1639 The ident authentication method works by obtaining the client's
1640 operating system user name from an ident server and using it as
1641 the allowed database user name (with an optional user name mapping).
1642 This is only supported on TCP/IP connections.
1643 </para>
1645 <note>
1646 <para>
1647 When ident is specified for a local (non-TCP/IP) connection,
1648 peer authentication (see <xref linkend="auth-peer"/>) will be
1649 used instead.
1650 </para>
1651 </note>
1653 <para>
1654 The following configuration options are supported for <literal>ident</literal>:
1655 <variablelist>
1656 <varlistentry>
1657 <term><literal>map</literal></term>
1658 <listitem>
1659 <para>
1660 Allows for mapping between system and database user names. See
1661 <xref linkend="auth-username-maps"/> for details.
1662 </para>
1663 </listitem>
1664 </varlistentry>
1665 </variablelist>
1666 </para>
1668 <para>
1669 The <quote>Identification Protocol</quote> is described in
1670 <ulink url="https://datatracker.ietf.org/doc/html/rfc1413">RFC 1413</ulink>.
1671 Virtually every Unix-like
1672 operating system ships with an ident server that listens on TCP
1673 port 113 by default. The basic functionality of an ident server
1674 is to answer questions like <quote>What user initiated the
1675 connection that goes out of your port <replaceable>X</replaceable>
1676 and connects to my port <replaceable>Y</replaceable>?</quote>.
1677 Since <productname>PostgreSQL</productname> knows both <replaceable>X</replaceable> and
1678 <replaceable>Y</replaceable> when a physical connection is established, it
1679 can interrogate the ident server on the host of the connecting
1680 client and can theoretically determine the operating system user
1681 for any given connection.
1682 </para>
1684 <para>
1685 The drawback of this procedure is that it depends on the integrity
1686 of the client: if the client machine is untrusted or compromised,
1687 an attacker could run just about any program on port 113 and
1688 return any user name they choose. This authentication method is
1689 therefore only appropriate for closed networks where each client
1690 machine is under tight control and where the database and system
1691 administrators operate in close contact. In other words, you must
1692 trust the machine running the ident server.
1693 Heed the warning:
1694 <blockquote>
1695 <attribution>RFC 1413</attribution>
1696 <para>
1697 The Identification Protocol is not intended as an authorization
1698 or access control protocol.
1699 </para>
1700 </blockquote>
1701 </para>
1703 <para>
1704 Some ident servers have a nonstandard option that causes the returned
1705 user name to be encrypted, using a key that only the originating
1706 machine's administrator knows. This option <emphasis>must not</emphasis> be
1707 used when using the ident server with <productname>PostgreSQL</productname>,
1708 since <productname>PostgreSQL</productname> does not have any way to decrypt the
1709 returned string to determine the actual user name.
1710 </para>
1711 </sect1>
1713 <sect1 id="auth-peer">
1714 <title>Peer Authentication</title>
1716 <indexterm>
1717 <primary>peer</primary>
1718 </indexterm>
1720 <para>
1721 The peer authentication method works by obtaining the client's
1722 operating system user name from the kernel and using it as the
1723 allowed database user name (with optional user name mapping). This
1724 method is only supported on local connections.
1725 </para>
1727 <para>
1728 The following configuration options are supported for <literal>peer</literal>:
1729 <variablelist>
1730 <varlistentry>
1731 <term><literal>map</literal></term>
1732 <listitem>
1733 <para>
1734 Allows for mapping between system and database user names. See
1735 <xref linkend="auth-username-maps"/> for details.
1736 </para>
1737 </listitem>
1738 </varlistentry>
1739 </variablelist>
1740 </para>
1742 <para>
1743 Peer authentication is only available on operating systems providing
1744 the <function>getpeereid()</function> function, the <symbol>SO_PEERCRED</symbol>
1745 socket parameter, or similar mechanisms. Currently that includes
1746 <systemitem class="osname">Linux</systemitem>,
1747 most flavors of <systemitem class="osname">BSD</systemitem> including
1748 <systemitem class="osname">macOS</systemitem>,
1749 and <systemitem class="osname">Solaris</systemitem>.
1750 </para>
1752 </sect1>
1754 <sect1 id="auth-ldap">
1755 <title>LDAP Authentication</title>
1757 <indexterm zone="auth-ldap">
1758 <primary>LDAP</primary>
1759 </indexterm>
1761 <para>
1762 This authentication method operates similarly to
1763 <literal>password</literal> except that it uses LDAP
1764 as the password verification method. LDAP is used only to validate
1765 the user name/password pairs. Therefore the user must already
1766 exist in the database before LDAP can be used for
1767 authentication.
1768 </para>
1770 <para>
1771 LDAP authentication can operate in two modes. In the first mode,
1772 which we will call the simple bind mode,
1773 the server will bind to the distinguished name constructed as
1774 <replaceable>prefix</replaceable> <replaceable>username</replaceable> <replaceable>suffix</replaceable>.
1775 Typically, the <replaceable>prefix</replaceable> parameter is used to specify
1776 <literal>cn=</literal>, or <replaceable>DOMAIN</replaceable><literal>\</literal> in an Active
1777 Directory environment. <replaceable>suffix</replaceable> is used to specify the
1778 remaining part of the DN in a non-Active Directory environment.
1779 </para>
1781 <para>
1782 In the second mode, which we will call the search+bind mode,
1783 the server first binds to the LDAP directory with
1784 a fixed user name and password, specified with <replaceable>ldapbinddn</replaceable>
1785 and <replaceable>ldapbindpasswd</replaceable>, and performs a search for the user trying
1786 to log in to the database. If no user and password is configured, an
1787 anonymous bind will be attempted to the directory. The search will be
1788 performed over the subtree at <replaceable>ldapbasedn</replaceable>, and will try to
1789 do an exact match of the attribute specified in
1790 <replaceable>ldapsearchattribute</replaceable>.
1791 Once the user has been found in
1792 this search, the server re-binds to the directory as
1793 this user, using the password specified by the client, to verify that the
1794 login is correct. This mode is the same as that used by LDAP authentication
1795 schemes in other software, such as Apache <literal>mod_authnz_ldap</literal> and <literal>pam_ldap</literal>.
1796 This method allows for significantly more flexibility
1797 in where the user objects are located in the directory, but will cause
1798 two additional requests to the LDAP server to be made.
1799 </para>
1801 <para>
1802 The following configuration options are used in both modes:
1803 <variablelist>
1804 <varlistentry>
1805 <term><literal>ldapserver</literal></term>
1806 <listitem>
1807 <para>
1808 Names or IP addresses of LDAP servers to connect to. Multiple
1809 servers may be specified, separated by spaces.
1810 </para>
1811 </listitem>
1812 </varlistentry>
1813 <varlistentry>
1814 <term><literal>ldapport</literal></term>
1815 <listitem>
1816 <para>
1817 Port number on LDAP server to connect to. If no port is specified,
1818 the LDAP library's default port setting will be used.
1819 </para>
1820 </listitem>
1821 </varlistentry>
1822 <varlistentry>
1823 <term><literal>ldapscheme</literal></term>
1824 <listitem>
1825 <para>
1826 Set to <literal>ldaps</literal> to use LDAPS. This is a non-standard
1827 way of using LDAP over SSL, supported by some LDAP server
1828 implementations. See also the <literal>ldaptls</literal> option for
1829 an alternative.
1830 </para>
1831 </listitem>
1832 </varlistentry>
1833 <varlistentry>
1834 <term><literal>ldaptls</literal></term>
1835 <listitem>
1836 <para>
1837 Set to 1 to make the connection between PostgreSQL and the LDAP server
1838 use TLS encryption. This uses the <literal>StartTLS</literal>
1839 operation per <ulink url="https://datatracker.ietf.org/doc/html/rfc4513">RFC 4513</ulink>.
1840 See also the <literal>ldapscheme</literal> option for an alternative.
1841 </para>
1842 </listitem>
1843 </varlistentry>
1844 </variablelist>
1845 </para>
1847 <para>
1848 Note that using <literal>ldapscheme</literal> or
1849 <literal>ldaptls</literal> only encrypts the traffic between the
1850 PostgreSQL server and the LDAP server. The connection between the
1851 PostgreSQL server and the PostgreSQL client will still be unencrypted
1852 unless SSL is used there as well.
1853 </para>
1855 <para>
1856 The following options are used in simple bind mode only:
1857 <variablelist>
1858 <varlistentry>
1859 <term><literal>ldapprefix</literal></term>
1860 <listitem>
1861 <para>
1862 String to prepend to the user name when forming the DN to bind as,
1863 when doing simple bind authentication.
1864 </para>
1865 </listitem>
1866 </varlistentry>
1867 <varlistentry>
1868 <term><literal>ldapsuffix</literal></term>
1869 <listitem>
1870 <para>
1871 String to append to the user name when forming the DN to bind as,
1872 when doing simple bind authentication.
1873 </para>
1874 </listitem>
1875 </varlistentry>
1876 </variablelist>
1877 </para>
1879 <para>
1880 The following options are used in search+bind mode only:
1881 <variablelist>
1882 <varlistentry>
1883 <term><literal>ldapbasedn</literal></term>
1884 <listitem>
1885 <para>
1886 Root DN to begin the search for the user in, when doing search+bind
1887 authentication.
1888 </para>
1889 </listitem>
1890 </varlistentry>
1891 <varlistentry>
1892 <term><literal>ldapbinddn</literal></term>
1893 <listitem>
1894 <para>
1895 DN of user to bind to the directory with to perform the search when
1896 doing search+bind authentication.
1897 </para>
1898 </listitem>
1899 </varlistentry>
1900 <varlistentry>
1901 <term><literal>ldapbindpasswd</literal></term>
1902 <listitem>
1903 <para>
1904 Password for user to bind to the directory with to perform the search
1905 when doing search+bind authentication.
1906 </para>
1907 </listitem>
1908 </varlistentry>
1909 <varlistentry>
1910 <term><literal>ldapsearchattribute</literal></term>
1911 <listitem>
1912 <para>
1913 Attribute to match against the user name in the search when doing
1914 search+bind authentication. If no attribute is specified, the
1915 <literal>uid</literal> attribute will be used.
1916 </para>
1917 </listitem>
1918 </varlistentry>
1919 <varlistentry>
1920 <term><literal>ldapsearchfilter</literal></term>
1921 <listitem>
1922 <para>
1923 The search filter to use when doing search+bind authentication.
1924 Occurrences of <literal>$username</literal> will be replaced with the
1925 user name. This allows for more flexible search filters than
1926 <literal>ldapsearchattribute</literal>.
1927 </para>
1928 </listitem>
1929 </varlistentry>
1930 </variablelist>
1931 </para>
1933 <para>
1934 The following option may be used as an alternative way to write some of the
1935 above LDAP options in a more compact and standard form:
1936 <variablelist>
1937 <varlistentry>
1938 <term><literal>ldapurl</literal></term>
1939 <listitem>
1940 <para>
1941 An <ulink url="https://datatracker.ietf.org/doc/html/rfc4516">RFC 4516</ulink>
1942 LDAP URL. The format is
1943 <synopsis>
1944 ldap[s]://<replaceable>host</replaceable>[:<replaceable>port</replaceable>]/<replaceable>basedn</replaceable>[?[<replaceable>attribute</replaceable>][?[<replaceable>scope</replaceable>][?[<replaceable>filter</replaceable>]]]]
1945 </synopsis>
1946 <replaceable>scope</replaceable> must be one
1947 of <literal>base</literal>, <literal>one</literal>, <literal>sub</literal>,
1948 typically the last. (The default is <literal>base</literal>, which
1949 is normally not useful in this application.) <replaceable>attribute</replaceable> can
1950 nominate a single attribute, in which case it is used as a value for
1951 <literal>ldapsearchattribute</literal>. If
1952 <replaceable>attribute</replaceable> is empty then
1953 <replaceable>filter</replaceable> can be used as a value for
1954 <literal>ldapsearchfilter</literal>.
1955 </para>
1957 <para>
1958 The URL scheme <literal>ldaps</literal> chooses the LDAPS method for
1959 making LDAP connections over SSL, equivalent to using
1960 <literal>ldapscheme=ldaps</literal>. To use encrypted LDAP
1961 connections using the <literal>StartTLS</literal> operation, use the
1962 normal URL scheme <literal>ldap</literal> and specify the
1963 <literal>ldaptls</literal> option in addition to
1964 <literal>ldapurl</literal>.
1965 </para>
1967 <para>
1968 For non-anonymous binds, <literal>ldapbinddn</literal>
1969 and <literal>ldapbindpasswd</literal> must be specified as separate
1970 options.
1971 </para>
1973 <para>
1974 LDAP URLs are currently only supported with
1975 <productname>OpenLDAP</productname>, not on Windows.
1976 </para>
1977 </listitem>
1978 </varlistentry>
1979 </variablelist>
1980 </para>
1982 <para>
1983 It is an error to mix configuration options for simple bind with options
1984 for search+bind. To use <literal>ldapurl</literal> in simple bind mode, the
1985 URL must not contain a <literal>basedn</literal> or query elements.
1986 </para>
1988 <para>
1989 When using search+bind mode, the search can be performed using a single
1990 attribute specified with <literal>ldapsearchattribute</literal>, or using
1991 a custom search filter specified with
1992 <literal>ldapsearchfilter</literal>.
1993 Specifying <literal>ldapsearchattribute=foo</literal> is equivalent to
1994 specifying <literal>ldapsearchfilter="(foo=$username)"</literal>. If neither
1995 option is specified the default is
1996 <literal>ldapsearchattribute=uid</literal>.
1997 </para>
1999 <para>
2000 If <productname>PostgreSQL</productname> was compiled with
2001 <productname>OpenLDAP</productname> as the LDAP client library, the
2002 <literal>ldapserver</literal> setting may be omitted. In that case, a
2003 list of host names and ports is looked up via
2004 <ulink url="https://datatracker.ietf.org/doc/html/rfc2782">RFC 2782</ulink> DNS SRV records.
2005 The name <literal>_ldap._tcp.DOMAIN</literal> is looked up, where
2006 <literal>DOMAIN</literal> is extracted from <literal>ldapbasedn</literal>.
2007 </para>
2009 <para>
2010 Here is an example for a simple-bind LDAP configuration:
2011 <programlisting>
2012 host ... ldap ldapserver=ldap.example.net ldapprefix="cn=" ldapsuffix=", dc=example, dc=net"
2013 </programlisting>
2014 When a connection to the database server as database
2015 user <literal>someuser</literal> is requested, PostgreSQL will attempt to
2016 bind to the LDAP server using the DN <literal>cn=someuser, dc=example,
2017 dc=net</literal> and the password provided by the client. If that connection
2018 succeeds, the database access is granted.
2019 </para>
2021 <para>
2022 Here is a different simple-bind configuration, which uses the LDAPS scheme
2023 and a custom port number, written as a URL:
2024 <programlisting>
2025 host ... ldap ldapurl="ldaps://ldap.example.net:49151" ldapprefix="cn=" ldapsuffix=", dc=example, dc=net"
2026 </programlisting>
2027 This is slightly more compact than specifying <literal>ldapserver</literal>,
2028 <literal>ldapscheme</literal>, and <literal>ldapport</literal> separately.
2029 </para>
2031 <para>
2032 Here is an example for a search+bind configuration:
2033 <programlisting>
2034 host ... ldap ldapserver=ldap.example.net ldapbasedn="dc=example, dc=net" ldapsearchattribute=uid
2035 </programlisting>
2036 When a connection to the database server as database
2037 user <literal>someuser</literal> is requested, PostgreSQL will attempt to
2038 bind anonymously (since <literal>ldapbinddn</literal> was not specified) to
2039 the LDAP server, perform a search for <literal>(uid=someuser)</literal>
2040 under the specified base DN. If an entry is found, it will then attempt to
2041 bind using that found information and the password supplied by the client.
2042 If that second bind succeeds, the database access is granted.
2043 </para>
2045 <para>
2046 Here is the same search+bind configuration written as a URL:
2047 <programlisting>
2048 host ... ldap ldapurl="ldap://ldap.example.net/dc=example,dc=net?uid?sub"
2049 </programlisting>
2050 Some other software that supports authentication against LDAP uses the
2051 same URL format, so it will be easier to share the configuration.
2052 </para>
2054 <para>
2055 Here is an example for a search+bind configuration that uses
2056 <literal>ldapsearchfilter</literal> instead of
2057 <literal>ldapsearchattribute</literal> to allow authentication by
2058 user ID or email address:
2059 <programlisting>
2060 host ... ldap ldapserver=ldap.example.net ldapbasedn="dc=example, dc=net" ldapsearchfilter="(|(uid=$username)(mail=$username))"
2061 </programlisting>
2062 </para>
2064 <para>
2065 Here is an example for a search+bind configuration that uses DNS SRV
2066 discovery to find the host name(s) and port(s) for the LDAP service for the
2067 domain name <literal>example.net</literal>:
2068 <programlisting>
2069 host ... ldap ldapbasedn="dc=example,dc=net"
2070 </programlisting>
2071 </para>
2073 <tip>
2074 <para>
2075 Since LDAP often uses commas and spaces to separate the different
2076 parts of a DN, it is often necessary to use double-quoted parameter
2077 values when configuring LDAP options, as shown in the examples.
2078 </para>
2079 </tip>
2081 </sect1>
2083 <sect1 id="auth-radius">
2084 <title>RADIUS Authentication</title>
2086 <indexterm zone="auth-radius">
2087 <primary>RADIUS</primary>
2088 </indexterm>
2090 <para>
2091 This authentication method operates similarly to
2092 <literal>password</literal> except that it uses RADIUS
2093 as the password verification method. RADIUS is used only to validate
2094 the user name/password pairs. Therefore the user must already
2095 exist in the database before RADIUS can be used for
2096 authentication.
2097 </para>
2099 <para>
2100 When using RADIUS authentication, an Access Request message will be sent
2101 to the configured RADIUS server. This request will be of type
2102 <literal>Authenticate Only</literal>, and include parameters for
2103 <literal>user name</literal>, <literal>password</literal> (encrypted) and
2104 <literal>NAS Identifier</literal>. The request will be encrypted using
2105 a secret shared with the server. The RADIUS server will respond to
2106 this request with either <literal>Access Accept</literal> or
2107 <literal>Access Reject</literal>. There is no support for RADIUS accounting.
2108 </para>
2110 <para>
2111 Multiple RADIUS servers can be specified, in which case they will
2112 be tried sequentially. If a negative response is received from
2113 a server, the authentication will fail. If no response is received,
2114 the next server in the list will be tried. To specify multiple
2115 servers, separate the server names with commas and surround the list
2116 with double quotes. If multiple servers are specified, the other
2117 RADIUS options can also be given as comma-separated lists, to provide
2118 individual values for each server. They can also be specified as
2119 a single value, in which case that value will apply to all servers.
2120 </para>
2122 <para>
2123 The following configuration options are supported for RADIUS:
2124 <variablelist>
2125 <varlistentry>
2126 <term><literal>radiusservers</literal></term>
2127 <listitem>
2128 <para>
2129 The DNS names or IP addresses of the RADIUS servers to connect to.
2130 This parameter is required.
2131 </para>
2132 </listitem>
2133 </varlistentry>
2135 <varlistentry>
2136 <term><literal>radiussecrets</literal></term>
2137 <listitem>
2138 <para>
2139 The shared secrets used when talking securely to the RADIUS
2140 servers. This must have exactly the same value on the PostgreSQL
2141 and RADIUS servers. It is recommended that this be a string of
2142 at least 16 characters. This parameter is required.
2143 <note>
2144 <para>
2145 The encryption vector used will only be cryptographically
2146 strong if <productname>PostgreSQL</productname> is built with support for
2147 <productname>OpenSSL</productname>. In other cases, the transmission to the
2148 RADIUS server should only be considered obfuscated, not secured, and
2149 external security measures should be applied if necessary.
2150 </para>
2151 </note>
2152 </para>
2153 </listitem>
2154 </varlistentry>
2156 <varlistentry>
2157 <term><literal>radiusports</literal></term>
2158 <listitem>
2159 <para>
2160 The port numbers to connect to on the RADIUS servers. If no port
2161 is specified, the default RADIUS port (<literal>1812</literal>)
2162 will be used.
2163 </para>
2164 </listitem>
2165 </varlistentry>
2167 <varlistentry>
2168 <term><literal>radiusidentifiers</literal></term>
2169 <listitem>
2170 <para>
2171 The strings to be used as <literal>NAS Identifier</literal> in the
2172 RADIUS requests. This parameter can be used, for example, to
2173 identify which database cluster the user is attempting to connect
2174 to, which can be useful for policy matching on
2175 the RADIUS server. If no identifier is specified, the default
2176 <literal>postgresql</literal> will be used.
2177 </para>
2178 </listitem>
2179 </varlistentry>
2181 </variablelist>
2182 </para>
2184 <para>
2185 If it is necessary to have a comma or whitespace in a RADIUS parameter
2186 value, that can be done by putting double quotes around the value, but
2187 it is tedious because two layers of double-quoting are now required.
2188 An example of putting whitespace into RADIUS secret strings is:
2189 <programlisting>
2190 host ... radius radiusservers="server1,server2" radiussecrets="""secret one"",""secret two"""
2191 </programlisting>
2192 </para>
2193 </sect1>
2195 <sect1 id="auth-cert">
2196 <title>Certificate Authentication</title>
2198 <indexterm zone="auth-cert">
2199 <primary>Certificate</primary>
2200 </indexterm>
2202 <para>
2203 This authentication method uses SSL client certificates to perform
2204 authentication. It is therefore only available for SSL connections;
2205 see <xref linkend="ssl-openssl-config"/> for SSL configuration instructions.
2206 When using this authentication method, the server will require that
2207 the client provide a valid, trusted certificate. No password prompt
2208 will be sent to the client. The <literal>cn</literal> (Common Name)
2209 attribute of the certificate
2210 will be compared to the requested database user name, and if they match
2211 the login will be allowed. User name mapping can be used to allow
2212 <literal>cn</literal> to be different from the database user name.
2213 </para>
2215 <para>
2216 The following configuration options are supported for SSL certificate
2217 authentication:
2218 <variablelist>
2219 <varlistentry>
2220 <term><literal>map</literal></term>
2221 <listitem>
2222 <para>
2223 Allows for mapping between system and database user names. See
2224 <xref linkend="auth-username-maps"/> for details.
2225 </para>
2226 </listitem>
2227 </varlistentry>
2228 </variablelist>
2229 </para>
2231 <para>
2232 It is redundant to use the <literal>clientcert</literal> option with
2233 <literal>cert</literal> authentication because <literal>cert</literal>
2234 authentication is effectively <literal>trust</literal> authentication
2235 with <literal>clientcert=verify-full</literal>.
2236 </para>
2237 </sect1>
2239 <sect1 id="auth-pam">
2240 <title>PAM Authentication</title>
2242 <indexterm zone="auth-pam">
2243 <primary>PAM</primary>
2244 </indexterm>
2246 <para>
2247 This authentication method operates similarly to
2248 <literal>password</literal> except that it uses PAM (Pluggable
2249 Authentication Modules) as the authentication mechanism. The
2250 default PAM service name is <literal>postgresql</literal>.
2251 PAM is used only to validate user name/password pairs and optionally the
2252 connected remote host name or IP address. Therefore the user must already
2253 exist in the database before PAM can be used for authentication. For more
2254 information about PAM, please read the
2255 <ulink url="https://www.kernel.org/pub/linux/libs/pam/">
2256 <productname>Linux-PAM</productname> Page</ulink>.
2257 </para>
2259 <para>
2260 The following configuration options are supported for PAM:
2261 <variablelist>
2262 <varlistentry>
2263 <term><literal>pamservice</literal></term>
2264 <listitem>
2265 <para>
2266 PAM service name.
2267 </para>
2268 </listitem>
2269 </varlistentry>
2270 <varlistentry>
2271 <term><literal>pam_use_hostname</literal></term>
2272 <listitem>
2273 <para>
2274 Determines whether the remote IP address or the host name is provided
2275 to PAM modules through the <symbol>PAM_RHOST</symbol> item. By
2276 default, the IP address is used. Set this option to 1 to use the
2277 resolved host name instead. Host name resolution can lead to login
2278 delays. (Most PAM configurations don't use this information, so it is
2279 only necessary to consider this setting if a PAM configuration was
2280 specifically created to make use of it.)
2281 </para>
2282 </listitem>
2283 </varlistentry>
2284 </variablelist>
2285 </para>
2287 <note>
2288 <para>
2289 If PAM is set up to read <filename>/etc/shadow</filename>, authentication
2290 will fail because the PostgreSQL server is started by a non-root
2291 user. However, this is not an issue when PAM is configured to use
2292 LDAP or other authentication methods.
2293 </para>
2294 </note>
2295 </sect1>
2297 <sect1 id="auth-bsd">
2298 <title>BSD Authentication</title>
2300 <indexterm zone="auth-bsd">
2301 <primary>BSD Authentication</primary>
2302 </indexterm>
2304 <para>
2305 This authentication method operates similarly to
2306 <literal>password</literal> except that it uses BSD Authentication
2307 to verify the password. BSD Authentication is used only
2308 to validate user name/password pairs. Therefore the user's role must
2309 already exist in the database before BSD Authentication can be used
2310 for authentication. The BSD Authentication framework is currently
2311 only available on OpenBSD.
2312 </para>
2314 <para>
2315 BSD Authentication in <productname>PostgreSQL</productname> uses
2316 the <literal>auth-postgresql</literal> login type and authenticates with
2317 the <literal>postgresql</literal> login class if that's defined
2318 in <filename>login.conf</filename>. By default that login class does not
2319 exist, and <productname>PostgreSQL</productname> will use the default login class.
2320 </para>
2322 <note>
2323 <para>
2324 To use BSD Authentication, the PostgreSQL user account (that is, the
2325 operating system user running the server) must first be added to
2326 the <literal>auth</literal> group. The <literal>auth</literal> group
2327 exists by default on OpenBSD systems.
2328 </para>
2329 </note>
2330 </sect1>
2332 <sect1 id="client-authentication-problems">
2333 <title>Authentication Problems</title>
2335 <para>
2336 Authentication failures and related problems generally
2337 manifest themselves through error messages like the following:
2338 </para>
2340 <para>
2341 <programlisting>
2342 FATAL: no pg_hba.conf entry for host "123.123.123.123", user "andym", database "testdb"
2343 </programlisting>
2344 This is what you are most likely to get if you succeed in contacting
2345 the server, but it does not want to talk to you. As the message
2346 suggests, the server refused the connection request because it found
2347 no matching entry in its <filename>pg_hba.conf</filename>
2348 configuration file.
2349 </para>
2351 <para>
2352 <programlisting>
2353 FATAL: password authentication failed for user "andym"
2354 </programlisting>
2355 Messages like this indicate that you contacted the server, and it is
2356 willing to talk to you, but not until you pass the authorization
2357 method specified in the <filename>pg_hba.conf</filename> file. Check
2358 the password you are providing, or check your Kerberos or ident
2359 software if the complaint mentions one of those authentication
2360 types.
2361 </para>
2363 <para>
2364 <programlisting>
2365 FATAL: user "andym" does not exist
2366 </programlisting>
2367 The indicated database user name was not found.
2368 </para>
2370 <para>
2371 <programlisting>
2372 FATAL: database "testdb" does not exist
2373 </programlisting>
2374 The database you are trying to connect to does not exist. Note that
2375 if you do not specify a database name, it defaults to the database
2376 user name.
2377 </para>
2379 <tip>
2380 <para>
2381 The server log might contain more information about an
2382 authentication failure than is reported to the client. If you are
2383 confused about the reason for a failure, check the server log.
2384 </para>
2385 </tip>
2386 </sect1>
2388 </chapter>