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