From dd1f67a5682eb126f118d7aedd0842d855e38b19 Mon Sep 17 00:00:00 2001 From: Julien Lepiller Date: Fri, 14 Sep 2018 21:44:08 +0200 Subject: [PATCH 2/2] gnu: postgresql: Add extensions. * gnu/services/databases.scm (postgresql-configuration): Add extensions. (postgresql-shepherd-service): New key #:extensions. * doc/guix.texi (Database Services): Document it. --- doc/guix.texi | 34 +++++++++++++++++++++++++++++++- gnu/services/databases.scm | 40 +++++++++++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index cccf166d0..ecd486112 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -13718,13 +13718,45 @@ The @code{(gnu services databases)} module provides the following services. @deffn {Scheme Procedure} postgresql-service [#:postgresql postgresql] @ [#:config-file] [#:data-directory ``/var/lib/postgresql/data''] @ - [#:port 5432] [#:locale ``en_US.utf8''] + [#:port 5432] [#:locale ``en_US.utf8''] [#:extensions '()] Return a service that runs @var{postgresql}, the PostgreSQL database server. The PostgreSQL daemon loads its runtime configuration from @var{config-file}, creates a database cluster with @var{locale} as the default locale, stored in @var{data-directory}. It then listens on @var{port}. + address@hidden postgresql extensions +Additional extensions are loaded from packages listed in @var{extensions}. +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: + address@hidden postgis address@hidden +(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 #:extensions (list postgis)) + %base-services))) address@hidden example + +Then the extension becomes visible and you can initialise an empty geographic +database in this way: + address@hidden +psql -U postgres +> create database postgistest; +> \connect postgistest; +> create extension postgis; +> create extension postgis_topology; address@hidden example @end deffn @deffn {Scheme Procedure} mysql-service [#:config (mysql-configuration)] diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm index aff78a056..59cd36c3f 100644 --- a/gnu/services/databases.scm +++ b/gnu/services/databases.scm @@ -4,6 +4,7 @@ ;;; Copyright © 2016 Leo Famulari ;;; Copyright © 2017 Christopher Baines ;;; Copyright © 2018 Clément Lassieur +;;; Copyright © 2018 Julien Lepiller ;;; ;;; This file is part of GNU Guix. ;;; @@ -26,7 +27,10 @@ #:use-module (gnu system shadow) #:use-module (gnu packages admin) #:use-module (gnu packages databases) + #:use-module (guix build-system trivial) + #:use-module (guix build union) #:use-module (guix modules) + #:use-module (guix packages) #:use-module (guix records) #:use-module (guix gexp) #:use-module (srfi srfi-1) @@ -150,7 +154,9 @@ host all all ::1/128 trust")) (config-file postgresql-configuration-file (default (postgresql-config-file))) (data-directory postgresql-configuration-data-directory - (default "/var/lib/postgresql/data"))) + (default "/var/lib/postgresql/data")) + (extensions postgresql-configuration-extensions + (default '()))) (define %postgresql-accounts (list (user-group (name "postgres") (system? #t)) @@ -162,15 +168,33 @@ host all all ::1/128 trust")) (home-directory "/var/empty") (shell (file-append shadow "/sbin/nologin"))))) +(define (final-postgresql postgresql extensions) + (if (null? extensions) + 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)) extensions)))))) + (define postgresql-activation (match-lambda - (($ postgresql port locale config-file data-directory) + (($ postgresql port locale config-file data-directory extensions) #~(begin (use-modules (guix build utils) (ice-9 match)) (let ((user (getpwnam "postgres")) - (initdb (string-append #$postgresql "/bin/initdb")) + (initdb (string-append #$(final-postgresql postgresql extensions) "/bin/initdb")) (initdb-args (append (if #$locale @@ -202,7 +226,7 @@ host all all ::1/128 trust")) (define postgresql-shepherd-service (match-lambda - (($ postgresql port locale config-file data-directory) + (($ postgresql port locale config-file data-directory extensions) (let* ((pg_ctl-wrapper ;; Wrapper script that switches to the 'postgres' user before ;; launching daemon. @@ -214,7 +238,7 @@ host all all ::1/128 trust")) (match (command-line) ((_ mode) (let ((user (getpwnam "postgres")) - (pg_ctl #$(file-append postgresql "/bin/pg_ctl")) + (pg_ctl #$(file-append (final-postgresql postgresql extensions) "/bin/pg_ctl")) (options (format #f "--config-file=~a -p ~d" #$config-file #$port))) (setgid (passwd:gid user)) @@ -253,7 +277,8 @@ host all all ::1/128 trust")) (port 5432) (locale "en_US.utf8") (config-file (postgresql-config-file)) - (data-directory "/var/lib/postgresql/data")) + (data-directory "/var/lib/postgresql/data") + (extensions '())) "Return a service that runs @var{postgresql}, the PostgreSQL database server. The PostgreSQL daemon loads its runtime configuration from @var{config-file} @@ -264,7 +289,8 @@ and stores the database cluster in @var{data-directory}." (port port) (locale locale) (config-file config-file) - (data-directory data-directory)))) + (data-directory data-directory) + (extensions extensions)))) ;;; -- 2.18.0