ugh..
This commit is contained in:
parent
7248fc5552
commit
529467cd0c
1 changed files with 10 additions and 9 deletions
19
flake.nix
19
flake.nix
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue