Change:
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!

Discussion:
</programlisting> </para> </listitem> <ins> <listitem> <para> The <xref linkend="opt-services.postgresql.dataDir"/> option is now set to <literal>"/var/lib/postgresql/${cfg.package.psqlSchema}"</literal> regardless of your <xref linkend="opt-system.stateVersion"/>. Users with an existing postgresql install that have a <xref linkend="opt-system.stateVersion"/> of <literal>17.09</literal> or below should double check what the value of their <xref linkend="opt-services.postgresql.dataDir"/> option is (<literal>/var/db/postgresql</literal>) and then explicitly set this value to maintain compatibility: <programlisting> services.postgresql.dataDir = "/var/db/postgresql"; </programlisting> </para> </listitem> </ins> </itemizedlist> </section>

listen_addresses = '${if cfg.enableTCPIP then "*" else "localhost"}' port = ${toString cfg.port} ${cfg.extraConfig} <del> ''; </del> <ins> ''; </ins> groupAccessAvailable = versionAtLeast postgresql.version "11.0";

dataDir = mkOption { type = types.path; <ins> defaultText = "/var/lib/postgresql/${config.services.postgresql.package.psqlSchema}"; </ins> example = "/var/lib/postgresql/11"; description = '' <del> Data directory for PostgreSQL. </del> <ins> The data directory for PostgreSQL. If left as the default value this directory will automatically be created before the PostgreSQL server starts, otherwise the sysadmin is responsible for ensuring the directory exists with appropriate ownership and permissions. </ins> ''; };

else if versionAtLeast config.system.stateVersion "16.03" then pkgs.postgresql_9_5 else throw "postgresql_9_4 was removed, please upgrade your postgresql version."); <del> services.postgresql.dataDir = mkDefault (if versionAtLeast config.system.stateVersion "17.09" then "/var/lib/postgresql/${cfg.package.psqlSchema}" else "/var/db/postgresql"); </del> <ins> services.postgresql.dataDir = mkDefault "/var/lib/postgresql/${cfg.package.psqlSchema}"; </ins> services.postgresql.authentication = mkAfter ''

preStart = '' <del> # Create data directory. </del> if ! test -e ${cfg.dataDir}/PG_VERSION; then <del> mkdir -m 0700 -p ${cfg.dataDir} </del> <ins> # Cleanup the data directory. </ins> rm -f ${cfg.dataDir}/*.conf <del> chown -R postgres:postgres ${cfg.dataDir} fi ''; # */ script = '' # Initialise the database. if ! test -e ${cfg.dataDir}/PG_VERSION; then </del> <ins> # Initialise the database. </ins> initdb -U ${cfg.superUser} ${concatStringsSep " " cfg.initdbArgs} # See postStart! touch "${cfg.dataDir}/.first_startup" fi ln -sfn "${configFile}" "${cfg.dataDir}/postgresql.conf" ${optionalString (cfg.recoveryConfig != null) '' ln -sfn "${pkgs.writeText "recovery.conf" cfg.recoveryConfig}"  "${cfg.dataDir}/recovery.conf" ''} <del> ${optionalString (!groupAccessAvailable) '' # postgresql pre 11.0 doesn't start if state directory mode is group accessible chmod 0700 "${cfg.dataDir}" ''} exec postgres </del> ''; <del> serviceConfig = </del> <ins> serviceConfig = mkMerge [ </ins> { ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID"; User = "postgres"; Group = "postgres"; <del> PermissionsStartOnly = true; </del> RuntimeDirectory = "postgresql"; Type = if versionAtLeast cfg.package.version "9.6" then "notify"

# 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}"; };

meta.maintainers = with maintainers; [ pacien ]; machine = { ... }: { <ins> # Needed because this test uses a non-default 'services.postgresql.dataDir'. systemd.tmpfiles.rules = [ "d /var/db/postgresql 0700 postgres postgres" ]; </ins> services.postgresql = { package = postgresqlPackage; enable = true;