Skip to content

Commit f42f8a6

Browse files
committed
nixos/postgresql: replace deprecated usage of PermissionsStartOnly
1 parent e50e89e commit f42f8a6

1 file changed

Lines changed: 40 additions & 39 deletions

File tree

nixos/modules/services/databases/postgresql.nix

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -292,34 +292,28 @@ in
292292

293293
preStart =
294294
''
295-
# Cleanup the data directory.
296295
if ! test -e ${cfg.dataDir}/PG_VERSION; then
296+
# Cleanup the data directory.
297297
rm -f ${cfg.dataDir}/*.conf
298-
fi
299-
'';
300298
301-
script =
302-
''
303-
# Initialise the database.
304-
if ! test -e ${cfg.dataDir}/PG_VERSION; then
299+
# Initialise the database.
305300
initdb -U ${cfg.superUser} ${concatStringsSep " " cfg.initdbArgs}
301+
306302
# See postStart!
307303
touch "${cfg.dataDir}/.first_startup"
308304
fi
305+
309306
ln -sfn "${configFile}" "${cfg.dataDir}/postgresql.conf"
310307
${optionalString (cfg.recoveryConfig != null) ''
311308
ln -sfn "${pkgs.writeText "recovery.conf" cfg.recoveryConfig}" \
312309
"${cfg.dataDir}/recovery.conf"
313310
''}
314-
315-
exec postgres
316311
'';
317312

318313
serviceConfig = mkMerge [
319314
{ ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
320315
User = "postgres";
321316
Group = "postgres";
322-
PermissionsStartOnly = true;
323317
RuntimeDirectory = "postgresql";
324318
Type = if versionAtLeast cfg.package.version "9.6"
325319
then "notify"
@@ -333,42 +327,49 @@ in
333327
# Give Postgres a decent amount of time to clean up after
334328
# receiving systemd's SIGINT.
335329
TimeoutSec = 120;
330+
331+
ExecStart = "${postgresql}/bin/postgres";
332+
333+
# Wait for PostgreSQL to be ready to accept connections.
334+
ExecStartPost =
335+
let
336+
setupScript = pkgs.writeScript "postgresql-setup" ''
337+
#!${pkgs.runtimeShell} -e
338+
339+
PSQL="${pkgs.utillinux}/bin/runuser -u ${cfg.superUser} -- psql --port=${toString cfg.port}"
340+
341+
while ! $PSQL -d postgres -c "" 2> /dev/null; do
342+
if ! kill -0 "$MAINPID"; then exit 1; fi
343+
sleep 0.1
344+
done
345+
346+
if test -e "${cfg.dataDir}/.first_startup"; then
347+
${optionalString (cfg.initialScript != null) ''
348+
$PSQL -f "${cfg.initialScript}" -d postgres
349+
''}
350+
rm -f "${cfg.dataDir}/.first_startup"
351+
fi
352+
'' + optionalString (cfg.ensureDatabases != []) ''
353+
${concatMapStrings (database: ''
354+
$PSQL -tAc "SELECT 1 FROM pg_database WHERE datname = '${database}'" | grep -q 1 || $PSQL -tAc 'CREATE DATABASE "${database}"'
355+
'') cfg.ensureDatabases}
356+
'' + ''
357+
${concatMapStrings (user: ''
358+
$PSQL -tAc "SELECT 1 FROM pg_roles WHERE rolname='${user.name}'" | grep -q 1 || $PSQL -tAc 'CREATE USER "${user.name}"'
359+
${concatStringsSep "\n" (mapAttrsToList (database: permission: ''
360+
$PSQL -tAc 'GRANT ${permission} ON ${database} TO "${user.name}"'
361+
'') user.ensurePermissions)}
362+
'') cfg.ensureUsers}
363+
'';
364+
in
365+
"+${setupScript}";
336366
}
337367
(mkIf (cfg.dataDir == "/var/lib/postgresql/${cfg.package.psqlSchema}") {
338368
StateDirectory = "postgresql postgresql/${cfg.package.psqlSchema}";
339369
StateDirectoryMode = if groupAccessAvailable then "0750" else "0700";
340370
})
341371
];
342372

343-
# Wait for PostgreSQL to be ready to accept connections.
344-
postStart =
345-
''
346-
PSQL="${pkgs.utillinux}/bin/runuser -u ${cfg.superUser} -- psql --port=${toString cfg.port}"
347-
348-
while ! $PSQL -d postgres -c "" 2> /dev/null; do
349-
if ! kill -0 "$MAINPID"; then exit 1; fi
350-
sleep 0.1
351-
done
352-
353-
if test -e "${cfg.dataDir}/.first_startup"; then
354-
${optionalString (cfg.initialScript != null) ''
355-
$PSQL -f "${cfg.initialScript}" -d postgres
356-
''}
357-
rm -f "${cfg.dataDir}/.first_startup"
358-
fi
359-
'' + optionalString (cfg.ensureDatabases != []) ''
360-
${concatMapStrings (database: ''
361-
$PSQL -tAc "SELECT 1 FROM pg_database WHERE datname = '${database}'" | grep -q 1 || $PSQL -tAc 'CREATE DATABASE "${database}"'
362-
'') cfg.ensureDatabases}
363-
'' + ''
364-
${concatMapStrings (user: ''
365-
$PSQL -tAc "SELECT 1 FROM pg_roles WHERE rolname='${user.name}'" | grep -q 1 || $PSQL -tAc 'CREATE USER "${user.name}"'
366-
${concatStringsSep "\n" (mapAttrsToList (database: permission: ''
367-
$PSQL -tAc 'GRANT ${permission} ON ${database} TO "${user.name}"'
368-
'') user.ensurePermissions)}
369-
'') cfg.ensureUsers}
370-
'';
371-
372373
unitConfig.RequiresMountsFor = "${cfg.dataDir}";
373374
};
374375

0 commit comments

Comments
 (0)