This commit is contained in:
zack 2024-10-21 20:49:22 -04:00
parent 7248fc5552
commit 529467cd0c
No known key found for this signature in database
GPG key ID: 5F873416BCF59F35

View file

@ -130,6 +130,7 @@
wantedBy = ["multi-user.target"]; wantedBy = ["multi-user.target"];
after = ["postgresql.service"]; after = ["postgresql.service"];
before = ["zoeyscomputer.service"]; before = ["zoeyscomputer.service"];
path = with pkgs; [util-linux];
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
@ -140,20 +141,20 @@
# Read password from file # Read password from file
PASSWORD=$(cat ${cfg.phx.database.passwordFile}) PASSWORD=$(cat ${cfg.phx.database.passwordFile})
# Check if user exists # Create user and database if they don't exist
if ! psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='${cfg.phx.database.user}'" | grep -q 1; then if ! runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='${cfg.phx.database.user}'" | grep -q 1; then
# Create user with password if it doesn't exist echo "CREATE USER ${cfg.phx.database.user} WITH PASSWORD '$PASSWORD'" | \
psql -c "CREATE USER ${cfg.phx.database.user} WITH PASSWORD '$PASSWORD'" runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/psql
else else
# Update password if user exists echo "ALTER USER ${cfg.phx.database.user} WITH PASSWORD '$PASSWORD'" | \
psql -c "ALTER USER ${cfg.phx.database.user} WITH PASSWORD '$PASSWORD'" runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/psql
fi fi
# Ensure user has access to database # Grant privileges to the user
psql -c "GRANT ALL PRIVILEGES ON DATABASE ${cfg.phx.database.name} TO ${cfg.phx.database.user}" echo "GRANT ALL PRIVILEGES ON DATABASE ${cfg.phx.database.name} TO ${cfg.phx.database.user}" | \
runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/psql
''} ''}
''; '';
path = [config.services.postgresql.package];
}; };
# Ensure database exists # Ensure database exists