services: postgresql: Add log directory support.
* gnu/services/databases.scm (postgresql-configuration-log-directory): New procedure. (<postgresql-configuration>)[log-directory]: New field. (postgresql-activation): Create the log directory. (postgresql-shepherd-service): Honor it. * gnu/tests/databases.scm (%postgresql-log-directory): New variable. (log-file): New test case. * doc/guix.texi (Database Services): Document it.
This commit is contained in:
parent
6c0679215f
commit
fe4b882350
3 changed files with 53 additions and 8 deletions
|
@ -19344,6 +19344,11 @@ The configuration file to use when running PostgreSQL@. The default
|
||||||
behaviour uses the postgresql-config-file record with the default values
|
behaviour uses the postgresql-config-file record with the default values
|
||||||
for the fields.
|
for the fields.
|
||||||
|
|
||||||
|
@item @code{log-directory} (default: @code{"/var/log/postgresql"})
|
||||||
|
The directory where @command{pg_ctl} output will be written in a file
|
||||||
|
named @code{"pg_ctl.log"}. This file can be useful to debug PostgreSQL
|
||||||
|
configuration errors for instance.
|
||||||
|
|
||||||
@item @code{data-directory} (default: @code{"/var/lib/postgresql/data"})
|
@item @code{data-directory} (default: @code{"/var/lib/postgresql/data"})
|
||||||
Directory in which to store the data.
|
Directory in which to store the data.
|
||||||
|
|
||||||
|
|
|
@ -52,6 +52,7 @@
|
||||||
postgresql-configuration-port
|
postgresql-configuration-port
|
||||||
postgresql-configuration-locale
|
postgresql-configuration-locale
|
||||||
postgresql-configuration-file
|
postgresql-configuration-file
|
||||||
|
postgresql-configuration-log-directory
|
||||||
postgresql-configuration-data-directory
|
postgresql-configuration-data-directory
|
||||||
|
|
||||||
postgresql-service
|
postgresql-service
|
||||||
|
@ -164,6 +165,8 @@ host all all ::1/128 md5"))
|
||||||
(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)))
|
||||||
|
(log-directory postgresql-configuration-log-directory
|
||||||
|
(default "/var/log/postgresql"))
|
||||||
(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
|
(extension-packages postgresql-configuration-extension-packages
|
||||||
|
@ -200,15 +203,18 @@ host all all ::1/128 md5"))
|
||||||
|
|
||||||
(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
|
||||||
extension-packages)
|
log-directory 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 #$(final-postgresql postgresql extension-packages)
|
(initdb (string-append
|
||||||
"/bin/initdb"))
|
#$(final-postgresql postgresql
|
||||||
|
extension-packages)
|
||||||
|
"/bin/initdb"))
|
||||||
(initdb-args
|
(initdb-args
|
||||||
(append
|
(append
|
||||||
(if #$locale
|
(if #$locale
|
||||||
|
@ -225,6 +231,11 @@ host all all ::1/128 md5"))
|
||||||
(mkdir-p socket-directory)
|
(mkdir-p socket-directory)
|
||||||
(chown socket-directory (passwd:uid user) (passwd:gid user))))
|
(chown socket-directory (passwd:uid user) (passwd:gid user))))
|
||||||
|
|
||||||
|
;; Create the log directory.
|
||||||
|
(when (string? #$log-directory)
|
||||||
|
(mkdir-p #$log-directory)
|
||||||
|
(chown #$log-directory (passwd:uid user) (passwd:gid user)))
|
||||||
|
|
||||||
;; Drop privileges and init state directory in a new
|
;; Drop privileges and init state directory in a new
|
||||||
;; process. Wait for it to finish before proceeding.
|
;; process. Wait for it to finish before proceeding.
|
||||||
(match (primitive-fork)
|
(match (primitive-fork)
|
||||||
|
@ -247,8 +258,9 @@ host all all ::1/128 md5"))
|
||||||
|
|
||||||
(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
|
||||||
extension-packages)
|
log-directory 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.
|
||||||
|
@ -260,13 +272,21 @@ host all all ::1/128 md5"))
|
||||||
(match (command-line)
|
(match (command-line)
|
||||||
((_ mode)
|
((_ mode)
|
||||||
(let ((user (getpwnam "postgres"))
|
(let ((user (getpwnam "postgres"))
|
||||||
(pg_ctl #$(file-append (final-postgresql postgresql extension-packages)
|
(pg_ctl #$(file-append
|
||||||
|
(final-postgresql postgresql
|
||||||
|
extension-packages)
|
||||||
"/bin/pg_ctl"))
|
"/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))
|
||||||
(setuid (passwd:uid user))
|
(setuid (passwd:uid user))
|
||||||
(execl pg_ctl pg_ctl "-D" #$data-directory "-o" options
|
(execl pg_ctl pg_ctl "-D" #$data-directory
|
||||||
|
#$@(if (string? log-directory)
|
||||||
|
(list "-l"
|
||||||
|
(string-append log-directory
|
||||||
|
"/pg_ctl.log"))
|
||||||
|
'())
|
||||||
|
"-o" options
|
||||||
mode)))))))
|
mode)))))))
|
||||||
(pid-file (in-vicinity data-directory "postmaster.pid"))
|
(pid-file (in-vicinity data-directory "postmaster.pid"))
|
||||||
(action (lambda args
|
(action (lambda args
|
||||||
|
|
|
@ -214,6 +214,9 @@
|
||||||
;;; The PostgreSQL service.
|
;;; The PostgreSQL service.
|
||||||
;;;
|
;;;
|
||||||
|
|
||||||
|
(define %postgresql-log-directory
|
||||||
|
"/var/log/postgresql")
|
||||||
|
|
||||||
(define %postgresql-os
|
(define %postgresql-os
|
||||||
(simple-operating-system
|
(simple-operating-system
|
||||||
(service postgresql-service-type
|
(service postgresql-service-type
|
||||||
|
@ -262,6 +265,23 @@
|
||||||
(start-service 'postgres))
|
(start-service 'postgres))
|
||||||
marionette))
|
marionette))
|
||||||
|
|
||||||
|
(test-assert "log-file"
|
||||||
|
(marionette-eval
|
||||||
|
'(begin
|
||||||
|
(use-modules (ice-9 ftw)
|
||||||
|
(ice-9 match))
|
||||||
|
(current-output-port
|
||||||
|
(open-file "/dev/console" "w0"))
|
||||||
|
(let ((server-log-file
|
||||||
|
(string-append #$%postgresql-log-directory
|
||||||
|
"/pg_ctl.log")))
|
||||||
|
(and (file-exists? server-log-file)
|
||||||
|
(display
|
||||||
|
(call-with-input-file server-log-file
|
||||||
|
get-string-all)))
|
||||||
|
#t))
|
||||||
|
marionette))
|
||||||
|
|
||||||
(test-end)
|
(test-end)
|
||||||
(exit (= (test-runner-fail-count (test-runner-current)) 0)))))
|
(exit (= (test-runner-fail-count (test-runner-current)) 0)))))
|
||||||
|
|
||||||
|
|
Reference in a new issue