Replace dendrite with synapse
diff --git a/modules/postgres.nix b/modules/postgres.nix
index 63e7697..742e3d4 100644
--- a/modules/postgres.nix
+++ b/modules/postgres.nix
@@ -5,7 +5,6 @@
     package = pkgs.postgresql;
     settings = {
       log_connections = true;
-      log_statement = "all";
       logging_collector = true;
       log_disconnections = true;
       log_destination = lib.mkForce "syslog";
@@ -20,17 +19,9 @@
         };
       }
       {
-        name = "dendrite";
+        name = "synapse";
         ensurePermissions = {
-          "DATABASE dendrite_account_database" = "ALL PRIVILEGES";
-          "DATABASE dendrite_device_database" = "ALL PRIVILEGES";
-          "DATABASE dendrite_sync_api" = "ALL PRIVILEGES";
-          "DATABASE dendrite_room_server" = "ALL PRIVILEGES";
-          "DATABASE dendrite_mscs" = "ALL PRIVILEGES";
-          "DATABASE dendrite_media_api" = "ALL PRIVILEGES";
-          "DATABASE dendrite_key_server" = "ALL PRIVILEGES";
-          "DATABASE dendrite_federation_api" = "ALL PRIVILEGES";
-          "DATABASE dendrite_app_service_api" = "ALL PRIVILEGES";
+          "DATABASE synapse" = "ALL PRIVILEGES";
         };
       }
     ] ++ (map
@@ -41,33 +32,39 @@
         }
       )) [ "minion" "coded" "pinea" ]);
 
-    ensureDatabases = [
-      "dendrite_account_database"
-      "dendrite_device_database"
-      "dendrite_sync_api"
-      "dendrite_sync_api"
-      "dendrite_room_server"
-      "dendrite_mscs"
-      "dendrite_media_api"
-      "dendrite_key_server"
-      "dendrite_federation_api"
-      "dendrite_app_service_api"
-    ];
   };
 
-  systemd.services.postgresql.postStart = lib.mkAfter (lib.pipe [
-    { user = "clicks_grafana"; passwordFile = config.sops.secrets.clicks_grafana_db_password.path; }
-    { user = "dendrite"; passwordFile = config.sops.secrets.dendrite_db_password.path; }
-  ] [
-    (map (userData: ''
-      $PSQL -tAc "ALTER USER ${userData.user} PASSWORD '$(cat ${userData.passwordFile})';"
-    ''))
-    (lib.concatStringsSep "\n")
-  ]);
+  systemd.services.postgresql.postStart = lib.mkMerge [
+    (
+      let
+        database = "synapse";
+        cfg = config.services.postgresql;
+      in
+      lib.mkBefore (
+        ''
+          PSQL="psql --port=${toString cfg.port}"
+
+          while ! $PSQL -d postgres -c "" 2> /dev/null; do
+              if ! kill -0 "$MAINPID"; then exit 1; fi
+              sleep 0.1
+          done
+
+          $PSQL -tAc "SELECT 1 FROM pg_database WHERE datname = '${database}'" | grep -q 1 || $PSQL -tAc 'CREATE DATABASE "${database}" WITH LC_CTYPE="C" LC_COLLATE="C" TEMPLATE="template0"'
+        ''
+      ) # synapse needs C collation, so we can't use ensureDatabases for it
+    )
+    (lib.mkAfter (lib.pipe [
+      { user = "clicks_grafana"; passwordFile = config.sops.secrets.clicks_grafana_db_password.path; }
+    ] [
+      (map (userData: ''
+        $PSQL -tAc "ALTER USER ${userData.user} PASSWORD '$(cat ${userData.passwordFile})';"
+      ''))
+      (lib.concatStringsSep "\n")
+    ]))
+  ];
 
   sops.secrets = lib.pipe [
     "clicks_grafana_db_password"
-    "dendrite_db_password"
   ] [
     (map (name: {
       inherit name;