September 26, 2024: PostgreSQL 17 Released!
Supported Versions: Current (17) / 16 / 15 / 14 / 13 / 12
Development Versions: devel
Unsupported versions: 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

4.2. Authentication methods

The following describes the authentication methods in detail.

4.2.1. Password authentication

Postgres database passwords are separate from any operating system user passwords. Ordinarily, the password for each database user is stored in the pg_shadow system catalog table. Passwords can be managed with the query language commands CREATE USER and ALTER USER, e.g., CREATE USER foo WITH PASSWORD 'secret';. By default, that is, if no password has been set up, the stored password is NULL and password authentication will always fail for that user.

To restrict the set of users that are allowed to connect to certain databases, list the set of users in a separate file (one user name per line) in the same directory that pg_hba.conf is in, and mention the (base) name of the file after the password or crypt keyword, respectively, in pg_hba.conf. If you do not use this feature, then any user that is known to the database system can connect to any database (so long as he passes password authentication, of course).

These files can also be used to apply a different set of passwords to a particular database or set thereof. In that case, the files have a format similar to the standard Unix password file /etc/passwd, that is,

username:password
Any extra colon separated fields following the password are ignored. The password is expected to be encrypted using the system's crypt() function. The utility program pg_passwd that is installed with Postgres can be used to manage these password files.

Lines with and without passwords can be mixed in secondary password files. Lines without password indicate use of the main password in pg_shadow that is managed by CREATE USER and ALTER USER. Lines with passwords will cause that password to be used. A password entry of "+" also means using the pg_shadow password.

Alternative passwords cannot be used when using the crypt method. The file will still be evaluated as usual but the password field will simply be ignored and the pg_shadow password will be used.

Note that using alternative passwords like this means that one can no longer use ALTER USER to change one's password. It will still appear to work but the password one is actually changing is not the password that the system will end up using.

4.2.2. Kerberos authentication

Kerberos is an industry-standard secure authentication system suitable for distributed computing over a public network. A description of the Kerberos system is far beyond the scope of this document; in all generality it can be quite complex (yet powerful). The Kerberos FAQ or MIT Project Athena can be a good starting point for exploration. Several sources for Kerberos distributions exist.

In order to use Kerberos, support for it must be enabled at build time. Both Kerberos 4 and 5 are supported (./configure --with-krb4 or ./configure --with-krb5 respectively).

Postgres should operate like a normal Kerberos service. The name of the service principal is normally postgres, unless it was changed during the build. Make sure that your server key file is readable (and preferably only readable) by the Postgres server account (see Section 3.1). The location of the key file is specified with the krb_server_keyfile run time configuration parameter. (See also Section 3.4.) The default is /etc/srvtab if you are using Kerberos 4 and FILE:/usr/local/pgsql/etc/krb5.keytab (or whichever directory was specified as sysconfdir at build time) with Kerberos 5.

To generate the keytab file, use for example (with version 5)

kadmin% ank -randkey postgres/server.my.domain.org
kadmin% ktadd -k krb5.keytab postgres/server.my.domain.org
Read the Kerberos documentation for defails.

In the Kerberos 5 hooks, the following assumptions are made about user and service naming:

  • User principal names (anames) are assumed to contain the actual Unix/Postgres user name in the first component.

  • The Postgres service is assumed to be have two components, the service name and a hostname, canonicalized as in Version 4 (i.e., with all domain suffixes removed).

Parameter Example
user frew@S2K.ORG
user aoki/HOST=miyu.S2K.Berkeley.EDU@S2K.ORG
host postgres_dbms/ucbvax@S2K.ORG

If you use mod_auth_krb and mod_perl on your Apache web server, you can use AuthType KerberosV5SaveCredentials with a mod_perl script. This gives secure database access over the web, no extra passwords required.

4.2.3. Ident-based authentication

The "Identification Protocol" is described in RFC 1413. Virtually every Unix-like operating system ships with an ident server that listens on TCP port 113 by default. The basic functionality of an ident server is to answer questions like "What user initiated the connection that goes out of your port X and connects to my port Y?". Since Postgres knows both X and Y when a physical connection is established, it can interrogate the ident server on the host of the connecting client and could theoretically determine the operating system user for any given connection this way.

The drawback of this procedure is that it depends on the integrity of the client: if the client machine is untrusted or compromised an attacker could run just about any program on port 113 and return any user name he chooses. This authentication method is therefore only appropriate for closed networks where each client machine is under tight control and where the database and system administrators operate in close contact. Heed the warning:

 

The Identification Protocol is not intended as an authorization or access control protocol.

 
--RFC 1413  

When using ident-based authentication, after having determined the operating system user that initiated the connection, Postgres determines as what database system user he may connect. This is controlled by the ident map argument that follows the ident keyword in the pg_hba.conf file. The simplest ident map is sameuser, which allows any operating system user to connect as the database user of the same name (if the latter exists). Other maps must be created manually.

Ident maps are held in the file pg_ident.conf in the data directory, which contains lines of the general form:

map-name ident-username database-username
Comments and whitespace are handled in the usual way. The map-name is an arbitrary name that will be used to refer to this mapping in pg_hba.conf. The other two fields specify which operating system user is allowed to connect as which database user. The same map-name can be used repeatedly to specify more user-mappings. There is also no restriction regarding how many database users a given operating system may correspond to and vice versa.

A pg_ident.conf file that could be used in conjunction with the pg_hba.conf file in Example 4-1 is shown in Example 4-2. In this example setup, anyone logged in to a machine on the 192.168 network that does not have the Unix user name bryanh, ann, or robert would not be granted access. Unix user robert would only be allowed access when he tries to connect as Postgres user "bob", not as "robert" or anyone else. "ann" would only be allowed to connect as "ann". User bryanh would be allowed to connect as either "bryanh" himself or as "guest1".

Example 4-2. An example pg_ident.conf file

#MAP           IDENT-NAME   POSTGRESQL-NAME

omicron        bryanh       bryanh
omicron        ann          ann
# bob has username robert on these machines
omicron        robert       bob
# bryanh can also connect as guest1
omicron        bryanh       guest1