Nextcloud does not start anymore

Hello, my nextcloud is not working anymore. Error message:

Sep 10 22:32:32 server nextcloud-setup-start[532958]: Next Doctrine\DBAL\Exception\TableNotFoundException: An exception occurred while executing a query: SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "oc_appconfig" does not exist
Sep 10 22:32:32 server nextcloud-setup-start[532958]: LINE 1: SELECT * FROM "oc_appconfig"
Sep 10 22:32:32 server nextcloud-setup-start[532958]:                       ^ in /nix/store/9qnln1n8d9xgxqlhy0nbkd2v6l0x2hbm-nextcloud-2

I suspect it has something to do with the code I recently added to my config, because before it did not happen. Here the relevant snippet out of my config:

services.postgresql = {
    enable = true;
    package = pkgs.postgresql_15;
    ensureDatabases = [ "discordbot" ];
    authentication = pkgs.lib.mkOverride 10 ''
      #type database DBuser auth-method
      local all      all    trust
    '';
  };

nextcloud = {
      enable = true;
      package = pkgs.nextcloud27;
      hostName = "cloud.shaniag.com";
      database.createLocally = true;
      https = true;
      configureRedis = true;
      config = {
        dbtype = "pgsql";
        adminpassFile = "/var/run/nextcloud-pass.txt";
        defaultPhoneRegion = "DE";
      };
      extraOptions = {
        mail_smtpmode = "sendmail";
        mail_sendmailmode = "pipe";
        twofactor_enforced = false;
      };
      autoUpdateApps.enable = true;
    };
  };

Perhaps I am missing it in your post, but which part of the code was recently added?

mostly facing this issue: postgresql_15 requires granting permissions on schema public, ensureUsers insufficient · Issue #216989 · NixOS/nixpkgs · GitHub

I understand that there is a desire for additional config options to be added to the NixOS service to support this. In the meantime to get Nexcloud started again, is it possible to manually grant the permission? Since the database it stateful, it shouldn’t cause problems with future rebuilds and it could always be documented in comments in the .nix configuration.

Or is this more complicated than I am understanding?

BTW: I am also interested in this topic because I run Nextcloud on a NixOS server. I currently run it using an oci-container from Docker Hub along with an oci-container for mariadb; but I was thinking I may move to the services.nextcloud option.

@genson i tried to ensure user permissions but all of that does not really work. I land on a page where it asks me if i want to use sqlite or postgres even though i specified that

This is happening to me after switching from 23.05 to 23.11 and replacing services.postgresql.ensureUsers.*.ensurePermissions with services.postgresql.ensureUsers.*.ensureDBOwnership = true. Disappears when reverting.

I wonder since it has been 3 months and I just have run into the same issue. Has anyone managed to get it working on postgres 15?

I have the same issue and it is due to this commit: nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgre… · Ma27/nixpkgs@4845956 · GitHub
it basically broke my nextcloud installation completely and I haven’t figured it out yet either :-/
The old config throws an error and doesn’t build, the new config complains about the missing “oc_appconfig” table. The main issue seems to be that the dbuser and dbname were different, but making them the same messes up the existing db.

Did you have any joy with getting this back up and running? I’ve got the same error. Everything else appears to still be in the database though, apart from the oc_appconfig table.

Had the same issue earlier today, but found out that one of my packages was forcing the use of PostgreSQL 14, instead of 15 and all my databases were using the newer version. Just had to make sure to set the package to 15 again and got it back working.
In my case the package / flake was GitHub - nix-community/authentik-nix: Nix flake with package, NixOS module and basic VM test for authentik. Trying to provide an alternative deployment mode to the officially supported docker-compose approach. Not affiliated with or officially supported by the authentik project [maintainer=@willibutz]