Commit | Line | Data |
---|---|---|
105369a4 DT |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2015 David Thompson <davet@gnu.org> | |
9b1cee97 | 3 | ;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org> |
8823ed4e | 4 | ;;; Copyright © 2016 Leo Famulari <leo@famulari.name> |
105369a4 DT |
5 | ;;; |
6 | ;;; This file is part of GNU Guix. | |
7 | ;;; | |
8 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
9 | ;;; under the terms of the GNU General Public License as published by | |
10 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
11 | ;;; your option) any later version. | |
12 | ;;; | |
13 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
14 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;;; GNU General Public License for more details. | |
17 | ;;; | |
18 | ;;; You should have received a copy of the GNU General Public License | |
19 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
20 | ||
21 | (define-module (gnu services databases) | |
22 | #:use-module (gnu services) | |
0190c1c0 | 23 | #:use-module (gnu services shepherd) |
105369a4 DT |
24 | #:use-module (gnu system shadow) |
25 | #:use-module (gnu packages admin) | |
26 | #:use-module (gnu packages databases) | |
27 | #:use-module (guix records) | |
105369a4 | 28 | #:use-module (guix gexp) |
0adfe95a | 29 | #:use-module (ice-9 match) |
105369a4 DT |
30 | #:export (postgresql-service)) |
31 | ||
32 | ;;; Commentary: | |
33 | ;;; | |
34 | ;;; Database services. | |
35 | ;;; | |
36 | ;;; Code: | |
37 | ||
0adfe95a LC |
38 | (define-record-type* <postgresql-configuration> |
39 | postgresql-configuration make-postgresql-configuration | |
40 | postgresql-configuration? | |
41 | (postgresql postgresql-configuration-postgresql ;<package> | |
42 | (default postgresql)) | |
43 | (config-file postgresql-configuration-file) | |
44 | (data-directory postgresql-configuration-data-directory)) | |
45 | ||
105369a4 | 46 | (define %default-postgres-hba |
be1c2c54 LC |
47 | (plain-file "pg_hba.conf" |
48 | " | |
105369a4 DT |
49 | local all all trust |
50 | host all all 127.0.0.1/32 trust | |
51 | host all all ::1/128 trust")) | |
52 | ||
53 | (define %default-postgres-ident | |
be1c2c54 | 54 | (plain-file "pg_ident.conf" |
105369a4 DT |
55 | "# MAPNAME SYSTEM-USERNAME PG-USERNAME")) |
56 | ||
57 | (define %default-postgres-config | |
be1c2c54 | 58 | (mixed-text-file "postgresql.conf" |
9b1cee97 | 59 | "log_destination = 'syslog'\n" |
be1c2c54 | 60 | "hba_file = '" %default-postgres-hba "'\n" |
8823ed4e | 61 | "ident_file = '" %default-postgres-ident "'\n")) |
105369a4 | 62 | |
0adfe95a LC |
63 | (define %postgresql-accounts |
64 | (list (user-group (name "postgres") (system? #t)) | |
65 | (user-account | |
66 | (name "postgres") | |
67 | (group "postgres") | |
68 | (system? #t) | |
69 | (comment "PostgreSQL server user") | |
70 | (home-directory "/var/empty") | |
71 | (shell #~(string-append #$shadow "/sbin/nologin"))))) | |
72 | ||
73 | (define postgresql-activation | |
74 | (match-lambda | |
75 | (($ <postgresql-configuration> postgresql config-file data-directory) | |
76 | #~(begin | |
77 | (use-modules (guix build utils) | |
78 | (ice-9 match)) | |
79 | ||
80 | (let ((user (getpwnam "postgres")) | |
81 | (initdb (string-append #$postgresql "/bin/initdb"))) | |
82 | ;; Create db state directory. | |
83 | (mkdir-p #$data-directory) | |
84 | (chown #$data-directory (passwd:uid user) (passwd:gid user)) | |
85 | ||
86 | ;; Drop privileges and init state directory in a new | |
87 | ;; process. Wait for it to finish before proceeding. | |
88 | (match (primitive-fork) | |
89 | (0 | |
90 | ;; Exit with a non-zero status code if an exception is thrown. | |
91 | (dynamic-wind | |
92 | (const #t) | |
93 | (lambda () | |
94 | (setgid (passwd:gid user)) | |
95 | (setuid (passwd:uid user)) | |
96 | (primitive-exit (system* initdb "-D" #$data-directory))) | |
97 | (lambda () | |
98 | (primitive-exit 1)))) | |
99 | (pid (waitpid pid)))))))) | |
100 | ||
d4053c71 | 101 | (define postgresql-shepherd-service |
0adfe95a LC |
102 | (match-lambda |
103 | (($ <postgresql-configuration> postgresql config-file data-directory) | |
104 | (let ((start-script | |
105 | ;; Wrapper script that switches to the 'postgres' user before | |
106 | ;; launching daemon. | |
107 | (program-file "start-postgres" | |
108 | #~(let ((user (getpwnam "postgres")) | |
109 | (postgres (string-append #$postgresql | |
110 | "/bin/postgres"))) | |
111 | (setgid (passwd:gid user)) | |
112 | (setuid (passwd:uid user)) | |
113 | (system* postgres | |
114 | (string-append "--config-file=" | |
115 | #$config-file) | |
116 | "-D" #$data-directory))))) | |
d4053c71 | 117 | (list (shepherd-service |
0adfe95a LC |
118 | (provision '(postgres)) |
119 | (documentation "Run the PostgreSQL daemon.") | |
9b1cee97 | 120 | (requirement '(user-processes loopback syslogd)) |
0adfe95a LC |
121 | (start #~(make-forkexec-constructor #$start-script)) |
122 | (stop #~(make-kill-destructor)))))))) | |
123 | ||
124 | (define postgresql-service-type | |
125 | (service-type (name 'postgresql) | |
126 | (extensions | |
d4053c71 AK |
127 | (list (service-extension shepherd-root-service-type |
128 | postgresql-shepherd-service) | |
0adfe95a LC |
129 | (service-extension activation-service-type |
130 | postgresql-activation) | |
131 | (service-extension account-service-type | |
132 | (const %postgresql-accounts)))))) | |
133 | ||
105369a4 DT |
134 | (define* (postgresql-service #:key (postgresql postgresql) |
135 | (config-file %default-postgres-config) | |
136 | (data-directory "/var/lib/postgresql/data")) | |
137 | "Return a service that runs @var{postgresql}, the PostgreSQL database server. | |
138 | ||
139 | The PostgreSQL daemon loads its runtime configuration from @var{config-file} | |
140 | and stores the database cluster in @var{data-directory}." | |
0adfe95a LC |
141 | (service postgresql-service-type |
142 | (postgresql-configuration | |
143 | (postgresql postgresql) | |
144 | (config-file config-file) | |
145 | (data-directory data-directory)))) |