1.9 KiB
1.9 KiB
Fix Database Permissions Error
Problem
You're getting this error:
error: permission denied for schema public
code: '42501'
This happens because the read_write_user doesn't have CREATE permission on the public schema.
Solution
You need to grant permissions using a database admin/superuser account. The read_write_user cannot grant permissions to itself.
Option 1: Using Admin Database URL (Recommended)
-
Get admin database credentials from your AWS RDS console or database administrator
- You need a user with superuser privileges or the schema owner
-
Add to your
.envfile:ADMIN_DATABASE_URL=postgresql://admin_user:admin_password@db.livingai.app:5432/livingai_test_db -
Run the setup script:
npm run setup-db
Option 2: Manual SQL (If you have database access)
Connect to your database using any PostgreSQL client (psql, pgAdmin, DBeaver, etc.) as an admin user and run:
GRANT USAGE ON SCHEMA public TO read_write_user;
GRANT CREATE ON SCHEMA public TO read_write_user;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
Option 3: AWS RDS Console
If you're using AWS RDS:
- Go to AWS RDS Console
- Find your database instance
- Use "Query Editor" or connect via psql with master credentials
- Run the SQL commands from Option 2
Verification
After running the fix, verify permissions:
SELECT
has_schema_privilege('read_write_user', 'public', 'USAGE') as has_usage,
has_schema_privilege('read_write_user', 'public', 'CREATE') as has_create;
Both should return true.
Why This Happens
- PostgreSQL doesn't allow users to grant permissions to themselves
- The
read_write_userneeds CREATE permission to create tables (likeotp_codes) - Only a superuser or schema owner can grant these permissions
After Fixing
- Restart your application
- Try creating an OTP - it should work now