Cleanup PostgreSQL for state version 17.09 (#25753)

* postgresql service: make 9.6 the default version for 17.09

* postgresql service: change default superuser for 17.09

Change the default superuser from `root` to `postgres` for state
version 17.09

* postgresql service: change default data directory for 17.09

The new directory includes the schema version of the database.
This makes upgrades easier and is more consistent with other distros.

* updated nixos release notes

authored by

Pascal Bach and committed by zimbatm.tngl.sh de52d245 3fa1be6f

+26 -5
+11
nixos/doc/manual/release-notes/rl-1709.xml
··· 78 78 </programlisting> 79 79 </para> 80 80 </listitem> 81 + <listitem> 82 + <para> 83 + The <literal>postgres</literal> default version was changed from 9.5 to 9.6. 84 + </para> 85 + <para> 86 + The <literal>postgres</literal> superuser name has changed from <literal>root</literal> to <literal>postgres</literal> to more closely follow what other Linux distributions are doing. 87 + </para> 88 + <para> 89 + The <literal>postgres</literal> default <literal>dataDir</literal> has changed from <literal>/var/db/postgres</literal> to <literal>/var/lib/postgresql/$psqlSchema</literal> where $psqlSchema is 9.6 for example. 90 + </para> 91 + </listitem> 81 92 </itemizedlist> 82 93 83 94
+15 -5
nixos/modules/services/databases/postgresql.nix
··· 38 38 39 39 pre84 = versionOlder (builtins.parseDrvName postgresql.name).version "8.4"; 40 40 41 + # NixOS traditionally used `root` as superuser, most other distros use `postgres`. From 17.09 42 + # we also try to follow this standard 43 + superuser = (if versionAtLeast config.system.stateVersion "17.09" then "postgres" else "root"); 44 + 41 45 in 42 46 43 47 { ··· 74 78 75 79 dataDir = mkOption { 76 80 type = types.path; 77 - default = "/var/db/postgresql"; 81 + example = "/var/lib/postgresql/9.6"; 78 82 description = '' 79 83 Data directory for PostgreSQL. 80 84 ''; ··· 160 164 # Note: when changing the default, make it conditional on 161 165 # ‘system.stateVersion’ to maintain compatibility with existing 162 166 # systems! 163 - mkDefault (if versionAtLeast config.system.stateVersion "16.03" then pkgs.postgresql95 else pkgs.postgresql94); 167 + mkDefault (if versionAtLeast config.system.stateVersion "17.09" then pkgs.postgresql96 168 + else if versionAtLeast config.system.stateVersion "16.03" then pkgs.postgresql95 169 + else pkgs.postgresql94); 170 + 171 + services.postgresql.dataDir = 172 + mkDefault (if versionAtLeast config.system.stateVersion "17.09" then "/var/lib/postgresql/${config.services.postgresql.package.psqlSchema}" 173 + else "/var/db/postgresql"); 164 174 165 175 services.postgresql.authentication = mkAfter 166 176 '' ··· 205 215 '' 206 216 # Initialise the database. 207 217 if ! test -e ${cfg.dataDir}/PG_VERSION; then 208 - initdb -U root 218 + initdb -U ${superuser} 209 219 # See postStart! 210 220 touch "${cfg.dataDir}/.first_startup" 211 221 fi ··· 237 247 # Wait for PostgreSQL to be ready to accept connections. 238 248 postStart = 239 249 '' 240 - while ! psql --port=${toString cfg.port} postgres -c "" 2> /dev/null; do 250 + while ! ${pkgs.sudo}/bin/sudo -u ${superuser} psql --port=${toString cfg.port} -d postgres -c "" 2> /dev/null; do 241 251 if ! kill -0 "$MAINPID"; then exit 1; fi 242 252 sleep 0.1 243 253 done 244 254 245 255 if test -e "${cfg.dataDir}/.first_startup"; then 246 256 ${optionalString (cfg.initialScript != null) '' 247 - psql -f "${cfg.initialScript}" --port=${toString cfg.port} postgres 257 + ${pkgs.sudo}/bin/sudo -u ${superuser} psql -f "${cfg.initialScript}" --port=${toString cfg.port} -d postgres 248 258 ''} 249 259 rm -f "${cfg.dataDir}/.first_startup" 250 260 fi