System Manager is still in its early state, and doesn't yet have user management, which is a planned feature that will be here soon. As such, for now, before you run this, you'll need to manually create the postgres user. Additionally, go ahead and create two directories and grant the postgres user access to them:
{ config, lib, pkgs,...}:{config={ nixpkgs.hostPlatform="x86_64-linux"; environment.systemPackages=with pkgs;[ postgresql_16
];# PostgreSQL service systemd.services.postgresql={description="PostgreSQL database server";wantedBy=["multi-user.target"];after=["network.target"];serviceConfig={Type="notify";User="postgres";Group="postgres";ExecStart="${pkgs.postgresql_16}/bin/postgres -D /var/lib/postgresql/16";ExecReload="${pkgs.coreutils}/bin/kill -HUP $MAINPID";KillMode="mixed";KillSignal="SIGINT";TimeoutSec=120;# Create directories and initialize databaseExecStartPre=["${pkgs.coreutils}/bin/mkdir -p /var/lib/postgresql/16""${pkgs.bash}/bin/bash -c 'if [ ! -d /var/lib/postgresql/16/base ]; then ${pkgs.postgresql_16}/bin/initdb -D /var/lib/postgresql/16; fi'"];};environment={PGDATA="/var/lib/postgresql/16";};};# Initialize database and user systemd.services.postgresql-init={description="Initialize PostgreSQL database for myapp";after=["postgresql.service"];wantedBy=["multi-user.target"];serviceConfig={Type="oneshot";RemainAfterExit=true;User="postgres";};script='' # Wait for PostgreSQL to be ready until ${pkgs.postgresql_16}/bin/pg_isready; do echo "Waiting for PostgreSQL..." sleep 2 done # Optional: Create database if it doesn't exist${pkgs.postgresql_16}/bin/psql -lqt | ${pkgs.coreutils}/bin/cut -d \| -f 1 | ${pkgs.gnugrep}/bin/grep -qw myapp || \${pkgs.postgresql_16}/bin/createdb myapp # Optional: Create user if it doesn't exist${pkgs.postgresql_16}/bin/psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='myapp'" | ${pkgs.gnugrep}/bin/grep -q 1 || \${pkgs.postgresql_16}/bin/createuser myapp # Grant database privileges${pkgs.postgresql_16}/bin/psql -c "GRANT ALL PRIVILEGES ON DATABASE myapp TO myapp" # Grant schema privileges (allows creating tables!)${pkgs.postgresql_16}/bin/psql -d myapp -c "GRANT ALL ON SCHEMA public TO myapp"${pkgs.postgresql_16}/bin/psql -d myapp -c "GRANT ALL ON ALL TABLES IN SCHEMA public TO myapp"${pkgs.postgresql_16}/bin/psql -d myapp -c "GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO myapp" echo "PostgreSQL is ready and configured!" '';};};}