Packaging Postgresql database

I have a piece of Python package foo which depends on openstreetmap-carto style.
foo to run and be able to render maps requires connection to an PostgreSQL database with preprocessed data.

It is relatively easy to make derivation creating the pgdata directory, or the database SQL dump after the load. I’m not sure thou how to make it usable. Ideally it would be perfect if foo can be wrapped with some script starting this database as user systemd service or something.

Do you have any suggestions how to deal with it?

The idea behind packages is just to provide software and maybe data. You can provide a systemd unit configuration within that derivation which can define dependencies to other services (e.g.). You could then have it run a script before your service (ExecStartPre) that checks the database if it already contains your data and inserts it if not.

I see I was able to achieve the best results by simply preparing the pgdata directory. Approach with pg_dump was much slower, both on initialization and startup.

Because pgdata have to be writable, I’m currently struggling with improving current approach of copying it from nix store to /tmp.

Probably I could use for example overlay fs or something like that.