Row-Level Security
Database access control
Row-Level Security is the foundation of CodePlanet's data protection. It ensures that users can only access their own data, enforced at the database level.
What is RLS?
Row-Level Security is a PostgreSQL feature that allows you to define policies controlling which rows users can see or modify. Unlike application-level security, RLS works at the database level, meaning it cannot be bypassed — even if there's a bug in the application code.
How It Works
Enabling RLS
Every table in CodePlanet has RLS enabled:
Policy Types
SELECT Policies (Read)
Control which rows can be read:
INSERT Policies (Create)
Control what data can be inserted:
UPDATE Policies (Modify)
Control which rows can be updated:
DELETE Policies (Remove)
Control which rows can be deleted:
CodePlanet Policies
Profiles Table
Submissions Table
Topic Performance Table
Payments Table
Public vs Private Tables
Some tables are intentionally public:
Service Role Bypass
For server-side operations, the service role bypasses RLS:
Important: The service role key must never be exposed to the client.
Common Patterns
Owner Access
Role-Based Access
Time-Based Access
Plan-Based Access
Testing RLS
Test your policies in the Supabase SQL editor:
Debugging RLS Issues
"No rows returned"
- Check if RLS is enabled (it blocks all if no matching policy)
- Verify the user ID matches
- Check policy conditions
"Permission denied"
- Check if there's a policy for the operation type
- Verify the user is authenticated
- Check the WITH CHECK clause for inserts/updates
View Active Policies
Best Practices
- Always enable RLS on new tables
- Use FORCE ROW LEVEL SECURITY to apply to table owners
- Test policies before deploying
- Prefer USING over WITH CHECK where possible
- Keep policies simple — complex policies hurt performance
- Document your policies in code
Performance Considerations
RLS adds overhead to queries. Optimize by:
- Indexing columns used in policies (especially
user_id) - Keeping policy conditions simple
- Avoiding subqueries in frequently-called policies
Next Steps
- API Security — Securing API endpoints
- Security Overview — Complete security guide
- Data Privacy — Privacy policies