QuestionMy application DB user must be granted access to newly created tables after a migration. Right now we do it "manually" (add grant statements to migration) Is there a way to automate this? How to reproduce (optional)Expected behavior (optional)No response Information about Prisma Schema, Client Queries and Environment (optional)// Add your schema.prisma// Add any relevant Prisma Client queries here
|
Replies: 3 comments 1 reply
|
Hi @gcb! Currently you would need to manually grant the permissions. We have a related feature request here, which should make it easier to do this in future: |
Automating Post-Migration Tasks (GRANTs, etc.)I see you're looking for a way to automate database permissions after running 1. The "Custom Script" Wrapper (Recommended)The most robust way is to wrap your migration command in a script (Bash or JS) that runs your GRANT statements immediately after the migration succeeds. This ensures the logic is versioned alongside your app. Example #!/bin/bash
npx prisma migrate deploy
if [ $? -eq 0 ]; then
echo "Migration successful, applying permissions..."
# Use a raw SQL client to run your grants
psql $DATABASE_URL -f ./scripts/apply_grants.sql
else
echo "Migration failed, skipping grants."
exit 1
fi2. Using "Default Privileges" (Database Level)If you are using PostgreSQL, you can solve this permanently at the database level without needing a hook. You can set Default Privileges so that any future tables created by your migration user are automatically granted to your application user. Run this once in your DB: ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO your_app_user;Now, every time 3. Prisma "Seed" as a Hook (Workaround)Some teams use the Recommendation: If this helps automate your permissions, please consider marking it as the answer! |
|
No native hook yet. Two clean-ish options: chain it in your deploy script ( |
Automating Post-Migration Tasks (GRANTs, etc.)
I see you're looking for a way to automate database permissions after running
prisma migrate. While Prisma doesn't currently have a built-in "post-migration hook" in theschema.prismafile, there are two established ways to handle this in a production workflow.1. The "Custom Script" Wrapper (Recommended)
The most robust way is to wrap your migration command in a script (Bash or JS) that runs your GRANT statements immediately after the migration succeeds. This ensures the logic is versioned alongside your app.
Example
migrate.sh: