Files
supabase/apps/docs/content/guides/storage/schema/custom-roles.mdx
b24330e6bc docs(storage): fix create role statement in custom roles guide (#53724)
## Problem

The first step of the Storage "Custom Roles" guide uses `create role
'manager';`. Role names are identifiers, not string literals, so
Postgres rejects it with `syntax error at or near "'manager'"`. Every
later step in the guide depends on that role.

Closes #53723

## Solution

Remove the single quotes: `create role manager;`. The rest of the page
already uses the unquoted name (`grant manager to authenticator;`, `to
manager`).

## Review instructions

1. Open the [Custom Roles
guide](https://supabase.com/docs/guides/storage/schema/custom-roles) and
look at the first SQL block: `create role 'manager';` is a syntax error
in Postgres (reproduced with the Postgres parser, see #53723).
2. Open the preview of this PR. The block is now `create role manager;`,
which parses, and the following `grant` statements refer to the same
unquoted name.
3. Run `pnpm test:prettier`. It passes for this file with the repo's
`prettier.config.mjs`.

I also parsed every `sql` block under `guides/storage/schema` with the
Postgres parser (`libpg-query`). All 7 parse after this change.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide).
(Not run: this is a one-token correction to an existing SQL statement.)

Co-authored-by: Santhosh-x23 <185920429+Santhosh-x23@users.noreply.github.com>
Co-authored-by: Nik Richers <nrichers@gmail.com>
2026-12-01 10:10:03 -07:00

74 lines
2.3 KiB
Plaintext

---
id: 'storage-schema-design'
title: 'Custom Roles'
description: 'Learn about the storage schema'
subtitle: 'Learn about using custom roles with storage schema'
sidebar_label: 'Schema'
---
In this guide, you will learn how to create and use custom roles with Storage to manage role-based access to objects and buckets. The same approach can be used to use custom roles with any other Supabase service.
Supabase Storage uses the same role-based access control system as any other Supabase service using RLS ([Row Level Security](/docs/guides/database/postgres/row-level-security)).
## Create a custom role
Create a custom role `manager` to provide full read access to a specific bucket. For a more advanced setup, see the [RBAC Guide](/docs/guides/api/custom-claims-and-role-based-access-control-rbac#create-auth-hook-to-apply-user-role).
```sql
create role manager;
-- Important to grant the role to the authenticator and anon role
grant manager to authenticator;
grant anon to manager;
```
## Create a policy
Create a policy that gives full read permissions to all objects in the bucket `teams` for the `manager` role.
```sql
create policy "Manager can view all files in the bucket 'teams'"
on storage.objects
for select
to manager
using (
bucket_id = 'teams'
);
```
## Test the policy
To impersonate the `manager` role, you will need a valid JWT token with the `manager` role.
You can create one using the `jsonwebtoken` library in Node.js.
<Admonition type="danger">
Signing a new JWT requires your `JWT_SECRET`. You must store this secret securely. Never expose it in frontend code, and do not check it into version control.
</Admonition>
```js
const jwt = require('jsonwebtoken')
const JWT_SECRET = 'your-jwt-secret' // You can find this in your Supabase project settings under API. Store this securely.
const USER_ID = '' // the user id that we want to give the manager role
const token = jwt.sign({ role: 'manager', sub: USER_ID }, JWT_SECRET, {
expiresIn: '1h',
})
```
Now you can use this token to access the Storage API.
```js
const { StorageClient } = require('@supabase/storage-js')
const PROJECT_URL = 'https://your-project-id.supabase.co/storage/v1'
const storage = new StorageClient(PROJECT_URL, {
authorization: `Bearer ${token}`,
})
await storage.from('teams').list()
```