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 </programlisting> 79 </para> 80 </listitem> 81 </itemizedlist> 82 83
··· 78 </programlisting> 79 </para> 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> 92 </itemizedlist> 93 94
+15 -5
nixos/modules/services/databases/postgresql.nix
··· 38 39 pre84 = versionOlder (builtins.parseDrvName postgresql.name).version "8.4"; 40 41 in 42 43 { ··· 74 75 dataDir = mkOption { 76 type = types.path; 77 - default = "/var/db/postgresql"; 78 description = '' 79 Data directory for PostgreSQL. 80 ''; ··· 160 # Note: when changing the default, make it conditional on 161 # ‘system.stateVersion’ to maintain compatibility with existing 162 # systems! 163 - mkDefault (if versionAtLeast config.system.stateVersion "16.03" then pkgs.postgresql95 else pkgs.postgresql94); 164 165 services.postgresql.authentication = mkAfter 166 '' ··· 205 '' 206 # Initialise the database. 207 if ! test -e ${cfg.dataDir}/PG_VERSION; then 208 - initdb -U root 209 # See postStart! 210 touch "${cfg.dataDir}/.first_startup" 211 fi ··· 237 # Wait for PostgreSQL to be ready to accept connections. 238 postStart = 239 '' 240 - while ! psql --port=${toString cfg.port} postgres -c "" 2> /dev/null; do 241 if ! kill -0 "$MAINPID"; then exit 1; fi 242 sleep 0.1 243 done 244 245 if test -e "${cfg.dataDir}/.first_startup"; then 246 ${optionalString (cfg.initialScript != null) '' 247 - psql -f "${cfg.initialScript}" --port=${toString cfg.port} postgres 248 ''} 249 rm -f "${cfg.dataDir}/.first_startup" 250 fi
··· 38 39 pre84 = versionOlder (builtins.parseDrvName postgresql.name).version "8.4"; 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 + 45 in 46 47 { ··· 78 79 dataDir = mkOption { 80 type = types.path; 81 + example = "/var/lib/postgresql/9.6"; 82 description = '' 83 Data directory for PostgreSQL. 84 ''; ··· 164 # Note: when changing the default, make it conditional on 165 # ‘system.stateVersion’ to maintain compatibility with existing 166 # systems! 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"); 174 175 services.postgresql.authentication = mkAfter 176 '' ··· 215 '' 216 # Initialise the database. 217 if ! test -e ${cfg.dataDir}/PG_VERSION; then 218 + initdb -U ${superuser} 219 # See postStart! 220 touch "${cfg.dataDir}/.first_startup" 221 fi ··· 247 # Wait for PostgreSQL to be ready to accept connections. 248 postStart = 249 '' 250 + while ! ${pkgs.sudo}/bin/sudo -u ${superuser} psql --port=${toString cfg.port} -d postgres -c "" 2> /dev/null; do 251 if ! kill -0 "$MAINPID"; then exit 1; fi 252 sleep 0.1 253 done 254 255 if test -e "${cfg.dataDir}/.first_startup"; then 256 ${optionalString (cfg.initialScript != null) '' 257 + ${pkgs.sudo}/bin/sudo -u ${superuser} psql -f "${cfg.initialScript}" --port=${toString cfg.port} -d postgres 258 ''} 259 rm -f "${cfg.dataDir}/.first_startup" 260 fi