App Finishers

Fix guide · updated 2026-11-13

Supabase Storage upload fails: "new row violates row-level security policy"

new row violates row-level security policy · StorageApiError · upload to bucket refused

Your app tries to upload a file, often a profile picture, a receipt or a product photo, and gets new row violates row-level security policy. Nothing is wrong with the file. Supabase keeps a record of every file in a database table called storage.objects, and that table is protected by row-level security (RLS), like your own tables. Each upload adds a row to it. By default Storage refuses every upload until a policy allows it, so the message means: no policy said yes to this upload.

1. Find the request that failed

In Chrome, open the developer tools (Ctrl + Shift + I on Windows, Cmd + Option + I on a Mac), click the Network tab, then try the upload again. Click the request whose address contains /storage/v1/object/. The address shows two things you need: the bucket name, right after /object/, and the file path after it. For example, /storage/v1/object/avatars/2f9c…/photo.png means bucket avatars, path 2f9c…/photo.png. The Response tab shows the message.

Write down the bucket name and the path exactly. Most fixes come down to the policy not matching one of them.

2. Add an upload policy for that bucket

Uploading needs one permission: INSERT on storage.objects. You add it as a policy. In the Supabase dashboard, open the SQL Editor and run a policy like this one, which lets signed-in users upload into the avatars bucket, but only inside a folder named after their own user ID:

create policy "Users upload into their own folder"
on storage.objects for insert
to authenticated
with check (
  bucket_id = 'avatars'
  and (storage.foldername(name))[1] = (select auth.jwt() ->> 'sub')
);

Replace avatars with your bucket name. storage.foldername(name) returns the folders of the file's path, so [1] is the first folder. auth.jwt() ->> 'sub' is the signed-in user's ID. Both come from Supabase's own Storage documentation.

This policy works only if the app puts each file in a folder named after the user. The upload code must build the path that way:

const { data: { user } } = await supabase.auth.getUser()
const path = `${user.id}/${file.name}`
const { error } = await supabase.storage.from('avatars').upload(path, file)

3. Overwriting a file needs two more permissions

If your app replaces a file at the same path, a new avatar for example, it uploads with upsert: true. Supabase's docs say an overwrite needs SELECT and UPDATE on storage.objects on top of INSERT. With only the upload policy above, the first upload works and every replacement fails with the same message. Add both:

create policy "Users read their own files"
on storage.objects for select
to authenticated
using (
  bucket_id = 'avatars'
  and (storage.foldername(name))[1] = (select auth.jwt() ->> 'sub')
);

create policy "Users replace their own files"
on storage.objects for update
to authenticated
using (
  bucket_id = 'avatars'
  and (storage.foldername(name))[1] = (select auth.jwt() ->> 'sub')
);

Without upsert, uploading to a path that already exists gives a different error, "The resource already exists" (status 400, "error":"Duplicate"; Supabase's docs call it Asset Already Exists). That one is not a policy problem. Use a new file name, or turn on upsert and add the two policies: see Supabase Storage "The resource already exists".

4. The usual mismatches in AI-built apps

When a policy exists and uploads still fail, the policy and the request disagree somewhere. Compare them one by one:

To see the policies that exist, open Storage in the dashboard, then Policies. If the error comes from saving a row in one of your own tables instead of a file, read our guide to new row violates row-level security policy on a table.

Asking Lovable or Bolt to fix it

A vague prompt such as "fix the upload error" often gets you a policy that allows everything, or code that moves the upload somewhere else. Name the parts instead:

File uploads to the Supabase bucket "avatars" fail with "new row violates
row-level security policy". Create policies on storage.objects for this
bucket: INSERT, SELECT and UPDATE for authenticated users only, where
(storage.foldername(name))[1] = auth.uid()::text. Upload each file to
`${user.id}/${fileName}` with upsert: true. Don't use the service role key.

Then check the policies the AI created against the bucket name and the path you wrote down in step 1.

The fix you must never use

The service role key gets past every policy, so putting it in the app makes the error disappear. It also lets anyone who opens your site read, replace or delete every file in every bucket, because anything shipped to the browser can be read by visitors. Supabase's docs say to use that key only from trusted servers and never to share it publicly. If an upload must skip the policies, do it inside an Edge Function, where the key stays on the server.

Still failing?

Look at the request again. If its address, bucket and path match your policies and the user is signed in, compare the policy's role (to authenticated) with how the app calls Storage. Uploads sent with only the public key, without the user's session, run as anonymous.

Other Supabase problems? See Supabase fixes.