gnu: postgresql: Add extension-packages.

* gnu/services/databases.scm (postgresql-configuration): Add
extension-packages.
(postgresql-shepherd-service): New key #:extension-packages.
* doc/guix.texi (Database Services): Document it.
This commit is contained in:
Julien Lepiller 2018-09-14 21:44:08 +02:00
parent c336567dad
commit 0d57a50af2
No known key found for this signature in database
GPG Key ID: 43111F4520086A0C
2 changed files with 84 additions and 17 deletions

View File

@ -13383,13 +13383,49 @@ The @code{(gnu services databases)} module provides the following services.
@deffn {Scheme Procedure} postgresql-service [#:postgresql postgresql] @ @deffn {Scheme Procedure} postgresql-service [#:postgresql postgresql] @
[#:config-file] [#:data-directory ``/var/lib/postgresql/data''] @ [#:config-file] [#:data-directory ``/var/lib/postgresql/data''] @
[#:port 5432] [#:locale ``en_US.utf8''] [#:port 5432] [#:locale ``en_US.utf8''] [#:extension-packages '()]
Return a service that runs @var{postgresql}, the PostgreSQL database Return a service that runs @var{postgresql}, the PostgreSQL database
server. server.
The PostgreSQL daemon loads its runtime configuration from @var{config-file}, The PostgreSQL daemon loads its runtime configuration from @var{config-file},
creates a database cluster with @var{locale} as the default creates a database cluster with @var{locale} as the default
locale, stored in @var{data-directory}. It then listens on @var{port}. locale, stored in @var{data-directory}. It then listens on @var{port}.
@cindex postgresql extension-packages
Additional extensions are loaded from packages listed in
@var{extension-packages}. Extensions are available at runtime. For instance,
to create a geographic database using the @code{postgis} extension, a user can
configure the postgresql-service as in this example:
@cindex postgis
@example
(use-package-modules databases geo)
(operating-system
...
;; postgresql is required to run `psql' but postgis is not required for
;; proper operation.
(packages (cons* postgresql %base-packages))
(services
(cons*
(postgresql-service #:extension-packages (list postgis))
%base-services)))
@end example
Then the extension becomes visible and you can initialise an empty geographic
database in this way:
@example
psql -U postgres
> create database postgistest;
> \connect postgistest;
> create extension postgis;
> create extension postgis_topology;
@end example
There is no need to add this field for contrib extensions such as hstore or
dblink as they are already loadable by postgresql. This field is only
required to add extensions provided by other packages.
@end deffn @end deffn
@deffn {Scheme Procedure} mysql-service [#:config (mysql-configuration)] @deffn {Scheme Procedure} mysql-service [#:config (mysql-configuration)]

View File

@ -4,6 +4,7 @@
;;; Copyright © 2016 Leo Famulari <leo@famulari.name> ;;; Copyright © 2016 Leo Famulari <leo@famulari.name>
;;; Copyright © 2017 Christopher Baines <mail@cbaines.net> ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org> ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
;;; ;;;
@ -26,7 +27,10 @@
#:use-module (gnu system shadow) #:use-module (gnu system shadow)
#:use-module (gnu packages admin) #:use-module (gnu packages admin)
#:use-module (gnu packages databases) #:use-module (gnu packages databases)
#:use-module (guix build-system trivial)
#:use-module (guix build union)
#:use-module (guix modules) #:use-module (guix modules)
#:use-module (guix packages)
#:use-module (guix records) #:use-module (guix records)
#:use-module (guix gexp) #:use-module (guix gexp)
#:use-module (srfi srfi-1) #:use-module (srfi srfi-1)
@ -141,16 +145,18 @@ host all all ::1/128 trust"))
(define-record-type* <postgresql-configuration> (define-record-type* <postgresql-configuration>
postgresql-configuration make-postgresql-configuration postgresql-configuration make-postgresql-configuration
postgresql-configuration? postgresql-configuration?
(postgresql postgresql-configuration-postgresql ;<package> (postgresql postgresql-configuration-postgresql ;<package>
(default postgresql)) (default postgresql))
(port postgresql-configuration-port (port postgresql-configuration-port
(default 5432)) (default 5432))
(locale postgresql-configuration-locale (locale postgresql-configuration-locale
(default "en_US.utf8")) (default "en_US.utf8"))
(config-file postgresql-configuration-file (config-file postgresql-configuration-file
(default (postgresql-config-file))) (default (postgresql-config-file)))
(data-directory postgresql-configuration-data-directory (data-directory postgresql-configuration-data-directory
(default "/var/lib/postgresql/data"))) (default "/var/lib/postgresql/data"))
(extension-packages postgresql-configuration-extension-packages
(default '())))
(define %postgresql-accounts (define %postgresql-accounts
(list (user-group (name "postgres") (system? #t)) (list (user-group (name "postgres") (system? #t))
@ -162,15 +168,36 @@ host all all ::1/128 trust"))
(home-directory "/var/empty") (home-directory "/var/empty")
(shell (file-append shadow "/sbin/nologin"))))) (shell (file-append shadow "/sbin/nologin")))))
(define (final-postgresql postgresql extension-packages)
(if (null? extension-packages)
postgresql
(package
(inherit postgresql)
(source #f)
(build-system trivial-build-system)
(arguments
`(#:modules ((guix build utils) (guix build union))
#:builder
(begin
(use-modules (guix build utils) (guix build union) (srfi srfi-26))
(union-build (assoc-ref %outputs "out") (map (lambda (input) (cdr input)) %build-inputs))
#t)))
(inputs
`(("postgresql" ,postgresql)
,@(map (lambda (extension) (list "extension" extension))
extension-packages))))))
(define postgresql-activation (define postgresql-activation
(match-lambda (match-lambda
(($ <postgresql-configuration> postgresql port locale config-file data-directory) (($ <postgresql-configuration> postgresql port locale config-file data-directory
extension-packages)
#~(begin #~(begin
(use-modules (guix build utils) (use-modules (guix build utils)
(ice-9 match)) (ice-9 match))
(let ((user (getpwnam "postgres")) (let ((user (getpwnam "postgres"))
(initdb (string-append #$postgresql "/bin/initdb")) (initdb (string-append #$(final-postgresql postgresql extension-packages)
"/bin/initdb"))
(initdb-args (initdb-args
(append (append
(if #$locale (if #$locale
@ -202,7 +229,8 @@ host all all ::1/128 trust"))
(define postgresql-shepherd-service (define postgresql-shepherd-service
(match-lambda (match-lambda
(($ <postgresql-configuration> postgresql port locale config-file data-directory) (($ <postgresql-configuration> postgresql port locale config-file data-directory
extension-packages)
(let* ((pg_ctl-wrapper (let* ((pg_ctl-wrapper
;; Wrapper script that switches to the 'postgres' user before ;; Wrapper script that switches to the 'postgres' user before
;; launching daemon. ;; launching daemon.
@ -214,7 +242,8 @@ host all all ::1/128 trust"))
(match (command-line) (match (command-line)
((_ mode) ((_ mode)
(let ((user (getpwnam "postgres")) (let ((user (getpwnam "postgres"))
(pg_ctl #$(file-append postgresql "/bin/pg_ctl")) (pg_ctl #$(file-append (final-postgresql postgresql extension-packages)
"/bin/pg_ctl"))
(options (format #f "--config-file=~a -p ~d" (options (format #f "--config-file=~a -p ~d"
#$config-file #$port))) #$config-file #$port)))
(setgid (passwd:gid user)) (setgid (passwd:gid user))
@ -253,7 +282,8 @@ host all all ::1/128 trust"))
(port 5432) (port 5432)
(locale "en_US.utf8") (locale "en_US.utf8")
(config-file (postgresql-config-file)) (config-file (postgresql-config-file))
(data-directory "/var/lib/postgresql/data")) (data-directory "/var/lib/postgresql/data")
(extension-packages '()))
"Return a service that runs @var{postgresql}, the PostgreSQL database server. "Return a service that runs @var{postgresql}, the PostgreSQL database server.
The PostgreSQL daemon loads its runtime configuration from @var{config-file} The PostgreSQL daemon loads its runtime configuration from @var{config-file}
@ -264,7 +294,8 @@ and stores the database cluster in @var{data-directory}."
(port port) (port port)
(locale locale) (locale locale)
(config-file config-file) (config-file config-file)
(data-directory data-directory)))) (data-directory data-directory)
(extension-packages extension-packages))))
;;; ;;;