Introduction
Select policies control who can read data from your database. They keep your data safe by allowing only the right people to see it.
Select policies control who can read data from your database. They keep your data safe by allowing only the right people to see it.
CREATE POLICY policy_name ON table_name FOR SELECT USING (condition);
CREATE POLICY "Allow logged-in users to read"
ON profiles
FOR SELECT
USING (auth.uid() = user_id);CREATE POLICY "Public read access"
ON products
FOR SELECT
USING (true);This example creates a messages table and a policy that lets users read only their own messages by matching their user ID with the authenticated user ID.
CREATE TABLE messages (
id SERIAL PRIMARY KEY,
user_id UUID,
content TEXT
);
-- Enable row level security
ALTER TABLE messages ENABLE ROW LEVEL SECURITY;
-- Policy: users can read only their own messages
CREATE POLICY "Users can read own messages"
ON messages
FOR SELECT
USING (auth.uid() = user_id);Always test your policies to make sure they allow the right access.
Remember to enable row level security on the table before creating policies.
Select policies control who can read data in your tables.
Use the USING clause to set conditions for read access.
Enable row level security to activate policies.
SELECT policy in Supabase?profiles?CREATE POLICY "User can read own profile" ON profiles FOR SELECT USING (auth.uid() = user_id);
user_id is different from their auth.uid()?CREATE POLICY "Allow read" ON orders FOR SELECT USING (user_id = auth.uid);
messages table. The table has columns id, content, and owner_id. Which policy correctly enforces this read access?Pass the test and this pattern moves unknown