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