···564564 services.postgresql.enable = lib.mkIf haveLocalDB true;
565565566566 services.postgresql.identMap = lib.optionalString haveLocalDB ''
567567- hydra-users hydra hydra
568568- hydra-users hydra-queue-runner hydra
569569- hydra-users hydra-www hydra
570570- hydra-users root hydra
571571- # The postgres user is used to create the pg_trgm extension for the hydra database
572572- hydra-users postgres postgres
567567+ hydra hydra hydra
568568+ hydra hydra-queue-runner hydra
569569+ hydra hydra-www hydra
570570+ hydra root hydra
573571 '';
574572575573 services.postgresql.authentication = lib.optionalString haveLocalDB ''
576576- local hydra all ident map=hydra-users
574574+ local all hydra peer map=hydra
577575 '';
578576579577 };
+32
nixos/modules/services/databases/postgresql.md
···170170 }
171171```
172172173173+## Authentication {#module-services-postgres-authentication}
174174+175175+Local connections are made through unix sockets by default and support [peer authentication](https://www.postgresql.org/docs/current/auth-peer.html).
176176+This allows system users to login with database roles of the same name.
177177+For example, the `postgres` system user is allowed to login with the database role `postgres`.
178178+179179+System users and database roles might not always match.
180180+In this case, to allow access for a service, you can create a [user name map](https://www.postgresql.org/docs/current/auth-username-maps.html) between system roles and an existing database role.
181181+182182+### User Mapping {#module-services-postgres-authentication-user-mapping}
183183+184184+Assume that your app creates a role `admin` and you want the `root` user to be able to login with it.
185185+You can then use [](#opt-services.postgresql.identMap) to define the map and [](#opt-services.postgresql.authentication) to enable it:
186186+187187+```nix
188188+services.postgresql = {
189189+ identMap = ''
190190+ admin root admin
191191+ '';
192192+ authentication = ''
193193+ local all admin peer map=admin
194194+ '';
195195+}
196196+```
197197+198198+::: {.warning}
199199+To avoid conflicts with other modules, you should never apply a map to `all` roles.
200200+Because PostgreSQL will stop on the first matching line in `pg_hba.conf`, a line matching all roles would lock out other services.
201201+Each module should only manage user maps for the database roles that belong to this module.
202202+Best practice is to name the map after the database role it manages to avoid name conflicts.
203203+:::
204204+173205## Upgrading {#module-services-postgres-upgrading}
174206175207::: {.note}
+16
nixos/modules/services/databases/postgresql.nix
···274274 Defines the mapping from system users to database users.
275275276276 See the [auth doc](https://postgresql.org/docs/current/auth-username-maps.html).
277277+278278+ There is a default map "postgres" which is used for local peer authentication
279279+ as the postgres superuser role.
280280+ For example, to allow the root user to login as the postgres superuser, add:
281281+282282+ ```
283283+ postgres root postgres
284284+ ```
277285 '';
278286 };
279287···674682 (mkBefore "# Generated file; do not edit!")
675683 (mkAfter ''
676684 # default value of services.postgresql.authentication
685685+ local all postgres peer map=postgres
677686 local all all peer
678687 host all all 127.0.0.1/32 md5
679688 host all all ::1/128 md5
680689 '')
681690 ];
691691+692692+ # The default allows to login with the same database username as the current system user.
693693+ # This is the default for peer authentication without a map, but needs to be made explicit
694694+ # once a map is used.
695695+ services.postgresql.identMap = mkAfter ''
696696+ postgres postgres postgres
697697+ '';
682698683699 services.postgresql.systemCallFilter = mkMerge [
684700 (mapAttrs (const mkDefault) {
+4-1
nixos/tests/postgresql/postgresql.nix
···5454 services.postgresql = {
5555 inherit package;
5656 enable = true;
5757+ identMap = ''
5858+ postgres root postgres
5959+ '';
5760 # TODO(@Ma27) split this off into its own VM test and move a few other
5861 # extension tests to use postgresqlTestExtension.
5962 extensions = ps: with ps; [ plv8 ];
···7376 in
7477 ''
7578 def check_count(statement, lines):
7676- return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format(
7979+ return 'test $(psql -U postgres postgres -tAc "{}"|wc -l) -eq {}'.format(
7780 statement, lines
7881 )
7982