2014-08-27 10:24:10 +00:00
<chapter xmlns= "http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude"
version="5.0"
xml:id="module-postgresql">
2018-09-30 00:51:11 +00:00
<title > PostgreSQL</title>
2014-08-27 10:24:10 +00:00
<!-- FIXME: render nicely -->
<!-- FIXME: source can be added automatically -->
2018-09-30 00:51:11 +00:00
<para >
2019-09-19 17:17:30 +00:00
<emphasis > Source:</emphasis>
<filename > modules/services/databases/postgresql.nix</filename>
2018-09-30 00:51:11 +00:00
</para>
<para >
2019-09-19 17:17:30 +00:00
<emphasis > Upstream documentation:</emphasis>
<link xlink:href= "http://www.postgresql.org/docs/" />
2018-09-30 00:51:11 +00:00
</para>
2014-08-27 10:24:10 +00:00
<!-- FIXME: more stuff, like maintainer? -->
2018-09-30 00:51:11 +00:00
<para >
PostgreSQL is an advanced, free relational database.
<!-- MORE -->
</para>
<section xml:id= "module-services-postgres-configuring" >
<title > Configuring</title>
<para >
2019-09-19 17:17:30 +00:00
To enable PostgreSQL, add the following to your
<filename > configuration.nix</filename> :
2014-08-27 10:24:10 +00:00
<programlisting >
2018-04-05 08:43:56 +00:00
<xref linkend= "opt-services.postgresql.enable" /> = true;
2019-07-21 19:05:41 +00:00
<xref linkend= "opt-services.postgresql.package" /> = pkgs.postgresql_11;
2014-08-27 10:24:10 +00:00
</programlisting>
2019-09-19 17:17:30 +00:00
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.
2018-09-30 00:51:11 +00:00
</para>
2014-08-27 10:24:10 +00:00
<!--
<para > After running <command > nixos-rebuild</command> , you can verify
whether PostgreSQL works by running <command > psql</command> :
<screen >
2019-06-17 11:25:50 +00:00
<prompt > $ </prompt> psql
2014-08-27 10:24:10 +00:00
psql (9.2.9)
Type "help" for help.
2019-06-17 11:25:50 +00:00
<prompt > alice=></prompt>
2014-08-27 10:24:10 +00:00
</screen>
-->
2018-09-30 00:51:11 +00:00
<para >
2019-09-19 17:17:30 +00:00
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.
2014-08-27 10:24:10 +00:00
<programlisting >
2018-04-05 08:43:56 +00:00
<xref linkend= "opt-services.postgresql.dataDir" /> = "/data/postgresql";
2014-08-27 10:24:10 +00:00
</programlisting>
2018-09-30 00:51:11 +00:00
</para>
</section>
<section xml:id= "module-services-postgres-upgrading" >
<title > Upgrading</title>
<para >
2020-03-16 17:16:25 +00:00
Major PostgreSQL upgrade requires PostgreSQL downtime and a few imperative steps to be called. To simplify this process, use the following NixOS module:
<programlisting >
containers.temp-pg.config.services.postgresql = {
enable = true;
package = pkgs.postgresql_12;
## set a custom new dataDir
# dataDir = "/some/data/dir";
};
environment.systemPackages =
let newpg = config.containers.temp-pg.config.services.postgresql;
in [
(pkgs.writeScriptBin "upgrade-pg-cluster" ''
set -x
export OLDDATA="${config.services.postgresql.dataDir}"
export NEWDATA="${newpg.dataDir}"
export OLDBIN="${config.services.postgresql.package}/bin"
export NEWBIN="${newpg.package}/bin"
install -d -m 0700 -o postgres -g postgres "$NEWDATA"
cd "$NEWDATA"
sudo -u postgres $NEWBIN/initdb -D "$NEWDATA"
systemctl stop postgresql # old one
sudo -u postgres $NEWBIN/pg_upgrade \
--old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \
--old-bindir $OLDBIN --new-bindir $NEWBIN \
"$@"
'')
];
</programlisting>
</para>
<para >
The upgrade process is:</para>
<orderedlist >
<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 new one and migrate old one to 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" /> for details.</para> </listitem>
<listitem > <para > Change postgresql package in NixOS configuration to the one you were upgrading to, and change <literal > dataDir</literal> to the one you have migrated to. Rebuild NixOS. This should start new postgres using upgraded data directory.</para> </listitem>
<listitem > <para > After upgrade you may want to <literal > ANALYZE</literal> new db.</para> </listitem>
</orderedlist>
2018-09-30 00:51:11 +00:00
</section>
<section xml:id= "module-services-postgres-options" >
<title > Options</title>
<para >
2019-09-19 17:17:30 +00:00
A complete list of options for the PostgreSQL module may be found
<link linkend= "opt-services.postgresql.enable" > here</link> .
2018-09-30 00:51:11 +00:00
</para>
</section>
2019-07-21 19:05:41 +00:00
<section xml:id= "module-services-postgres-plugins" >
<title > Plugins</title>
<para >
2019-09-19 17:17:30 +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> :
2019-07-21 19:05:41 +00:00
<screen >
<prompt > $ </prompt> nix repl '< nixpkgs> '
Loading '< nixpkgs> '...
Added 10574 variables.
<prompt > nix-repl> </prompt> postgresql_11.pkgs.< TAB> < TAB>
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
...
</screen>
</para>
<para >
2019-09-19 17:17:30 +00:00
To add plugins via NixOS configuration, set <literal > services.postgresql.extraPlugins</literal> :
2019-07-21 19:05:41 +00:00
<programlisting >
<xref linkend= "opt-services.postgresql.package" /> = pkgs.postgresql_11;
<xref linkend= "opt-services.postgresql.extraPlugins" /> = with pkgs.postgresql_11.pkgs; [
pg_repack
postgis
];
</programlisting>
</para>
<para >
2019-09-19 17:17:30 +00:00
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:
2019-07-21 19:05:41 +00:00
<programlisting >
self: super: {
postgresql_custom = self.postgresql_11.withPackages (ps: [
ps.pg_repack
ps.postgis
]);
}
</programlisting>
</para>
<para >
2019-09-19 17:17:30 +00:00
Here's a recipe on how to override a particular plugin through an overlay:
2019-07-21 19:05:41 +00:00
<programlisting >
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 (_: {
name = "pg_repack-v20181024";
src = self.fetchzip {
url = "https://github.com/reorg/pg_repack/archive/923fa2f3c709a506e111cc963034bf2fd127aa00.tar.gz";
sha256 = "17k6hq9xaax87yz79j773qyigm4fwk8z4zh5cyp6z0sxnwfqxxw5";
};
});
};
};
}
</programlisting>
</para>
</section>
2014-08-27 10:24:10 +00:00
</chapter>