aboutsummaryrefslogtreecommitdiff
path: root/hosts/sagittarius/web/mediadb.nix
blob: 90c95d6fb5d5d1862b195faa43ab91cf71e373c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{
  pkgs,
  lib,
  config,
  ...
}:
let
  newPostgres = pkgs.postgresql_18_jit;
  cfg = config.services.postgresql;
  upgrade-pg-cluster = pkgs.writeScriptBin "upgrade-pg-cluster" ''
    set -eux
    systemctl stop postgresql

    export NEWDATA="/var/lib/postgresql/${newPostgres.psqlSchema}"
    export NEWBIN="${newPostgres}/bin"

    export OLDDATA="${cfg.dataDir}"
    export OLDBIN="${cfg.finalPackage}/bin"

    install -d -m 0700 -o postgres -g postgres "$NEWDATA"
    cd "$NEWDATA"
    sudo -u postgres "$NEWBIN/initdb" -D "$NEWDATA" ${lib.escapeShellArgs cfg.initdbArgs}

    sudo -u postgres "$NEWBIN/pg_upgrade" \
      --old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \
      --old-bindir "$OLDBIN" --new-bindir "$NEWBIN" \
      "$@"
  '';
in
{
  networking.firewall.allowedTCPPorts = [ 5432 ];

  services.postgresql = {
    enable = true;
    package = pkgs.postgresql_15_jit;
    enableTCPIP = true;
    enableJIT = true;

    settings = {
      password_encryption = "scram-sha-256";
    };

    authentication = ''
      # TYPE  DATABASE  USER        ADDRESS  METHOD
      local   all       all                  peer
      host    all       postgres    all      reject
      host    all       all         all      scram-sha-256
    '';
  }; # end of services.postgreslq

  environment.systemPackages = [ upgrade-pg-cluster ];
}