diff --git a/machines/gerd.nix b/machines/gerd.nix index e12911a..5dc34e0 100644 --- a/machines/gerd.nix +++ b/machines/gerd.nix @@ -4,6 +4,8 @@ ./../shared/applications/server/acme.nix ./../shared/applications/server/nginx.nix + ./../shared/applications/server/postgresql.nix + ./../shared/applications/state/postgresql.nix ./../shared/applications/state/ssh.nix ./gerd/services/fricloud-website.nix @@ -31,6 +33,8 @@ "safe/svcs/hedgedoc" = { mountpoint = "/srv/hedgedoc"; extra.options.quota = "5G"; }; "safe/svcs/nextcloud" = { mountpoint = "/srv/nextcloud"; extra.options.quota = "5G"; }; "safe/svcs/stalwart" = { mountpoint = "/srv/stalwart"; extra.options.quota = "5G"; }; + "safe/svcs/postgresql" = { mountpoint = "/srv/postgresql"; extra.options.quota = "5G"; }; + "backup/postgresql" = { mountpoint = "/media/backup/postgresqlbackup"; extra.options.quota = "5G"; }; }; }; diff --git a/shared/applications/server/postgresql.nix b/shared/applications/server/postgresql.nix new file mode 100644 index 0000000..39ea88b --- /dev/null +++ b/shared/applications/server/postgresql.nix @@ -0,0 +1,31 @@ +{ config, ... }: + +{ + services.postgresql = { + enable = true; + }; + + # backup postgresql databases (everything in ensuredatabases) + services.postgresqlBackup = { + enable = true; + + compression = "zstd"; + + # default to backup all databadatabases + databases = config.services.postgresql.ensureDatabases; + }; + + # default the locale to C. I have NO CLUE why you would + # like to use any other locale, than the default C. + # However, matrix synapse complains A LOT if it isn't C, + # so we just default to it! No worries! + # Matrix Synapse Locale Note + # - https://github.com/element-hq/synapse/blob/develop/docs/postgres.md#fixing-incorrect-collate-or-ctype + # NOTE from postgresql here https://www.postgresql.org/docs/current/locale.html + # Using C.UTF-8, because setting `LC_CTYPE=C` will default encoding to SQL_ASCII. + # https://pganalyze.com/blog/5mins-postgres-17-builtin-c-utf8-locale + systemd.services.postgresql.environment = { + LC_CTYPE = "C.UTF-8"; + LC_COLLATE = "C"; + }; +} diff --git a/shared/applications/state/postgresql.nix b/shared/applications/state/postgresql.nix new file mode 100644 index 0000000..3c1e371 --- /dev/null +++ b/shared/applications/state/postgresql.nix @@ -0,0 +1,20 @@ +{ config, ... }: + +let + stateDir= config.mine.zfsMounts."rpool/safe/svcs/postgresql"; + backupDir = config.mine.zfsMounts."rpool/backup/postgresql"; + + postgresql_user = config.systemd.services.postgresql.serviceConfig.User; + postgresql_group = config.systemd.services.postgresql.serviceConfig.Group; +in { + services.postgresql.dataDir = stateDir; + + # backup postgresql databases (everything in ensuredatabases) + services.postgresqlBackup.location = backupDir; + + # ensure correct permissions for postgresql and postgresql backup + systemd.tmpfiles.rules = [ + "Z ${stateDir} 0770 ${postgresql_user} ${postgresql_group} -" + "Z ${backupDir} 0770 ${postgresql_user} ${postgresql_group} -" + ]; +}