Note that you are required to specify the desired version of PostgreSQL (e.g. <literal>pkgs.postgresql_11</literal>). Since upgrading your PostgreSQL version requires a database dump and reload (see below), NixOS cannot provide a default value for <xreflinkend="opt-services.postgresql.package"/> such as the most recent release of PostgreSQL.
</para>
<!--
<para>After running <command>nixos-rebuild</command>, you can verify
whether PostgreSQL works by running <command>psql</command>:
<screen>
<prompt>$ </prompt>psql
psql (9.2.9)
Type "help" for help.
<prompt>alice=></prompt>
</screen>
-->
<para>
By default, PostgreSQL stores its databases in <filename>/var/lib/postgresql/$psqlSchema</filename>. You can override this using <xreflinkend="opt-services.postgresql.dataDir"/>, e.g.
Rebuild nixos configuration with the configuration above added to your <filename>configuration.nix</filename>. Alternatively, add that into separate file and reference it in <literal>imports</literal> list.
Run <literal>upgrade-pg-cluster</literal>. It will stop old postgresql, initialize a new one and migrate the old one to the new one. You may supply arguments like <literal>--jobs 4</literal> and <literal>--link</literal> to speedup migration process. See <linkxlink:href="https://www.postgresql.org/docs/current/pgupgrade.html"/> for details.
Change postgresql package in NixOS configuration to the one you were upgrading to via <xreflinkend="opt-services.postgresql.package"/>. Rebuild NixOS. This should start new postgres using upgraded data directory and all services you stopped during the upgrade.
After the upgrade it's advisable to analyze the new cluster.
</para>
<itemizedlist>
<listitem>
<para>
For PostgreSQL ≥ 14, use the <literal>vacuumdb</literal> command printed by the upgrades script.
</para>
</listitem>
<listitem>
<para>
For PostgreSQL < 14, run (as <literal>su -l postgres</literal> in the <xreflinkend="opt-services.postgresql.dataDir"/>, in this example <filename>/var/lib/postgresql/13</filename>):
Plugins collection for each PostgreSQL version can be accessed with <literal>.pkgs</literal>. For example, for <literal>pkgs.postgresql_11</literal> package, its plugin collection is accessed by <literal>pkgs.postgresql_11.pkgs</literal>:
<xreflinkend="opt-services.postgresql.extraPlugins"/> = with pkgs.postgresql_11.pkgs; [
pg_repack
postgis
];
</programlisting>
</para>
<para>
You can build custom PostgreSQL-with-plugins (to be used outside of NixOS) using function <literal>.withPackages</literal>. For example, creating a custom PostgreSQL package in an overlay can look like: