2023-01-20 10:41:00 +00:00
|
|
|
|
<!-- Do not edit this file directly, edit its companion .md instead
|
|
|
|
|
and regenerate this file using nixos/doc/manual/md-to-db.sh -->
|
|
|
|
|
<chapter xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="module-postgresql">
|
|
|
|
|
<title>PostgreSQL</title>
|
2020-04-24 23:36:52 +00:00
|
|
|
|
<para>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
<emphasis>Source:</emphasis>
|
|
|
|
|
<filename>modules/services/databases/postgresql.nix</filename>
|
2020-04-24 23:36:52 +00:00
|
|
|
|
</para>
|
|
|
|
|
<para>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
<emphasis>Upstream documentation:</emphasis>
|
|
|
|
|
<link xlink:href="http://www.postgresql.org/docs/">http://www.postgresql.org/docs/</link>
|
2020-04-24 23:36:52 +00:00
|
|
|
|
</para>
|
|
|
|
|
<para>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
PostgreSQL is an advanced, free relational database.
|
|
|
|
|
</para>
|
|
|
|
|
<section xml:id="module-services-postgres-configuring">
|
|
|
|
|
<title>Configuring</title>
|
|
|
|
|
<para>
|
|
|
|
|
To enable PostgreSQL, add the following to your
|
|
|
|
|
<filename>configuration.nix</filename>:
|
|
|
|
|
</para>
|
|
|
|
|
<programlisting>
|
|
|
|
|
services.postgresql.enable = true;
|
|
|
|
|
services.postgresql.package = pkgs.postgresql_11;
|
|
|
|
|
</programlisting>
|
|
|
|
|
<para>
|
|
|
|
|
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
|
|
|
|
|
<xref linkend="opt-services.postgresql.package" /> such as the
|
|
|
|
|
most recent release of PostgreSQL.
|
|
|
|
|
</para>
|
|
|
|
|
<para>
|
|
|
|
|
By default, PostgreSQL stores its databases in
|
|
|
|
|
<filename>/var/lib/postgresql/$psqlSchema</filename>. You can
|
|
|
|
|
override this using
|
|
|
|
|
<xref linkend="opt-services.postgresql.dataDir" />, e.g.
|
|
|
|
|
</para>
|
|
|
|
|
<programlisting>
|
|
|
|
|
services.postgresql.dataDir = "/data/postgresql";
|
2021-12-19 01:06:50 +00:00
|
|
|
|
</programlisting>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
</section>
|
|
|
|
|
<section xml:id="module-services-postgres-upgrading">
|
|
|
|
|
<title>Upgrading</title>
|
|
|
|
|
<note>
|
|
|
|
|
<para>
|
|
|
|
|
The steps below demonstrate how to upgrade from an older version
|
|
|
|
|
to <literal>pkgs.postgresql_13</literal>. These instructions are
|
|
|
|
|
also applicable to other versions.
|
|
|
|
|
</para>
|
|
|
|
|
</note>
|
|
|
|
|
<para>
|
|
|
|
|
Major PostgreSQL upgrades require a downtime and a few imperative
|
|
|
|
|
steps to be called. This is the case because each major version
|
|
|
|
|
has some internal changes in the databases’ state during major
|
|
|
|
|
releases. Because of that, NixOS places the state into
|
|
|
|
|
<filename>/var/lib/postgresql/<version></filename> where
|
|
|
|
|
each <literal>version</literal> can be obtained like this:
|
|
|
|
|
</para>
|
|
|
|
|
<programlisting>
|
|
|
|
|
$ nix-instantiate --eval -A postgresql_13.psqlSchema
|
|
|
|
|
"13"
|
|
|
|
|
</programlisting>
|
|
|
|
|
<para>
|
|
|
|
|
For an upgrade, a script like this can be used to simplify the
|
|
|
|
|
process:
|
|
|
|
|
</para>
|
|
|
|
|
<programlisting>
|
2021-12-19 01:06:50 +00:00
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
{
|
2023-01-20 10:41:00 +00:00
|
|
|
|
environment.systemPackages = [
|
2022-10-30 15:09:59 +00:00
|
|
|
|
(let
|
|
|
|
|
# XXX specify the postgresql package you'd like to upgrade to.
|
|
|
|
|
# Do not forget to list the extensions you need.
|
|
|
|
|
newPostgres = pkgs.postgresql_13.withPackages (pp: [
|
|
|
|
|
# pp.plv8
|
|
|
|
|
]);
|
2023-01-20 10:41:00 +00:00
|
|
|
|
in pkgs.writeScriptBin "upgrade-pg-cluster" ''
|
2021-12-19 01:06:50 +00:00
|
|
|
|
set -eux
|
|
|
|
|
# XXX it's perhaps advisable to stop all services that depend on postgresql
|
|
|
|
|
systemctl stop postgresql
|
|
|
|
|
|
2023-01-20 10:41:00 +00:00
|
|
|
|
export NEWDATA="/var/lib/postgresql/${newPostgres.psqlSchema}"
|
2021-12-19 01:06:50 +00:00
|
|
|
|
|
2023-01-20 10:41:00 +00:00
|
|
|
|
export NEWBIN="${newPostgres}/bin"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2023-01-20 10:41:00 +00:00
|
|
|
|
export OLDDATA="${config.services.postgresql.dataDir}"
|
|
|
|
|
export OLDBIN="${config.services.postgresql.package}/bin"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2023-01-20 10:41:00 +00:00
|
|
|
|
install -d -m 0700 -o postgres -g postgres "$NEWDATA"
|
|
|
|
|
cd "$NEWDATA"
|
|
|
|
|
sudo -u postgres $NEWBIN/initdb -D "$NEWDATA"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
|
sudo -u postgres $NEWBIN/pg_upgrade \
|
2023-01-20 10:41:00 +00:00
|
|
|
|
--old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \
|
2021-12-19 01:06:50 +00:00
|
|
|
|
--old-bindir $OLDBIN --new-bindir $NEWBIN \
|
2023-01-20 10:41:00 +00:00
|
|
|
|
"$@"
|
2021-12-19 01:06:50 +00:00
|
|
|
|
'')
|
|
|
|
|
];
|
|
|
|
|
}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
</programlisting>
|
|
|
|
|
<para>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
The upgrade process is:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
</para>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
<orderedlist numeration="arabic">
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
Login as root (<literal>sudo su -</literal>)
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
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
|
|
|
|
|
<link xlink:href="https://www.postgresql.org/docs/current/pgupgrade.html">https://www.postgresql.org/docs/current/pgupgrade.html</link>
|
|
|
|
|
for details.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
Change postgresql package in NixOS configuration to the one
|
|
|
|
|
you were upgrading to via
|
|
|
|
|
<xref linkend="opt-services.postgresql.package" />. Rebuild
|
|
|
|
|
NixOS. This should start new postgres using upgraded data
|
|
|
|
|
directory and all services you stopped during the upgrade.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
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
|
|
|
|
|
<xref linkend="opt-services.postgresql.dataDir" />, in
|
|
|
|
|
this example <filename>/var/lib/postgresql/13</filename>):
|
|
|
|
|
</para>
|
|
|
|
|
<programlisting>
|
|
|
|
|
$ ./analyze_new_cluster.sh
|
|
|
|
|
</programlisting>
|
|
|
|
|
</listitem>
|
|
|
|
|
</itemizedlist>
|
|
|
|
|
<warning>
|
|
|
|
|
<para>
|
|
|
|
|
The next step removes the old state-directory!
|
|
|
|
|
</para>
|
|
|
|
|
</warning>
|
|
|
|
|
<programlisting>
|
|
|
|
|
$ ./delete_old_cluster.sh
|
|
|
|
|
</programlisting>
|
|
|
|
|
</listitem>
|
|
|
|
|
</orderedlist>
|
|
|
|
|
</section>
|
|
|
|
|
<section xml:id="module-services-postgres-options">
|
|
|
|
|
<title>Options</title>
|
2020-04-24 23:36:52 +00:00
|
|
|
|
<para>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
A complete list of options for the PostgreSQL module may be found
|
|
|
|
|
<link linkend="opt-services.postgresql.enable">here</link>.
|
2022-10-30 15:09:59 +00:00
|
|
|
|
</para>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
</section>
|
|
|
|
|
<section xml:id="module-services-postgres-plugins">
|
|
|
|
|
<title>Plugins</title>
|
2022-10-30 15:09:59 +00:00
|
|
|
|
<para>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
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>:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
</para>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
<programlisting>
|
|
|
|
|
$ nix repl '<nixpkgs>'
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
Loading '<nixpkgs>'...
|
|
|
|
|
Added 10574 variables.
|
|
|
|
|
|
2023-01-20 10:41:00 +00:00
|
|
|
|
nix-repl> postgresql_11.pkgs.<TAB><TAB>
|
2020-04-24 23:36:52 +00:00
|
|
|
|
postgresql_11.pkgs.cstore_fdw postgresql_11.pkgs.pg_repack
|
|
|
|
|
postgresql_11.pkgs.pg_auto_failover postgresql_11.pkgs.pg_safeupdate
|
|
|
|
|
postgresql_11.pkgs.pg_bigm postgresql_11.pkgs.pg_similarity
|
|
|
|
|
postgresql_11.pkgs.pg_cron postgresql_11.pkgs.pg_topn
|
|
|
|
|
postgresql_11.pkgs.pg_hll postgresql_11.pkgs.pgjwt
|
|
|
|
|
postgresql_11.pkgs.pg_partman postgresql_11.pkgs.pgroonga
|
|
|
|
|
...
|
2023-01-20 10:41:00 +00:00
|
|
|
|
</programlisting>
|
|
|
|
|
<para>
|
|
|
|
|
To add plugins via NixOS configuration, set
|
|
|
|
|
<literal>services.postgresql.extraPlugins</literal>:
|
|
|
|
|
</para>
|
|
|
|
|
<programlisting>
|
|
|
|
|
services.postgresql.package = pkgs.postgresql_11;
|
|
|
|
|
services.postgresql.extraPlugins = with pkgs.postgresql_11.pkgs; [
|
2020-04-24 23:36:52 +00:00
|
|
|
|
pg_repack
|
|
|
|
|
postgis
|
|
|
|
|
];
|
|
|
|
|
</programlisting>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
<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:
|
|
|
|
|
</para>
|
|
|
|
|
<programlisting>
|
2020-04-24 23:36:52 +00:00
|
|
|
|
self: super: {
|
|
|
|
|
postgresql_custom = self.postgresql_11.withPackages (ps: [
|
|
|
|
|
ps.pg_repack
|
|
|
|
|
ps.postgis
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
</programlisting>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
<para>
|
|
|
|
|
Here’s a recipe on how to override a particular plugin through an
|
|
|
|
|
overlay:
|
|
|
|
|
</para>
|
|
|
|
|
<programlisting>
|
2020-04-24 23:36:52 +00:00
|
|
|
|
self: super: {
|
|
|
|
|
postgresql_11 = super.postgresql_11.override { this = self.postgresql_11; } // {
|
|
|
|
|
pkgs = super.postgresql_11.pkgs // {
|
|
|
|
|
pg_repack = super.postgresql_11.pkgs.pg_repack.overrideAttrs (_: {
|
2023-01-20 10:41:00 +00:00
|
|
|
|
name = "pg_repack-v20181024";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
src = self.fetchzip {
|
2023-01-20 10:41:00 +00:00
|
|
|
|
url = "https://github.com/reorg/pg_repack/archive/923fa2f3c709a506e111cc963034bf2fd127aa00.tar.gz";
|
|
|
|
|
sha256 = "17k6hq9xaax87yz79j773qyigm4fwk8z4zh5cyp6z0sxnwfqxxw5";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
</programlisting>
|
2023-01-20 10:41:00 +00:00
|
|
|
|
</section>
|
2020-04-24 23:36:52 +00:00
|
|
|
|
</chapter>
|