Change:
# Give Postgres a decent amount of time to clean up after # receiving systemd's SIGINT. TimeoutSec = 120; <del> }; # Wait for PostgreSQL to be ready to accept connections. postStart = '' PSQL="${pkgs.utillinux}/bin/runuser -u ${cfg.superUser} -- psql --port=${toString cfg.port}" while ! $PSQL -d postgres -c "" 2> /dev/null; do if ! kill -0 "$MAINPID"; then exit 1; fi sleep 0.1 done if test -e "${cfg.dataDir}/.first_startup"; then ${optionalString (cfg.initialScript != null) '' $PSQL -f "${cfg.initialScript}" -d postgres ''} rm -f "${cfg.dataDir}/.first_startup" fi '' + optionalString (cfg.ensureDatabases != []) '' ${concatMapStrings (database: '' $PSQL -tAc "SELECT 1 FROM pg_database WHERE datname = '${database}'" | grep -q 1 || $PSQL -tAc 'CREATE DATABASE "${database}"' '') cfg.ensureDatabases} '' + '' ${concatMapStrings (user: '' $PSQL -tAc "SELECT 1 FROM pg_roles WHERE rolname='${user.name}'" | grep -q 1 || $PSQL -tAc 'CREATE USER "${user.name}"' ${concatStringsSep "n" (mapAttrsToList (database: permission: '' $PSQL -tAc 'GRANT ${permission} ON ${database} TO "${user.name}"' '') user.ensurePermissions)} '') cfg.ensureUsers} ''; </del> <ins> ExecStart = "${postgresql}/bin/postgres"; # Wait for PostgreSQL to be ready to accept connections. ExecStartPost = let setupScript = pkgs.writeScript "postgresql-setup" '' #!${pkgs.runtimeShell} -e PSQL="${pkgs.utillinux}/bin/runuser -u ${cfg.superUser} -- psql --port=${toString cfg.port}" while ! $PSQL -d postgres -c "" 2> /dev/null; do if ! kill -0 "$MAINPID"; then exit 1; fi sleep 0.1 done if test -e "${cfg.dataDir}/.first_startup"; then ${optionalString (cfg.initialScript != null) '' $PSQL -f "${cfg.initialScript}" -d postgres ''} rm -f "${cfg.dataDir}/.first_startup" fi '' + optionalString (cfg.ensureDatabases != []) '' ${concatMapStrings (database: '' $PSQL -tAc "SELECT 1 FROM pg_database WHERE datname = '${database}'" | grep -q 1 || $PSQL -tAc 'CREATE DATABASE "${database}"' '') cfg.ensureDatabases} '' + '' ${concatMapStrings (user: '' $PSQL -tAc "SELECT 1 FROM pg_roles WHERE rolname='${user.name}'" | grep -q 1 || $PSQL -tAc 'CREATE USER "${user.name}"' ${concatStringsSep "n" (mapAttrsToList (database: permission: '' $PSQL -tAc 'GRANT ${permission} ON ${database} TO "${user.name}"' '') user.ensurePermissions)} '') cfg.ensureUsers} ''; in "+${setupScript}"; } (mkIf (cfg.dataDir == "/var/lib/postgresql/${cfg.package.psqlSchema}") { StateDirectory = "postgresql postgresql/${cfg.package.psqlSchema}"; StateDirectoryMode = if groupAccessAvailable then "0750" else "0700"; }) ]; </ins> unitConfig.RequiresMountsFor = "${cfg.dataDir}"; };

Discussion:
Describe the bug A clear and concise description of what the bug is. I have The error I got when starting the service is:
The folder is owned by user postgres:
what are the permissions on ?
it's
it works if I set it to but is created by the build/installation. why doesn't it set the right permission?
Looks line should be a one time required step and you're good to go :+1: Looks like with both and together pushes permissions up to all new parent directories. The module is still using (which is deprecated) so the module should be updated to remove that and fix your issue at the same time. Thanks for reporting!
Thank you for your help!