services: postgresql: Don't initdb when directory exists.

* gnu/services/databases.scm (postgresql-activation): Check if
directory exists.

Reviewed-by: Dale Mellor <guix-devel-0brg6b@rdmp.org>
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
Change-Id: Id563a3e8df2cb5b805e64cd8319303c65d308c69
This commit is contained in:
Robert Vollmert 2024-03-08 11:51:01 +00:00 committed by Ludovic Courtès
parent b0a37d8b1a
commit ed03a0aa9b
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5

View file

@ -235,20 +235,7 @@ host all all ::1/128 md5"))
(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)
"/bin/initdb"))
(initdb-args
(append
(if #$locale
(list (string-append "--locale=" #$locale))
'()))))
;; Create db state directory.
(mkdir-p #$data-directory)
(chown #$data-directory (passwd:uid user) (passwd:gid user))
;; Create the socket directory. ;; Create the socket directory.
(let ((socket-directory (let ((socket-directory
#$(postgresql-config-file-socket-directory config-file))) #$(postgresql-config-file-socket-directory config-file)))
@ -261,25 +248,40 @@ host all all ::1/128 md5"))
(mkdir-p #$log-directory) (mkdir-p #$log-directory)
(chown #$log-directory (passwd:uid user) (passwd:gid user))) (chown #$log-directory (passwd:uid user) (passwd:gid user)))
;; Drop privileges and init state directory in a new (unless (file-exists? #$data-directory)
;; process. Wait for it to finish before proceeding. (let ((initdb (string-append
(match (primitive-fork) #$(final-postgresql postgresql
(0 extension-packages)
;; Exit with a non-zero status code if an exception is thrown. "/bin/initdb"))
(dynamic-wind (initdb-args
(const #t) (append
(lambda () (if #$locale
(setgid (passwd:gid user)) (list (string-append "--locale=" #$locale))
(setuid (passwd:uid user)) '()))))
(primitive-exit ;; Create db state directory.
(apply system* (mkdir-p #$data-directory)
initdb (chown #$data-directory (passwd:uid user) (passwd:gid user))
"-D"
#$data-directory ;; Drop privileges and init state directory in a new
initdb-args))) ;; process. Wait for it to finish before proceeding.
(lambda () (match (primitive-fork)
(primitive-exit 1)))) (0
(pid (waitpid pid)))))))) ;; Exit with a non-zero status code if an exception is
;; thrown.
(dynamic-wind
(const #t)
(lambda ()
(setgid (passwd:gid user))
(setuid (passwd:uid user))
(primitive-exit
(apply system*
initdb
"-D"
#$data-directory
initdb-args)))
(lambda ()
(primitive-exit 1))))
(pid (waitpid pid))))))))))
(define postgresql-shepherd-service (define postgresql-shepherd-service
(match-lambda (match-lambda