doc: clarify pg_dump --no-comments meaning as SQL comments
[pgsql.git] / doc / src / sgml / client-auth.sgml
blob51343de7cadbeb6ff66f06aeef1e0e0b448fe63d
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 </listitem>
535 </varlistentry>
537 <varlistentry>
538 <term><literal>password</literal></term>
539 <listitem>
540 <para>
541 Require the client to supply an unencrypted password for
542 authentication.
543 Since the password is sent in clear text over the
544 network, this should not be used on untrusted networks.
545 See <xref linkend="auth-password"/> for details.
546 </para>
547 </listitem>
548 </varlistentry>
550 <varlistentry>
551 <term><literal>gss</literal></term>
552 <listitem>
553 <para>
554 Use GSSAPI to authenticate the user. This is only
555 available for TCP/IP connections. See <xref
556 linkend="gssapi-auth"/> for details. It can be used in conjunction
557 with GSSAPI encryption.
558 </para>
559 </listitem>
560 </varlistentry>
562 <varlistentry>
563 <term><literal>sspi</literal></term>
564 <listitem>
565 <para>
566 Use SSPI to authenticate the user. This is only
567 available on Windows. See <xref
568 linkend="sspi-auth"/> for details.
569 </para>
570 </listitem>
571 </varlistentry>
573 <varlistentry>
574 <term><literal>ident</literal></term>
575 <listitem>
576 <para>
577 Obtain the operating system user name of the client
578 by contacting the ident server on the client
579 and check if it matches the requested database user name.
580 Ident authentication can only be used on TCP/IP
581 connections. When specified for local connections, peer
582 authentication will be used instead.
583 See <xref linkend="auth-ident"/> for details.
584 </para>
585 </listitem>
586 </varlistentry>
588 <varlistentry>
589 <term><literal>peer</literal></term>
590 <listitem>
591 <para>
592 Obtain the client's operating system user name from the operating
593 system and check if it matches the requested database user name.
594 This is only available for local connections.
595 See <xref linkend="auth-peer"/> for details.
596 </para>
597 </listitem>
598 </varlistentry>
600 <varlistentry>
601 <term><literal>ldap</literal></term>
602 <listitem>
603 <para>
604 Authenticate using an <acronym>LDAP</acronym> server. See <xref
605 linkend="auth-ldap"/> for details.
606 </para>
607 </listitem>
608 </varlistentry>
610 <varlistentry>
611 <term><literal>radius</literal></term>
612 <listitem>
613 <para>
614 Authenticate using a RADIUS server. See <xref
615 linkend="auth-radius"/> for details.
616 </para>
617 </listitem>
618 </varlistentry>
620 <varlistentry>
621 <term><literal>cert</literal></term>
622 <listitem>
623 <para>
624 Authenticate using SSL client certificates. See
625 <xref linkend="auth-cert"/> for details.
626 </para>
627 </listitem>
628 </varlistentry>
630 <varlistentry>
631 <term><literal>pam</literal></term>
632 <listitem>
633 <para>
634 Authenticate using the Pluggable Authentication Modules
635 (PAM) service provided by the operating system. See <xref
636 linkend="auth-pam"/> for details.
637 </para>
638 </listitem>
639 </varlistentry>
641 <varlistentry>
642 <term><literal>bsd</literal></term>
643 <listitem>
644 <para>
645 Authenticate using the BSD Authentication service provided by the
646 operating system. See <xref linkend="auth-bsd"/> for details.
647 </para>
648 </listitem>
649 </varlistentry>
650 </variablelist>
652 </para>
653 </listitem>
654 </varlistentry>
656 <varlistentry>
657 <term><replaceable>auth-options</replaceable></term>
658 <listitem>
659 <para>
660 After the <replaceable>auth-method</replaceable> field, there can be field(s) of
661 the form <replaceable>name</replaceable><literal>=</literal><replaceable>value</replaceable> that
662 specify options for the authentication method. Details about which
663 options are available for which authentication methods appear below.
664 </para>
666 <para>
667 In addition to the method-specific options listed below, there is a
668 method-independent authentication option <literal>clientcert</literal>, which
669 can be specified in any <literal>hostssl</literal> record.
670 This option can be set to <literal>verify-ca</literal> or
671 <literal>verify-full</literal>. Both options require the client
672 to present a valid (trusted) SSL certificate, while
673 <literal>verify-full</literal> additionally enforces that the
674 <literal>cn</literal> (Common Name) in the certificate matches
675 the username or an applicable mapping.
676 This behavior is similar to the <literal>cert</literal> authentication
677 method (see <xref linkend="auth-cert"/>) but enables pairing
678 the verification of client certificates with any authentication
679 method that supports <literal>hostssl</literal> entries.
680 </para>
681 <para>
682 On any record using client certificate authentication (i.e. one
683 using the <literal>cert</literal> authentication method or one
684 using the <literal>clientcert</literal> option), you can specify
685 which part of the client certificate credentials to match using
686 the <literal>clientname</literal> option. This option can have one
687 of two values. If you specify <literal>clientname=CN</literal>, which
688 is the default, the username is matched against the certificate's
689 <literal>Common Name (CN)</literal>. If instead you specify
690 <literal>clientname=DN</literal> the username is matched against the
691 entire <literal>Distinguished Name (DN)</literal> of the certificate.
692 This option is probably best used in conjunction with a username map.
693 The comparison is done with the <literal>DN</literal> in
694 <ulink url="https://datatracker.ietf.org/doc/html/rfc2253">RFC 2253</ulink>
695 format. To see the <literal>DN</literal> of a client certificate
696 in this format, do
697 <programlisting>
698 openssl x509 -in myclient.crt -noout -subject -nameopt RFC2253 | sed "s/^subject=//"
699 </programlisting>
700 Care needs to be taken when using this option, especially when using
701 regular expression matching against the <literal>DN</literal>.
702 </para>
703 </listitem>
704 </varlistentry>
706 <varlistentry>
707 <term><literal>include</literal></term>
708 <listitem>
709 <para>
710 This line will be replaced by the contents of the given file.
711 </para>
712 </listitem>
713 </varlistentry>
715 <varlistentry>
716 <term><literal>include_if_exists</literal></term>
717 <listitem>
718 <para>
719 This line will be replaced by the content of the given file if the
720 file exists. Otherwise, a message is logged to indicate that the file
721 has been skipped.
722 </para>
723 </listitem>
724 </varlistentry>
726 <varlistentry>
727 <term><literal>include_dir</literal></term>
728 <listitem>
729 <para>
730 This line will be replaced by the contents of all the files found in
731 the directory, if they don't start with a <literal>.</literal> and end
732 with <literal>.conf</literal>, processed in file name order (according
733 to C locale rules, i.e., numbers before letters, and uppercase letters
734 before lowercase ones).
735 </para>
736 </listitem>
737 </varlistentry>
738 </variablelist>
739 </para>
741 <para>
742 Files included by <literal>@</literal> constructs are read as lists of names,
743 which can be separated by either whitespace or commas. Comments are
744 introduced by <literal>#</literal>, just as in
745 <filename>pg_hba.conf</filename>, and nested <literal>@</literal> constructs are
746 allowed. Unless the file name following <literal>@</literal> is an absolute
747 path, it is taken to be relative to the directory containing the
748 referencing file.
749 </para>
751 <para>
752 Since the <filename>pg_hba.conf</filename> records are examined
753 sequentially for each connection attempt, the order of the records is
754 significant. Typically, earlier records will have tight connection
755 match parameters and weaker authentication methods, while later
756 records will have looser match parameters and stronger authentication
757 methods. For example, one might wish to use <literal>trust</literal>
758 authentication for local TCP/IP connections but require a password for
759 remote TCP/IP connections. In this case a record specifying
760 <literal>trust</literal> authentication for connections from 127.0.0.1 would
761 appear before a record specifying password authentication for a wider
762 range of allowed client IP addresses.
763 </para>
765 <tip>
766 <para>
767 To connect to a particular database, a user must not only pass the
768 <filename>pg_hba.conf</filename> checks, but must have the
769 <literal>CONNECT</literal> privilege for the database. If you wish to
770 restrict which users can connect to which databases, it's usually
771 easier to control this by granting/revoking <literal>CONNECT</literal> privilege
772 than to put the rules in <filename>pg_hba.conf</filename> entries.
773 </para>
774 </tip>
776 <para>
777 Some examples of <filename>pg_hba.conf</filename> entries are shown in
778 <xref linkend="example-pg-hba.conf"/>. See the next section for details on the
779 different authentication methods.
780 </para>
782 <example id="example-pg-hba.conf">
783 <title>Example <filename>pg_hba.conf</filename> Entries</title>
784 <programlisting>
785 # Allow any user on the local system to connect to any database with
786 # any database user name using Unix-domain sockets (the default for local
787 # connections).
789 # TYPE DATABASE USER ADDRESS METHOD
790 local all all trust
792 # The same using local loopback TCP/IP connections.
794 # TYPE DATABASE USER ADDRESS METHOD
795 host all all 127.0.0.1/32 trust
797 # The same as the previous line, but using a separate netmask column
799 # TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
800 host all all 127.0.0.1 255.255.255.255 trust
802 # The same over IPv6.
804 # TYPE DATABASE USER ADDRESS METHOD
805 host all all ::1/128 trust
807 # The same using a host name (would typically cover both IPv4 and IPv6).
809 # TYPE DATABASE USER ADDRESS METHOD
810 host all all localhost trust
812 # The same using a regular expression for DATABASE, that allows connection
813 # to any databases with a name beginning with "db" and finishing with a
814 # number using two to four digits (like "db1234" or "db12").
816 # TYPE DATABASE USER ADDRESS METHOD
817 host "/^db\d{2,4}$" all localhost trust
819 # Allow any user from any host with IP address 192.168.93.x to connect
820 # to database "postgres" as the same user name that ident reports for
821 # the connection (typically the operating system user name).
823 # TYPE DATABASE USER ADDRESS METHOD
824 host postgres all 192.168.93.0/24 ident
826 # Allow any user from host 192.168.12.10 to connect to database
827 # "postgres" if the user's password is correctly supplied.
829 # TYPE DATABASE USER ADDRESS METHOD
830 host postgres all 192.168.12.10/32 scram-sha-256
832 # Allow any user from hosts in the example.com domain to connect to
833 # any database if the user's password is correctly supplied.
835 # Require SCRAM authentication for most users, but make an exception
836 # for user 'mike', who uses an older client that doesn't support SCRAM
837 # authentication.
839 # TYPE DATABASE USER ADDRESS METHOD
840 host all mike .example.com md5
841 host all all .example.com scram-sha-256
843 # In the absence of preceding "host" lines, these three lines will
844 # reject all connections from 192.168.54.1 (since that entry will be
845 # matched first), but allow GSSAPI-encrypted connections from anywhere else
846 # on the Internet. The zero mask causes no bits of the host IP address to
847 # be considered, so it matches any host. Unencrypted GSSAPI connections
848 # (which "fall through" to the third line since "hostgssenc" only matches
849 # encrypted GSSAPI connections) are allowed, but only from 192.168.12.10.
851 # TYPE DATABASE USER ADDRESS METHOD
852 host all all 192.168.54.1/32 reject
853 hostgssenc all all 0.0.0.0/0 gss
854 host all all 192.168.12.10/32 gss
856 # Allow users from 192.168.x.x hosts to connect to any database, if
857 # they pass the ident check. If, for example, ident says the user is
858 # "bryanh" and he requests to connect as PostgreSQL user "guest1", the
859 # connection is allowed if there is an entry in pg_ident.conf for map
860 # "omicron" that says "bryanh" is allowed to connect as "guest1".
862 # TYPE DATABASE USER ADDRESS METHOD
863 host all all 192.168.0.0/16 ident map=omicron
865 # If these are the only four lines for local connections, they will
866 # allow local users to connect only to their own databases (databases
867 # with the same name as their database user name) except for users whose
868 # name end with "helpdesk", administrators and members of role "support",
869 # who can connect to all databases. The file $PGDATA/admins contains a
870 # list of names of administrators. Passwords are required in all cases.
872 # TYPE DATABASE USER ADDRESS METHOD
873 local sameuser all md5
874 local all /^.*helpdesk$ md5
875 local all @admins md5
876 local all +support md5
878 # The last two lines above can be combined into a single line:
879 local all @admins,+support md5
881 # The database column can also use lists and file names:
882 local db1,db2,@demodbs all md5
883 </programlisting>
884 </example>
885 </sect1>
887 <sect1 id="auth-username-maps">
888 <title>User Name Maps</title>
890 <indexterm zone="auth-username-maps">
891 <primary>User name maps</primary>
892 </indexterm>
894 <para>
895 When using an external authentication system such as Ident or GSSAPI,
896 the name of the operating system user that initiated the connection
897 might not be the same as the database user (role) that is to be used.
898 In this case, a user name map can be applied to map the operating system
899 user name to a database user. To use user name mapping, specify
900 <literal>map</literal>=<replaceable>map-name</replaceable>
901 in the options field in <filename>pg_hba.conf</filename>. This option is
902 supported for all authentication methods that receive external user names.
903 Since different mappings might be needed for different connections,
904 the name of the map to be used is specified in the
905 <replaceable>map-name</replaceable> parameter in <filename>pg_hba.conf</filename>
906 to indicate which map to use for each individual connection.
907 </para>
909 <para>
910 User name maps are defined in the ident map file, which by default is named
911 <filename>pg_ident.conf</filename><indexterm><primary>pg_ident.conf</primary></indexterm>
912 and is stored in the
913 cluster's data directory. (It is possible to place the map file
914 elsewhere, however; see the <xref linkend="guc-ident-file"/>
915 configuration parameter.)
916 The ident map file contains lines of the general forms:
917 <synopsis>
918 <replaceable>map-name</replaceable> <replaceable>system-username</replaceable> <replaceable>database-username</replaceable>
919 <replaceable>include</replaceable> <replaceable>file</replaceable>
920 <replaceable>include_if_exists</replaceable> <replaceable>file</replaceable>
921 <replaceable>include_dir</replaceable> <replaceable>directory</replaceable>
922 </synopsis>
923 Comments, whitespace and line continuations are handled in the same way as in
924 <filename>pg_hba.conf</filename>. The
925 <replaceable>map-name</replaceable> is an arbitrary name that will be used to
926 refer to this mapping in <filename>pg_hba.conf</filename>. The other
927 two fields specify an operating system user name and a matching
928 database user name. The same <replaceable>map-name</replaceable> can be
929 used repeatedly to specify multiple user-mappings within a single map.
930 </para>
931 <para>
932 As for <filename>pg_hba.conf</filename>, the lines in this file can
933 be include directives, following the same rules.
934 </para>
936 <para>
937 The <filename>pg_ident.conf</filename> file is read on start-up and
938 when the main server process receives a
939 <systemitem>SIGHUP</systemitem><indexterm><primary>SIGHUP</primary></indexterm>
940 signal. If you edit the file on an
941 active system, you will need to signal the postmaster
942 (using <literal>pg_ctl reload</literal>, calling the SQL function
943 <function>pg_reload_conf()</function>, or using <literal>kill
944 -HUP</literal>) to make it re-read the file.
945 </para>
947 <para>
948 The system view
949 <link linkend="view-pg-ident-file-mappings"><structname>pg_ident_file_mappings</structname></link>
950 can be helpful for pre-testing changes to the
951 <filename>pg_ident.conf</filename> file, or for diagnosing problems if
952 loading of the file did not have the desired effects. Rows in the view with
953 non-null <structfield>error</structfield> fields indicate problems in the
954 corresponding lines of the file.
955 </para>
957 <para>
958 There is no restriction regarding how many database users a given
959 operating system user can correspond to, nor vice versa. Thus, entries
960 in a map should be thought of as meaning <quote>this operating system
961 user is allowed to connect as this database user</quote>, rather than
962 implying that they are equivalent. The connection will be allowed if
963 there is any map entry that pairs the user name obtained from the
964 external authentication system with the database user name that the
965 user has requested to connect as. The value <literal>all</literal>
966 can be used as the <replaceable>database-username</replaceable> to specify
967 that if the <replaceable>system-username</replaceable> matches, then this
968 user is allowed to log in as any of the existing database users. Quoting
969 <literal>all</literal> makes the keyword lose its special meaning.
970 </para>
971 <para>
972 If the <replaceable>database-username</replaceable> begins with a
973 <literal>+</literal> character, then the operating system user can login as
974 any user belonging to that role, similarly to how user names beginning with
975 <literal>+</literal> are treated in <literal>pg_hba.conf</literal>.
976 Thus, a <literal>+</literal> mark means <quote>match any of the roles that
977 are directly or indirectly members of this role</quote>, while a name
978 without a <literal>+</literal> mark matches only that specific role. Quoting
979 a username starting with a <literal>+</literal> makes the
980 <literal>+</literal> lose its special meaning.
981 </para>
982 <para>
983 If the <replaceable>system-username</replaceable> field starts with a slash (<literal>/</literal>),
984 the remainder of the field is treated as a regular expression.
985 (See <xref linkend="posix-syntax-details"/> for details of
986 <productname>PostgreSQL</productname>'s regular expression syntax.) The regular
987 expression can include a single capture, or parenthesized subexpression,
988 which can then be referenced in the <replaceable>database-username</replaceable>
989 field as <literal>\1</literal> (backslash-one). This allows the mapping of
990 multiple user names in a single line, which is particularly useful for
991 simple syntax substitutions. For example, these entries
992 <programlisting>
993 mymap /^(.*)@mydomain\.com$ \1
994 mymap /^(.*)@otherdomain\.com$ guest
995 </programlisting>
996 will remove the domain part for users with system user names that end with
997 <literal>@mydomain.com</literal>, and allow any user whose system name ends with
998 <literal>@otherdomain.com</literal> to log in as <literal>guest</literal>.
999 Quoting a <replaceable>database-username</replaceable> containing
1000 <literal>\1</literal> <emphasis>does not</emphasis> make
1001 <literal>\1</literal> lose its special meaning.
1002 </para>
1003 <para>
1004 If the <replaceable>database-username</replaceable> field starts with
1005 a slash (<literal>/</literal>), the remainder of the field is treated
1006 as a regular expression (see <xref linkend="posix-syntax-details"/>
1007 for details of <productname>PostgreSQL</productname>'s regular
1008 expression syntax). It is not possible to use <literal>\1</literal>
1009 to use a capture from regular expression on
1010 <replaceable>system-username</replaceable> for a regular expression
1011 on <replaceable>database-username</replaceable>.
1012 </para>
1014 <tip>
1015 <para>
1016 Keep in mind that by default, a regular expression can match just part of
1017 a string. It's usually wise to use <literal>^</literal> and <literal>$</literal>, as
1018 shown in the above example, to force the match to be to the entire
1019 system user name.
1020 </para>
1021 </tip>
1023 <para>
1024 A <filename>pg_ident.conf</filename> file that could be used in
1025 conjunction with the <filename>pg_hba.conf</filename> file in <xref
1026 linkend="example-pg-hba.conf"/> is shown in <xref
1027 linkend="example-pg-ident.conf"/>. In this example, anyone
1028 logged in to a machine on the 192.168 network that does not have the
1029 operating system user name <literal>bryanh</literal>, <literal>ann</literal>, or
1030 <literal>robert</literal> would not be granted access. Unix user
1031 <literal>robert</literal> would only be allowed access when he tries to
1032 connect as <productname>PostgreSQL</productname> user <literal>bob</literal>, not
1033 as <literal>robert</literal> or anyone else. <literal>ann</literal> would
1034 only be allowed to connect as <literal>ann</literal>. User
1035 <literal>bryanh</literal> would be allowed to connect as either
1036 <literal>bryanh</literal> or as <literal>guest1</literal>.
1037 </para>
1039 <example id="example-pg-ident.conf">
1040 <title>An Example <filename>pg_ident.conf</filename> File</title>
1041 <programlisting>
1042 # MAPNAME SYSTEM-USERNAME PG-USERNAME
1044 omicron bryanh bryanh
1045 omicron ann ann
1046 # bob has user name robert on these machines
1047 omicron robert bob
1048 # bryanh can also connect as guest1
1049 omicron bryanh guest1
1050 </programlisting>
1051 </example>
1052 </sect1>
1054 <sect1 id="auth-methods">
1055 <title>Authentication Methods</title>
1057 <para>
1058 <productname>PostgreSQL</productname> provides various methods for
1059 authenticating users:
1061 <itemizedlist>
1062 <listitem>
1063 <para>
1064 <link linkend="auth-trust">Trust authentication</link>, which
1065 simply trusts that users are who they say they are.
1066 </para>
1067 </listitem>
1068 <listitem>
1069 <para>
1070 <link linkend="auth-password">Password authentication</link>, which
1071 requires that users send a password.
1072 </para>
1073 </listitem>
1074 <listitem>
1075 <para>
1076 <link linkend="gssapi-auth">GSSAPI authentication</link>, which
1077 relies on a GSSAPI-compatible security library. Typically this is
1078 used to access an authentication server such as a Kerberos or
1079 Microsoft Active Directory server.
1080 </para>
1081 </listitem>
1082 <listitem>
1083 <para>
1084 <link linkend="sspi-auth">SSPI authentication</link>, which
1085 uses a Windows-specific protocol similar to GSSAPI.
1086 </para>
1087 </listitem>
1088 <listitem>
1089 <para>
1090 <link linkend="auth-ident">Ident authentication</link>, which
1091 relies on an <quote>Identification Protocol</quote>
1092 (<ulink url="https://datatracker.ietf.org/doc/html/rfc1413">RFC 1413</ulink>)
1093 service on the client's machine. (On local Unix-socket connections,
1094 this is treated as peer authentication.)
1095 </para>
1096 </listitem>
1097 <listitem>
1098 <para>
1099 <link linkend="auth-peer">Peer authentication</link>, which
1100 relies on operating system facilities to identify the process at the
1101 other end of a local connection. This is not supported for remote
1102 connections.
1103 </para>
1104 </listitem>
1105 <listitem>
1106 <para>
1107 <link linkend="auth-ldap">LDAP authentication</link>, which
1108 relies on an LDAP authentication server.
1109 </para>
1110 </listitem>
1111 <listitem>
1112 <para>
1113 <link linkend="auth-radius">RADIUS authentication</link>, which
1114 relies on a RADIUS authentication server.
1115 </para>
1116 </listitem>
1117 <listitem>
1118 <para>
1119 <link linkend="auth-cert">Certificate authentication</link>, which
1120 requires an SSL connection and authenticates users by checking the
1121 SSL certificate they send.
1122 </para>
1123 </listitem>
1124 <listitem>
1125 <para>
1126 <link linkend="auth-pam">PAM authentication</link>, which
1127 relies on a PAM (Pluggable Authentication Modules) library.
1128 </para>
1129 </listitem>
1130 <listitem>
1131 <para>
1132 <link linkend="auth-bsd">BSD authentication</link>, which
1133 relies on the BSD Authentication framework (currently available
1134 only on OpenBSD).
1135 </para>
1136 </listitem>
1137 </itemizedlist>
1138 </para>
1140 <para>
1141 Peer authentication is usually recommendable for local connections,
1142 though trust authentication might be sufficient in some circumstances.
1143 Password authentication is the easiest choice for remote connections.
1144 All the other options require some kind of external security
1145 infrastructure (usually an authentication server or a certificate
1146 authority for issuing SSL certificates), or are platform-specific.
1147 </para>
1149 <para>
1150 The following sections describe each of these authentication methods
1151 in more detail.
1152 </para>
1153 </sect1>
1155 <sect1 id="auth-trust">
1156 <title>Trust Authentication</title>
1158 <para>
1159 When <literal>trust</literal> authentication is specified,
1160 <productname>PostgreSQL</productname> assumes that anyone who can
1161 connect to the server is authorized to access the database with
1162 whatever database user name they specify (even superuser names).
1163 Of course, restrictions made in the <literal>database</literal> and
1164 <literal>user</literal> columns still apply.
1165 This method should only be used when there is adequate
1166 operating-system-level protection on connections to the server.
1167 </para>
1169 <para>
1170 <literal>trust</literal> authentication is appropriate and very
1171 convenient for local connections on a single-user workstation. It
1172 is usually <emphasis>not</emphasis> appropriate by itself on a multiuser
1173 machine. However, you might be able to use <literal>trust</literal> even
1174 on a multiuser machine, if you restrict access to the server's
1175 Unix-domain socket file using file-system permissions. To do this, set the
1176 <varname>unix_socket_permissions</varname> (and possibly
1177 <varname>unix_socket_group</varname>) configuration parameters as
1178 described in <xref linkend="runtime-config-connection"/>. Or you
1179 could set the <varname>unix_socket_directories</varname>
1180 configuration parameter to place the socket file in a suitably
1181 restricted directory.
1182 </para>
1184 <para>
1185 Setting file-system permissions only helps for Unix-socket connections.
1186 Local TCP/IP connections are not restricted by file-system permissions.
1187 Therefore, if you want to use file-system permissions for local security,
1188 remove the <literal>host ... 127.0.0.1 ...</literal> line from
1189 <filename>pg_hba.conf</filename>, or change it to a
1190 non-<literal>trust</literal> authentication method.
1191 </para>
1193 <para>
1194 <literal>trust</literal> authentication is only suitable for TCP/IP connections
1195 if you trust every user on every machine that is allowed to connect
1196 to the server by the <filename>pg_hba.conf</filename> lines that specify
1197 <literal>trust</literal>. It is seldom reasonable to use <literal>trust</literal>
1198 for any TCP/IP connections other than those from <systemitem>localhost</systemitem> (127.0.0.1).
1199 </para>
1201 </sect1>
1203 <sect1 id="auth-password">
1204 <title>Password Authentication</title>
1206 <indexterm>
1207 <primary>MD5</primary>
1208 </indexterm>
1209 <indexterm>
1210 <primary>SCRAM</primary>
1211 </indexterm>
1212 <indexterm>
1213 <primary>password</primary>
1214 <secondary>authentication</secondary>
1215 </indexterm>
1217 <para>
1218 There are several password-based authentication methods. These methods
1219 operate similarly but differ in how the users' passwords are stored on the
1220 server and how the password provided by a client is sent across the
1221 connection.
1222 </para>
1224 <variablelist>
1225 <varlistentry>
1226 <term><literal>scram-sha-256</literal></term>
1227 <listitem>
1228 <para>
1229 The method <literal>scram-sha-256</literal> performs SCRAM-SHA-256
1230 authentication, as described in
1231 <ulink url="https://datatracker.ietf.org/doc/html/rfc7677">RFC 7677</ulink>. It
1232 is a challenge-response scheme that prevents password sniffing on
1233 untrusted connections and supports storing passwords on the server in a
1234 cryptographically hashed form that is thought to be secure.
1235 </para>
1237 <para>
1238 This is the most secure of the currently provided methods, but it is
1239 not supported by older client libraries.
1240 </para>
1241 </listitem>
1242 </varlistentry>
1244 <varlistentry>
1245 <term><literal>md5</literal></term>
1246 <listitem>
1247 <para>
1248 The method <literal>md5</literal> uses a custom less secure challenge-response
1249 mechanism. It prevents password sniffing and avoids storing passwords
1250 on the server in plain text but provides no protection if an attacker
1251 manages to steal the password hash from the server. Also, the MD5 hash
1252 algorithm is nowadays no longer considered secure against determined
1253 attacks.
1254 </para>
1256 <para>
1257 To ease transition from the <literal>md5</literal> method to the newer
1258 SCRAM method, if <literal>md5</literal> is specified as a method
1259 in <filename>pg_hba.conf</filename> but the user's password on the
1260 server is encrypted for SCRAM (see below), then SCRAM-based
1261 authentication will automatically be chosen instead.
1262 </para>
1263 </listitem>
1264 </varlistentry>
1266 <varlistentry>
1267 <term><literal>password</literal></term>
1268 <listitem>
1269 <para>
1270 The method <literal>password</literal> sends the password in clear-text and is
1271 therefore vulnerable to password <quote>sniffing</quote> attacks. It should
1272 always be avoided if possible. If the connection is protected by SSL
1273 encryption then <literal>password</literal> can be used safely, though.
1274 (Though SSL certificate authentication might be a better choice if one
1275 is depending on using SSL).
1276 </para>
1277 </listitem>
1278 </varlistentry>
1279 </variablelist>
1281 <para>
1282 <productname>PostgreSQL</productname> database passwords are
1283 separate from operating system user passwords. The password for
1284 each database user is stored in the <literal>pg_authid</literal> system
1285 catalog. Passwords can be managed with the SQL commands
1286 <xref linkend="sql-createrole"/> and
1287 <xref linkend="sql-alterrole"/>,
1288 e.g., <userinput>CREATE ROLE foo WITH LOGIN PASSWORD 'secret'</userinput>,
1289 or the <application>psql</application>
1290 command <literal>\password</literal>.
1291 If no password has been set up for a user, the stored password
1292 is null and password authentication will always fail for that user.
1293 </para>
1295 <para>
1296 The availability of the different password-based authentication methods
1297 depends on how a user's password on the server is encrypted (or hashed,
1298 more accurately). This is controlled by the configuration
1299 parameter <xref linkend="guc-password-encryption"/> at the time the
1300 password is set. If a password was encrypted using
1301 the <literal>scram-sha-256</literal> setting, then it can be used for the
1302 authentication methods <literal>scram-sha-256</literal>
1303 and <literal>password</literal> (but password transmission will be in
1304 plain text in the latter case). The authentication method
1305 specification <literal>md5</literal> will automatically switch to using
1306 the <literal>scram-sha-256</literal> method in this case, as explained
1307 above, so it will also work. If a password was encrypted using
1308 the <literal>md5</literal> setting, then it can be used only for
1309 the <literal>md5</literal> and <literal>password</literal> authentication
1310 method specifications (again, with the password transmitted in plain text
1311 in the latter case). (Previous PostgreSQL releases supported storing the
1312 password on the server in plain text. This is no longer possible.) To
1313 check the currently stored password hashes, see the system
1314 catalog <literal>pg_authid</literal>.
1315 </para>
1317 <para>
1318 To upgrade an existing installation from <literal>md5</literal>
1319 to <literal>scram-sha-256</literal>, after having ensured that all client
1320 libraries in use are new enough to support SCRAM,
1321 set <literal>password_encryption = 'scram-sha-256'</literal>
1322 in <filename>postgresql.conf</filename>, make all users set new passwords,
1323 and change the authentication method specifications
1324 in <filename>pg_hba.conf</filename> to <literal>scram-sha-256</literal>.
1325 </para>
1326 </sect1>
1328 <sect1 id="gssapi-auth">
1329 <title>GSSAPI Authentication</title>
1331 <indexterm zone="gssapi-auth">
1332 <primary>GSSAPI</primary>
1333 </indexterm>
1335 <para>
1336 <productname>GSSAPI</productname> is an industry-standard protocol
1337 for secure authentication defined in
1338 <ulink url="https://datatracker.ietf.org/doc/html/rfc2743">RFC 2743</ulink>.
1339 <productname>PostgreSQL</productname>
1340 supports <productname>GSSAPI</productname> for authentication,
1341 communications encryption, or both.
1342 <productname>GSSAPI</productname> provides automatic authentication
1343 (single sign-on) for systems that support it. The authentication itself is
1344 secure. If <productname>GSSAPI</productname> encryption
1345 or <acronym>SSL</acronym> encryption is
1346 used, the data sent along the database connection will be encrypted;
1347 otherwise, it will not.
1348 </para>
1350 <para>
1351 GSSAPI support has to be enabled when <productname>PostgreSQL</productname> is built;
1352 see <xref linkend="installation"/> for more information.
1353 </para>
1355 <para>
1356 When <productname>GSSAPI</productname> uses
1357 <productname>Kerberos</productname>, it uses a standard service
1358 principal (authentication identity) name in the format
1359 <literal><replaceable>servicename</replaceable>/<replaceable>hostname</replaceable>@<replaceable>realm</replaceable></literal>.
1360 The principal name used by a particular installation is not encoded in
1361 the <productname>PostgreSQL</productname> server in any way; rather it
1362 is specified in the <firstterm>keytab</firstterm> file that the server
1363 reads to determine its identity. If multiple principals are listed in
1364 the keytab file, the server will accept any one of them.
1365 The server's realm name is the preferred realm specified in the Kerberos
1366 configuration file(s) accessible to the server.
1367 </para>
1369 <para>
1370 When connecting, the client must know the principal name of the server
1371 it intends to connect to. The <replaceable>servicename</replaceable>
1372 part of the principal is ordinarily <literal>postgres</literal>,
1373 but another value can be selected via <application>libpq</application>'s
1374 <xref linkend="libpq-connect-krbsrvname"/> connection parameter.
1375 The <replaceable>hostname</replaceable> part is the fully qualified
1376 host name that <application>libpq</application> is told to connect to.
1377 The realm name is the preferred realm specified in the Kerberos
1378 configuration file(s) accessible to the client.
1379 </para>
1381 <para>
1382 The client will also have a principal name for its own identity
1383 (and it must have a valid ticket for this principal). To
1384 use <productname>GSSAPI</productname> for authentication, the client
1385 principal must be associated with
1386 a <productname>PostgreSQL</productname> database user name.
1387 The <filename>pg_ident.conf</filename> configuration file can be used
1388 to map principals to user names; for example,
1389 <literal>pgusername@realm</literal> could be mapped to just <literal>pgusername</literal>.
1390 Alternatively, you can use the full <literal>username@realm</literal> principal as
1391 the role name in <productname>PostgreSQL</productname> without any mapping.
1392 </para>
1394 <para>
1395 <productname>PostgreSQL</productname> also supports mapping
1396 client principals to user names by just stripping the realm from
1397 the principal. This method is supported for backwards compatibility and is
1398 strongly discouraged as it is then impossible to distinguish different users
1399 with the same user name but coming from different realms. To enable this,
1400 set <literal>include_realm</literal> to 0. For simple single-realm
1401 installations, doing that combined with setting the
1402 <literal>krb_realm</literal> parameter (which checks that the principal's realm
1403 matches exactly what is in the <literal>krb_realm</literal> parameter)
1404 is still secure; but this is a
1405 less capable approach compared to specifying an explicit mapping in
1406 <filename>pg_ident.conf</filename>.
1407 </para>
1409 <para>
1410 The location of the server's keytab file is specified by the <xref
1411 linkend="guc-krb-server-keyfile"/> configuration parameter.
1412 For security reasons, it is recommended to use a separate keytab
1413 just for the <productname>PostgreSQL</productname> server rather
1414 than allowing the server to read the system keytab file.
1415 Make sure that your server keytab file is readable (and preferably
1416 only readable, not writable) by the <productname>PostgreSQL</productname>
1417 server account. (See also <xref linkend="postgres-user"/>.)
1418 </para>
1420 <para>
1421 The keytab file is generated using the Kerberos software; see the
1422 Kerberos documentation for details. The following example shows
1423 doing this using the <application>kadmin</application> tool of
1424 MIT Kerberos:
1425 <screen>
1426 <prompt>kadmin% </prompt><userinput>addprinc -randkey postgres/server.my.domain.org</userinput>
1427 <prompt>kadmin% </prompt><userinput>ktadd -k krb5.keytab postgres/server.my.domain.org</userinput>
1428 </screen>
1429 </para>
1431 <para>
1432 The following authentication options are supported for
1433 the <productname>GSSAPI</productname> authentication method:
1434 <variablelist>
1435 <varlistentry>
1436 <term><literal>include_realm</literal></term>
1437 <listitem>
1438 <para>
1439 If set to 0, the realm name from the authenticated user principal is
1440 stripped off before being passed through the user name mapping
1441 (<xref linkend="auth-username-maps"/>). This is discouraged and is
1442 primarily available for backwards compatibility, as it is not secure
1443 in multi-realm environments unless <literal>krb_realm</literal> is
1444 also used. It is recommended to
1445 leave <literal>include_realm</literal> set to the default (1) and to
1446 provide an explicit mapping in <filename>pg_ident.conf</filename> to convert
1447 principal names to <productname>PostgreSQL</productname> user names.
1448 </para>
1449 </listitem>
1450 </varlistentry>
1452 <varlistentry>
1453 <term><literal>map</literal></term>
1454 <listitem>
1455 <para>
1456 Allows mapping from client principals to database user names. See
1457 <xref linkend="auth-username-maps"/> for details. For a GSSAPI/Kerberos
1458 principal, such as <literal>username@EXAMPLE.COM</literal> (or, less
1459 commonly, <literal>username/hostbased@EXAMPLE.COM</literal>), the
1460 user name used for mapping is
1461 <literal>username@EXAMPLE.COM</literal> (or
1462 <literal>username/hostbased@EXAMPLE.COM</literal>, respectively),
1463 unless <literal>include_realm</literal> has been set to 0, in which case
1464 <literal>username</literal> (or <literal>username/hostbased</literal>)
1465 is what is seen as the system user name when mapping.
1466 </para>
1467 </listitem>
1468 </varlistentry>
1470 <varlistentry>
1471 <term><literal>krb_realm</literal></term>
1472 <listitem>
1473 <para>
1474 Sets the realm to match user principal names against. If this parameter
1475 is set, only users of that realm will be accepted. If it is not set,
1476 users of any realm can connect, subject to whatever user name mapping
1477 is done.
1478 </para>
1479 </listitem>
1480 </varlistentry>
1481 </variablelist>
1482 </para>
1484 <para>
1485 In addition to these settings, which can be different for
1486 different <filename>pg_hba.conf</filename> entries, there is the
1487 server-wide <xref linkend="guc-krb-caseins-users"/> configuration
1488 parameter. If that is set to true, client principals are matched to
1489 user map entries case-insensitively. <literal>krb_realm</literal>, if
1490 set, is also matched case-insensitively.
1491 </para>
1492 </sect1>
1494 <sect1 id="sspi-auth">
1495 <title>SSPI Authentication</title>
1497 <indexterm zone="sspi-auth">
1498 <primary>SSPI</primary>
1499 </indexterm>
1501 <para>
1502 <productname>SSPI</productname> is a <productname>Windows</productname>
1503 technology for secure authentication with single sign-on.
1504 <productname>PostgreSQL</productname> will use SSPI in
1505 <literal>negotiate</literal> mode, which will use
1506 <productname>Kerberos</productname> when possible and automatically
1507 fall back to <productname>NTLM</productname> in other cases.
1508 <productname>SSPI</productname> and <productname>GSSAPI</productname>
1509 interoperate as clients and servers, e.g., an
1510 <productname>SSPI</productname> client can authenticate to an
1511 <productname>GSSAPI</productname> server. It is recommended to use
1512 <productname>SSPI</productname> on Windows clients and servers and
1513 <productname>GSSAPI</productname> on non-Windows platforms.
1514 </para>
1516 <para>
1517 When using <productname>Kerberos</productname> authentication,
1518 <productname>SSPI</productname> works the same way
1519 <productname>GSSAPI</productname> does; see <xref linkend="gssapi-auth"/>
1520 for details.
1521 </para>
1523 <para>
1524 The following configuration options are supported for <productname>SSPI</productname>:
1525 <variablelist>
1527 <varlistentry>
1528 <term><literal>include_realm</literal></term>
1529 <listitem>
1530 <para>
1531 If set to 0, the realm name from the authenticated user principal is
1532 stripped off before being passed through the user name mapping
1533 (<xref linkend="auth-username-maps"/>). This is discouraged and is
1534 primarily available for backwards compatibility, as it is not secure
1535 in multi-realm environments unless <literal>krb_realm</literal> is
1536 also used. It is recommended to
1537 leave <literal>include_realm</literal> set to the default (1) and to
1538 provide an explicit mapping in <filename>pg_ident.conf</filename> to convert
1539 principal names to <productname>PostgreSQL</productname> user names.
1540 </para>
1541 </listitem>
1542 </varlistentry>
1544 <varlistentry>
1545 <term><literal>compat_realm</literal></term>
1546 <listitem>
1547 <para>
1548 If set to 1, the domain's SAM-compatible name (also known as the
1549 NetBIOS name) is used for the <literal>include_realm</literal>
1550 option. This is the default. If set to 0, the true realm name from
1551 the Kerberos user principal name is used.
1552 </para>
1553 <para>
1554 Do not disable this option unless your server runs under a domain
1555 account (this includes virtual service accounts on a domain member
1556 system) and all clients authenticating through SSPI are also using
1557 domain accounts, or authentication will fail.
1558 </para>
1559 </listitem>
1560 </varlistentry>
1562 <varlistentry>
1563 <term><literal>upn_username</literal></term>
1564 <listitem>
1565 <para>
1566 If this option is enabled along with <literal>compat_realm</literal>,
1567 the user name from the Kerberos UPN is used for authentication. If
1568 it is disabled (the default), the SAM-compatible user name is used.
1569 By default, these two names are identical for new user accounts.
1570 </para>
1571 <para>
1572 Note that <application>libpq</application> uses the SAM-compatible name if no
1573 explicit user name is specified. If you use
1574 <application>libpq</application> or a driver based on it, you should
1575 leave this option disabled or explicitly specify user name in the
1576 connection string.
1577 </para>
1578 </listitem>
1579 </varlistentry>
1581 <varlistentry>
1582 <term><literal>map</literal></term>
1583 <listitem>
1584 <para>
1585 Allows for mapping between system and database user names. See
1586 <xref linkend="auth-username-maps"/> for details. For an SSPI/Kerberos
1587 principal, such as <literal>username@EXAMPLE.COM</literal> (or, less
1588 commonly, <literal>username/hostbased@EXAMPLE.COM</literal>), the
1589 user name used for mapping is
1590 <literal>username@EXAMPLE.COM</literal> (or
1591 <literal>username/hostbased@EXAMPLE.COM</literal>, respectively),
1592 unless <literal>include_realm</literal> has been set to 0, in which case
1593 <literal>username</literal> (or <literal>username/hostbased</literal>)
1594 is what is seen as the system user name when mapping.
1595 </para>
1596 </listitem>
1597 </varlistentry>
1599 <varlistentry>
1600 <term><literal>krb_realm</literal></term>
1601 <listitem>
1602 <para>
1603 Sets the realm to match user principal names against. If this parameter
1604 is set, only users of that realm will be accepted. If it is not set,
1605 users of any realm can connect, subject to whatever user name mapping
1606 is done.
1607 </para>
1608 </listitem>
1609 </varlistentry>
1610 </variablelist>
1611 </para>
1612 </sect1>
1614 <sect1 id="auth-ident">
1615 <title>Ident Authentication</title>
1617 <indexterm>
1618 <primary>ident</primary>
1619 </indexterm>
1621 <para>
1622 The ident authentication method works by obtaining the client's
1623 operating system user name from an ident server and using it as
1624 the allowed database user name (with an optional user name mapping).
1625 This is only supported on TCP/IP connections.
1626 </para>
1628 <note>
1629 <para>
1630 When ident is specified for a local (non-TCP/IP) connection,
1631 peer authentication (see <xref linkend="auth-peer"/>) will be
1632 used instead.
1633 </para>
1634 </note>
1636 <para>
1637 The following configuration options are supported for <literal>ident</literal>:
1638 <variablelist>
1639 <varlistentry>
1640 <term><literal>map</literal></term>
1641 <listitem>
1642 <para>
1643 Allows for mapping between system and database user names. See
1644 <xref linkend="auth-username-maps"/> for details.
1645 </para>
1646 </listitem>
1647 </varlistentry>
1648 </variablelist>
1649 </para>
1651 <para>
1652 The <quote>Identification Protocol</quote> is described in
1653 <ulink url="https://datatracker.ietf.org/doc/html/rfc1413">RFC 1413</ulink>.
1654 Virtually every Unix-like
1655 operating system ships with an ident server that listens on TCP
1656 port 113 by default. The basic functionality of an ident server
1657 is to answer questions like <quote>What user initiated the
1658 connection that goes out of your port <replaceable>X</replaceable>
1659 and connects to my port <replaceable>Y</replaceable>?</quote>.
1660 Since <productname>PostgreSQL</productname> knows both <replaceable>X</replaceable> and
1661 <replaceable>Y</replaceable> when a physical connection is established, it
1662 can interrogate the ident server on the host of the connecting
1663 client and can theoretically determine the operating system user
1664 for any given connection.
1665 </para>
1667 <para>
1668 The drawback of this procedure is that it depends on the integrity
1669 of the client: if the client machine is untrusted or compromised,
1670 an attacker could run just about any program on port 113 and
1671 return any user name they choose. This authentication method is
1672 therefore only appropriate for closed networks where each client
1673 machine is under tight control and where the database and system
1674 administrators operate in close contact. In other words, you must
1675 trust the machine running the ident server.
1676 Heed the warning:
1677 <blockquote>
1678 <attribution>RFC 1413</attribution>
1679 <para>
1680 The Identification Protocol is not intended as an authorization
1681 or access control protocol.
1682 </para>
1683 </blockquote>
1684 </para>
1686 <para>
1687 Some ident servers have a nonstandard option that causes the returned
1688 user name to be encrypted, using a key that only the originating
1689 machine's administrator knows. This option <emphasis>must not</emphasis> be
1690 used when using the ident server with <productname>PostgreSQL</productname>,
1691 since <productname>PostgreSQL</productname> does not have any way to decrypt the
1692 returned string to determine the actual user name.
1693 </para>
1694 </sect1>
1696 <sect1 id="auth-peer">
1697 <title>Peer Authentication</title>
1699 <indexterm>
1700 <primary>peer</primary>
1701 </indexterm>
1703 <para>
1704 The peer authentication method works by obtaining the client's
1705 operating system user name from the kernel and using it as the
1706 allowed database user name (with optional user name mapping). This
1707 method is only supported on local connections.
1708 </para>
1710 <para>
1711 The following configuration options are supported for <literal>peer</literal>:
1712 <variablelist>
1713 <varlistentry>
1714 <term><literal>map</literal></term>
1715 <listitem>
1716 <para>
1717 Allows for mapping between system and database user names. See
1718 <xref linkend="auth-username-maps"/> for details.
1719 </para>
1720 </listitem>
1721 </varlistentry>
1722 </variablelist>
1723 </para>
1725 <para>
1726 Peer authentication is only available on operating systems providing
1727 the <function>getpeereid()</function> function, the <symbol>SO_PEERCRED</symbol>
1728 socket parameter, or similar mechanisms. Currently that includes
1729 <systemitem class="osname">Linux</systemitem>,
1730 most flavors of <systemitem class="osname">BSD</systemitem> including
1731 <systemitem class="osname">macOS</systemitem>,
1732 and <systemitem class="osname">Solaris</systemitem>.
1733 </para>
1735 </sect1>
1737 <sect1 id="auth-ldap">
1738 <title>LDAP Authentication</title>
1740 <indexterm zone="auth-ldap">
1741 <primary>LDAP</primary>
1742 </indexterm>
1744 <para>
1745 This authentication method operates similarly to
1746 <literal>password</literal> except that it uses LDAP
1747 as the password verification method. LDAP is used only to validate
1748 the user name/password pairs. Therefore the user must already
1749 exist in the database before LDAP can be used for
1750 authentication.
1751 </para>
1753 <para>
1754 LDAP authentication can operate in two modes. In the first mode,
1755 which we will call the simple bind mode,
1756 the server will bind to the distinguished name constructed as
1757 <replaceable>prefix</replaceable> <replaceable>username</replaceable> <replaceable>suffix</replaceable>.
1758 Typically, the <replaceable>prefix</replaceable> parameter is used to specify
1759 <literal>cn=</literal>, or <replaceable>DOMAIN</replaceable><literal>\</literal> in an Active
1760 Directory environment. <replaceable>suffix</replaceable> is used to specify the
1761 remaining part of the DN in a non-Active Directory environment.
1762 </para>
1764 <para>
1765 In the second mode, which we will call the search+bind mode,
1766 the server first binds to the LDAP directory with
1767 a fixed user name and password, specified with <replaceable>ldapbinddn</replaceable>
1768 and <replaceable>ldapbindpasswd</replaceable>, and performs a search for the user trying
1769 to log in to the database. If no user and password is configured, an
1770 anonymous bind will be attempted to the directory. The search will be
1771 performed over the subtree at <replaceable>ldapbasedn</replaceable>, and will try to
1772 do an exact match of the attribute specified in
1773 <replaceable>ldapsearchattribute</replaceable>.
1774 Once the user has been found in
1775 this search, the server re-binds to the directory as
1776 this user, using the password specified by the client, to verify that the
1777 login is correct. This mode is the same as that used by LDAP authentication
1778 schemes in other software, such as Apache <literal>mod_authnz_ldap</literal> and <literal>pam_ldap</literal>.
1779 This method allows for significantly more flexibility
1780 in where the user objects are located in the directory, but will cause
1781 two additional requests to the LDAP server to be made.
1782 </para>
1784 <para>
1785 The following configuration options are used in both modes:
1786 <variablelist>
1787 <varlistentry>
1788 <term><literal>ldapserver</literal></term>
1789 <listitem>
1790 <para>
1791 Names or IP addresses of LDAP servers to connect to. Multiple
1792 servers may be specified, separated by spaces.
1793 </para>
1794 </listitem>
1795 </varlistentry>
1796 <varlistentry>
1797 <term><literal>ldapport</literal></term>
1798 <listitem>
1799 <para>
1800 Port number on LDAP server to connect to. If no port is specified,
1801 the LDAP library's default port setting will be used.
1802 </para>
1803 </listitem>
1804 </varlistentry>
1805 <varlistentry>
1806 <term><literal>ldapscheme</literal></term>
1807 <listitem>
1808 <para>
1809 Set to <literal>ldaps</literal> to use LDAPS. This is a non-standard
1810 way of using LDAP over SSL, supported by some LDAP server
1811 implementations. See also the <literal>ldaptls</literal> option for
1812 an alternative.
1813 </para>
1814 </listitem>
1815 </varlistentry>
1816 <varlistentry>
1817 <term><literal>ldaptls</literal></term>
1818 <listitem>
1819 <para>
1820 Set to 1 to make the connection between PostgreSQL and the LDAP server
1821 use TLS encryption. This uses the <literal>StartTLS</literal>
1822 operation per <ulink url="https://datatracker.ietf.org/doc/html/rfc4513">RFC 4513</ulink>.
1823 See also the <literal>ldapscheme</literal> option for an alternative.
1824 </para>
1825 </listitem>
1826 </varlistentry>
1827 </variablelist>
1828 </para>
1830 <para>
1831 Note that using <literal>ldapscheme</literal> or
1832 <literal>ldaptls</literal> only encrypts the traffic between the
1833 PostgreSQL server and the LDAP server. The connection between the
1834 PostgreSQL server and the PostgreSQL client will still be unencrypted
1835 unless SSL is used there as well.
1836 </para>
1838 <para>
1839 The following options are used in simple bind mode only:
1840 <variablelist>
1841 <varlistentry>
1842 <term><literal>ldapprefix</literal></term>
1843 <listitem>
1844 <para>
1845 String to prepend to the user name when forming the DN to bind as,
1846 when doing simple bind authentication.
1847 </para>
1848 </listitem>
1849 </varlistentry>
1850 <varlistentry>
1851 <term><literal>ldapsuffix</literal></term>
1852 <listitem>
1853 <para>
1854 String to append to the user name when forming the DN to bind as,
1855 when doing simple bind authentication.
1856 </para>
1857 </listitem>
1858 </varlistentry>
1859 </variablelist>
1860 </para>
1862 <para>
1863 The following options are used in search+bind mode only:
1864 <variablelist>
1865 <varlistentry>
1866 <term><literal>ldapbasedn</literal></term>
1867 <listitem>
1868 <para>
1869 Root DN to begin the search for the user in, when doing search+bind
1870 authentication.
1871 </para>
1872 </listitem>
1873 </varlistentry>
1874 <varlistentry>
1875 <term><literal>ldapbinddn</literal></term>
1876 <listitem>
1877 <para>
1878 DN of user to bind to the directory with to perform the search when
1879 doing search+bind authentication.
1880 </para>
1881 </listitem>
1882 </varlistentry>
1883 <varlistentry>
1884 <term><literal>ldapbindpasswd</literal></term>
1885 <listitem>
1886 <para>
1887 Password for user to bind to the directory with to perform the search
1888 when doing search+bind authentication.
1889 </para>
1890 </listitem>
1891 </varlistentry>
1892 <varlistentry>
1893 <term><literal>ldapsearchattribute</literal></term>
1894 <listitem>
1895 <para>
1896 Attribute to match against the user name in the search when doing
1897 search+bind authentication. If no attribute is specified, the
1898 <literal>uid</literal> attribute will be used.
1899 </para>
1900 </listitem>
1901 </varlistentry>
1902 <varlistentry>
1903 <term><literal>ldapsearchfilter</literal></term>
1904 <listitem>
1905 <para>
1906 The search filter to use when doing search+bind authentication.
1907 Occurrences of <literal>$username</literal> will be replaced with the
1908 user name. This allows for more flexible search filters than
1909 <literal>ldapsearchattribute</literal>.
1910 </para>
1911 </listitem>
1912 </varlistentry>
1913 </variablelist>
1914 </para>
1916 <para>
1917 The following option may be used as an alternative way to write some of the
1918 above LDAP options in a more compact and standard form:
1919 <variablelist>
1920 <varlistentry>
1921 <term><literal>ldapurl</literal></term>
1922 <listitem>
1923 <para>
1924 An <ulink url="https://datatracker.ietf.org/doc/html/rfc4516">RFC 4516</ulink>
1925 LDAP URL. The format is
1926 <synopsis>
1927 ldap[s]://<replaceable>host</replaceable>[:<replaceable>port</replaceable>]/<replaceable>basedn</replaceable>[?[<replaceable>attribute</replaceable>][?[<replaceable>scope</replaceable>][?[<replaceable>filter</replaceable>]]]]
1928 </synopsis>
1929 <replaceable>scope</replaceable> must be one
1930 of <literal>base</literal>, <literal>one</literal>, <literal>sub</literal>,
1931 typically the last. (The default is <literal>base</literal>, which
1932 is normally not useful in this application.) <replaceable>attribute</replaceable> can
1933 nominate a single attribute, in which case it is used as a value for
1934 <literal>ldapsearchattribute</literal>. If
1935 <replaceable>attribute</replaceable> is empty then
1936 <replaceable>filter</replaceable> can be used as a value for
1937 <literal>ldapsearchfilter</literal>.
1938 </para>
1940 <para>
1941 The URL scheme <literal>ldaps</literal> chooses the LDAPS method for
1942 making LDAP connections over SSL, equivalent to using
1943 <literal>ldapscheme=ldaps</literal>. To use encrypted LDAP
1944 connections using the <literal>StartTLS</literal> operation, use the
1945 normal URL scheme <literal>ldap</literal> and specify the
1946 <literal>ldaptls</literal> option in addition to
1947 <literal>ldapurl</literal>.
1948 </para>
1950 <para>
1951 For non-anonymous binds, <literal>ldapbinddn</literal>
1952 and <literal>ldapbindpasswd</literal> must be specified as separate
1953 options.
1954 </para>
1956 <para>
1957 LDAP URLs are currently only supported with
1958 <productname>OpenLDAP</productname>, not on Windows.
1959 </para>
1960 </listitem>
1961 </varlistentry>
1962 </variablelist>
1963 </para>
1965 <para>
1966 It is an error to mix configuration options for simple bind with options
1967 for search+bind. To use <literal>ldapurl</literal> in simple bind mode, the
1968 URL must not contain a <literal>basedn</literal> or query elements.
1969 </para>
1971 <para>
1972 When using search+bind mode, the search can be performed using a single
1973 attribute specified with <literal>ldapsearchattribute</literal>, or using
1974 a custom search filter specified with
1975 <literal>ldapsearchfilter</literal>.
1976 Specifying <literal>ldapsearchattribute=foo</literal> is equivalent to
1977 specifying <literal>ldapsearchfilter="(foo=$username)"</literal>. If neither
1978 option is specified the default is
1979 <literal>ldapsearchattribute=uid</literal>.
1980 </para>
1982 <para>
1983 If <productname>PostgreSQL</productname> was compiled with
1984 <productname>OpenLDAP</productname> as the LDAP client library, the
1985 <literal>ldapserver</literal> setting may be omitted. In that case, a
1986 list of host names and ports is looked up via
1987 <ulink url="https://datatracker.ietf.org/doc/html/rfc2782">RFC 2782</ulink> DNS SRV records.
1988 The name <literal>_ldap._tcp.DOMAIN</literal> is looked up, where
1989 <literal>DOMAIN</literal> is extracted from <literal>ldapbasedn</literal>.
1990 </para>
1992 <para>
1993 Here is an example for a simple-bind LDAP configuration:
1994 <programlisting>
1995 host ... ldap ldapserver=ldap.example.net ldapprefix="cn=" ldapsuffix=", dc=example, dc=net"
1996 </programlisting>
1997 When a connection to the database server as database
1998 user <literal>someuser</literal> is requested, PostgreSQL will attempt to
1999 bind to the LDAP server using the DN <literal>cn=someuser, dc=example,
2000 dc=net</literal> and the password provided by the client. If that connection
2001 succeeds, the database access is granted.
2002 </para>
2004 <para>
2005 Here is a different simple-bind configuration, which uses the LDAPS scheme
2006 and a custom port number, written as a URL:
2007 <programlisting>
2008 host ... ldap ldapurl="ldaps://ldap.example.net:49151" ldapprefix="cn=" ldapsuffix=", dc=example, dc=net"
2009 </programlisting>
2010 This is slightly more compact than specifying <literal>ldapserver</literal>,
2011 <literal>ldapscheme</literal>, and <literal>ldapport</literal> separately.
2012 </para>
2014 <para>
2015 Here is an example for a search+bind configuration:
2016 <programlisting>
2017 host ... ldap ldapserver=ldap.example.net ldapbasedn="dc=example, dc=net" ldapsearchattribute=uid
2018 </programlisting>
2019 When a connection to the database server as database
2020 user <literal>someuser</literal> is requested, PostgreSQL will attempt to
2021 bind anonymously (since <literal>ldapbinddn</literal> was not specified) to
2022 the LDAP server, perform a search for <literal>(uid=someuser)</literal>
2023 under the specified base DN. If an entry is found, it will then attempt to
2024 bind using that found information and the password supplied by the client.
2025 If that second bind succeeds, the database access is granted.
2026 </para>
2028 <para>
2029 Here is the same search+bind configuration written as a URL:
2030 <programlisting>
2031 host ... ldap ldapurl="ldap://ldap.example.net/dc=example,dc=net?uid?sub"
2032 </programlisting>
2033 Some other software that supports authentication against LDAP uses the
2034 same URL format, so it will be easier to share the configuration.
2035 </para>
2037 <para>
2038 Here is an example for a search+bind configuration that uses
2039 <literal>ldapsearchfilter</literal> instead of
2040 <literal>ldapsearchattribute</literal> to allow authentication by
2041 user ID or email address:
2042 <programlisting>
2043 host ... ldap ldapserver=ldap.example.net ldapbasedn="dc=example, dc=net" ldapsearchfilter="(|(uid=$username)(mail=$username))"
2044 </programlisting>
2045 </para>
2047 <para>
2048 Here is an example for a search+bind configuration that uses DNS SRV
2049 discovery to find the host name(s) and port(s) for the LDAP service for the
2050 domain name <literal>example.net</literal>:
2051 <programlisting>
2052 host ... ldap ldapbasedn="dc=example,dc=net"
2053 </programlisting>
2054 </para>
2056 <tip>
2057 <para>
2058 Since LDAP often uses commas and spaces to separate the different
2059 parts of a DN, it is often necessary to use double-quoted parameter
2060 values when configuring LDAP options, as shown in the examples.
2061 </para>
2062 </tip>
2064 </sect1>
2066 <sect1 id="auth-radius">
2067 <title>RADIUS Authentication</title>
2069 <indexterm zone="auth-radius">
2070 <primary>RADIUS</primary>
2071 </indexterm>
2073 <para>
2074 This authentication method operates similarly to
2075 <literal>password</literal> except that it uses RADIUS
2076 as the password verification method. RADIUS is used only to validate
2077 the user name/password pairs. Therefore the user must already
2078 exist in the database before RADIUS can be used for
2079 authentication.
2080 </para>
2082 <para>
2083 When using RADIUS authentication, an Access Request message will be sent
2084 to the configured RADIUS server. This request will be of type
2085 <literal>Authenticate Only</literal>, and include parameters for
2086 <literal>user name</literal>, <literal>password</literal> (encrypted) and
2087 <literal>NAS Identifier</literal>. The request will be encrypted using
2088 a secret shared with the server. The RADIUS server will respond to
2089 this request with either <literal>Access Accept</literal> or
2090 <literal>Access Reject</literal>. There is no support for RADIUS accounting.
2091 </para>
2093 <para>
2094 Multiple RADIUS servers can be specified, in which case they will
2095 be tried sequentially. If a negative response is received from
2096 a server, the authentication will fail. If no response is received,
2097 the next server in the list will be tried. To specify multiple
2098 servers, separate the server names with commas and surround the list
2099 with double quotes. If multiple servers are specified, the other
2100 RADIUS options can also be given as comma-separated lists, to provide
2101 individual values for each server. They can also be specified as
2102 a single value, in which case that value will apply to all servers.
2103 </para>
2105 <para>
2106 The following configuration options are supported for RADIUS:
2107 <variablelist>
2108 <varlistentry>
2109 <term><literal>radiusservers</literal></term>
2110 <listitem>
2111 <para>
2112 The DNS names or IP addresses of the RADIUS servers to connect to.
2113 This parameter is required.
2114 </para>
2115 </listitem>
2116 </varlistentry>
2118 <varlistentry>
2119 <term><literal>radiussecrets</literal></term>
2120 <listitem>
2121 <para>
2122 The shared secrets used when talking securely to the RADIUS
2123 servers. This must have exactly the same value on the PostgreSQL
2124 and RADIUS servers. It is recommended that this be a string of
2125 at least 16 characters. This parameter is required.
2126 <note>
2127 <para>
2128 The encryption vector used will only be cryptographically
2129 strong if <productname>PostgreSQL</productname> is built with support for
2130 <productname>OpenSSL</productname>. In other cases, the transmission to the
2131 RADIUS server should only be considered obfuscated, not secured, and
2132 external security measures should be applied if necessary.
2133 </para>
2134 </note>
2135 </para>
2136 </listitem>
2137 </varlistentry>
2139 <varlistentry>
2140 <term><literal>radiusports</literal></term>
2141 <listitem>
2142 <para>
2143 The port numbers to connect to on the RADIUS servers. If no port
2144 is specified, the default RADIUS port (<literal>1812</literal>)
2145 will be used.
2146 </para>
2147 </listitem>
2148 </varlistentry>
2150 <varlistentry>
2151 <term><literal>radiusidentifiers</literal></term>
2152 <listitem>
2153 <para>
2154 The strings to be used as <literal>NAS Identifier</literal> in the
2155 RADIUS requests. This parameter can be used, for example, to
2156 identify which database cluster the user is attempting to connect
2157 to, which can be useful for policy matching on
2158 the RADIUS server. If no identifier is specified, the default
2159 <literal>postgresql</literal> will be used.
2160 </para>
2161 </listitem>
2162 </varlistentry>
2164 </variablelist>
2165 </para>
2167 <para>
2168 If it is necessary to have a comma or whitespace in a RADIUS parameter
2169 value, that can be done by putting double quotes around the value, but
2170 it is tedious because two layers of double-quoting are now required.
2171 An example of putting whitespace into RADIUS secret strings is:
2172 <programlisting>
2173 host ... radius radiusservers="server1,server2" radiussecrets="""secret one"",""secret two"""
2174 </programlisting>
2175 </para>
2176 </sect1>
2178 <sect1 id="auth-cert">
2179 <title>Certificate Authentication</title>
2181 <indexterm zone="auth-cert">
2182 <primary>Certificate</primary>
2183 </indexterm>
2185 <para>
2186 This authentication method uses SSL client certificates to perform
2187 authentication. It is therefore only available for SSL connections;
2188 see <xref linkend="ssl-openssl-config"/> for SSL configuration instructions.
2189 When using this authentication method, the server will require that
2190 the client provide a valid, trusted certificate. No password prompt
2191 will be sent to the client. The <literal>cn</literal> (Common Name)
2192 attribute of the certificate
2193 will be compared to the requested database user name, and if they match
2194 the login will be allowed. User name mapping can be used to allow
2195 <literal>cn</literal> to be different from the database user name.
2196 </para>
2198 <para>
2199 The following configuration options are supported for SSL certificate
2200 authentication:
2201 <variablelist>
2202 <varlistentry>
2203 <term><literal>map</literal></term>
2204 <listitem>
2205 <para>
2206 Allows for mapping between system and database user names. See
2207 <xref linkend="auth-username-maps"/> for details.
2208 </para>
2209 </listitem>
2210 </varlistentry>
2211 </variablelist>
2212 </para>
2214 <para>
2215 It is redundant to use the <literal>clientcert</literal> option with
2216 <literal>cert</literal> authentication because <literal>cert</literal>
2217 authentication is effectively <literal>trust</literal> authentication
2218 with <literal>clientcert=verify-full</literal>.
2219 </para>
2220 </sect1>
2222 <sect1 id="auth-pam">
2223 <title>PAM Authentication</title>
2225 <indexterm zone="auth-pam">
2226 <primary>PAM</primary>
2227 </indexterm>
2229 <para>
2230 This authentication method operates similarly to
2231 <literal>password</literal> except that it uses PAM (Pluggable
2232 Authentication Modules) as the authentication mechanism. The
2233 default PAM service name is <literal>postgresql</literal>.
2234 PAM is used only to validate user name/password pairs and optionally the
2235 connected remote host name or IP address. Therefore the user must already
2236 exist in the database before PAM can be used for authentication. For more
2237 information about PAM, please read the
2238 <ulink url="https://www.kernel.org/pub/linux/libs/pam/">
2239 <productname>Linux-PAM</productname> Page</ulink>.
2240 </para>
2242 <para>
2243 The following configuration options are supported for PAM:
2244 <variablelist>
2245 <varlistentry>
2246 <term><literal>pamservice</literal></term>
2247 <listitem>
2248 <para>
2249 PAM service name.
2250 </para>
2251 </listitem>
2252 </varlistentry>
2253 <varlistentry>
2254 <term><literal>pam_use_hostname</literal></term>
2255 <listitem>
2256 <para>
2257 Determines whether the remote IP address or the host name is provided
2258 to PAM modules through the <symbol>PAM_RHOST</symbol> item. By
2259 default, the IP address is used. Set this option to 1 to use the
2260 resolved host name instead. Host name resolution can lead to login
2261 delays. (Most PAM configurations don't use this information, so it is
2262 only necessary to consider this setting if a PAM configuration was
2263 specifically created to make use of it.)
2264 </para>
2265 </listitem>
2266 </varlistentry>
2267 </variablelist>
2268 </para>
2270 <note>
2271 <para>
2272 If PAM is set up to read <filename>/etc/shadow</filename>, authentication
2273 will fail because the PostgreSQL server is started by a non-root
2274 user. However, this is not an issue when PAM is configured to use
2275 LDAP or other authentication methods.
2276 </para>
2277 </note>
2278 </sect1>
2280 <sect1 id="auth-bsd">
2281 <title>BSD Authentication</title>
2283 <indexterm zone="auth-bsd">
2284 <primary>BSD Authentication</primary>
2285 </indexterm>
2287 <para>
2288 This authentication method operates similarly to
2289 <literal>password</literal> except that it uses BSD Authentication
2290 to verify the password. BSD Authentication is used only
2291 to validate user name/password pairs. Therefore the user's role must
2292 already exist in the database before BSD Authentication can be used
2293 for authentication. The BSD Authentication framework is currently
2294 only available on OpenBSD.
2295 </para>
2297 <para>
2298 BSD Authentication in <productname>PostgreSQL</productname> uses
2299 the <literal>auth-postgresql</literal> login type and authenticates with
2300 the <literal>postgresql</literal> login class if that's defined
2301 in <filename>login.conf</filename>. By default that login class does not
2302 exist, and <productname>PostgreSQL</productname> will use the default login class.
2303 </para>
2305 <note>
2306 <para>
2307 To use BSD Authentication, the PostgreSQL user account (that is, the
2308 operating system user running the server) must first be added to
2309 the <literal>auth</literal> group. The <literal>auth</literal> group
2310 exists by default on OpenBSD systems.
2311 </para>
2312 </note>
2313 </sect1>
2315 <sect1 id="client-authentication-problems">
2316 <title>Authentication Problems</title>
2318 <para>
2319 Authentication failures and related problems generally
2320 manifest themselves through error messages like the following:
2321 </para>
2323 <para>
2324 <programlisting>
2325 FATAL: no pg_hba.conf entry for host "123.123.123.123", user "andym", database "testdb"
2326 </programlisting>
2327 This is what you are most likely to get if you succeed in contacting
2328 the server, but it does not want to talk to you. As the message
2329 suggests, the server refused the connection request because it found
2330 no matching entry in its <filename>pg_hba.conf</filename>
2331 configuration file.
2332 </para>
2334 <para>
2335 <programlisting>
2336 FATAL: password authentication failed for user "andym"
2337 </programlisting>
2338 Messages like this indicate that you contacted the server, and it is
2339 willing to talk to you, but not until you pass the authorization
2340 method specified in the <filename>pg_hba.conf</filename> file. Check
2341 the password you are providing, or check your Kerberos or ident
2342 software if the complaint mentions one of those authentication
2343 types.
2344 </para>
2346 <para>
2347 <programlisting>
2348 FATAL: user "andym" does not exist
2349 </programlisting>
2350 The indicated database user name was not found.
2351 </para>
2353 <para>
2354 <programlisting>
2355 FATAL: database "testdb" does not exist
2356 </programlisting>
2357 The database you are trying to connect to does not exist. Note that
2358 if you do not specify a database name, it defaults to the database
2359 user name.
2360 </para>
2362 <tip>
2363 <para>
2364 The server log might contain more information about an
2365 authentication failure than is reported to the client. If you are
2366 confused about the reason for a failure, check the server log.
2367 </para>
2368 </tip>
2369 </sect1>
2371 </chapter>