anchor.diff:          # `plausible' requires the `citext'-extension.
           plausible-postgres = {
             after = [ "postgresql.service" ];
  -          bindsTo = [ "postgresql.service" ];
  -          requiredBy = [ "plausible.service" ];
             partOf = [ "plausible.service" ];
  -          serviceConfig.Type = "oneshot";
  -          unitConfig.ConditionPathExists = "!/var/lib/plausible/.db-setup";
  -          script = ''
  -            mkdir -p /var/lib/plausible/
  +          serviceConfig = {
  +            Type = "oneshot";
  +            User = config.services.postgresql.superUser;
  +            RemainAfterExit = true;
  +          };
  +          script = with cfg.database.postgres; ''
               PSQL() {
  -              /run/wrappers/bin/sudo -Hu postgres ${config.services.postgresql.package}/bin/psql --port=5432 "$@"
  +              ${config.services.postgresql.package}/bin/psql --port=5432 "$@"
               }
  -            PSQL -tAc "CREATE ROLE plausible WITH LOGIN;"
  -            PSQL -tAc "CREATE DATABASE plausible WITH OWNER plausible;"
  -            PSQL -d plausible -tAc "CREATE EXTENSION IF NOT EXISTS citext;"
  -            touch /var/lib/plausible/.db-setup
  +            # check if the database already exists
  +            if ! PSQL -lqt | ${pkgs.coreutils}/bin/cut -d \| -f 1 | ${pkgs.gnugrep}/bin/grep -qw ${dbname} ; then
  +              PSQL -tAc "CREATE ROLE plausible WITH LOGIN;"
  +              PSQL -tAc "CREATE DATABASE ${dbname} WITH OWNER plausible;"
  +              PSQL -d ${dbname} -tAc "CREATE EXTENSION IF NOT EXISTS citext;"
  +            fi
             '';
           };
         })
  
positive.code: 
            path = [ pkgs.plausible ]
              ++ optional cfg.database.postgres.setup config.services.postgresql.package;
  
            serviceConfig = {
              DynamicUser = true;
              PrivateTmp = true;
              WorkingDirectory = "/var/lib/plausible";
              StateDirectory = "plausible";
              ExecStartPre = "@${pkgs.writeShellScript "plausible-setup" ''
                set -eu -o pipefail
                ${envSecrets}
                ${pkgs.plausible}/createdb.sh
                ${pkgs.plausible}/migrate.sh
                ${optionalString cfg.adminUser.activate ''
                  if ! ${pkgs.plausible}/init-admin.sh | grep 'already exists'; then
                    psql -d plausible <<< "UPDATE users SET email_verified=true;"
                  fi
                ''}
              ''} plausible-setup";
              ExecStart = "@${pkgs.writeShellScript "plausible" ''
                set -eu -o pipefail
                ${envSecrets}
                plausible start
              ''} plausible";
            };
          };
        }
  
negative.code:       enable = true;
      };
  
      services.epmd.enable = true;
  
      systemd.services = mkMerge [
        {
          plausible = {
            inherit (pkgs.plausible.meta) description;
            documentation = [ "https://plausible.io/docs/self-hosting" ];
            wantedBy = [ "multi-user.target" ];
            after = optionals cfg.database.postgres.setup [ "postgresql.service" "plausible-postgres.service" ];
            requires = optional cfg.database.clickhouse.setup "clickhouse.service"
              ++ optionals cfg.database.postgres.setup [
                "postgresql.service"
  