How to Audit Agent Data Access Logs in Supabase
How to
2026-11-23
9 min read
Intended first value
You leave with a four-step setup that ties every agent write in Supabase to a named role, a statement log, and a before-and-after row record.
Auditing agent data access in Supabase takes more than opening Logs Explorer. Each agent needs its own identity, statement-level logging, a record of row changes, and access policies, all set up before the agent runs its first query. Those controls make operational access attributable. A separate governed data layer then keeps analytical queries off production entirely. Dreambase covers the parts Supabase leaves to you: a daily security check on every connected project, alerts when it finds a gap, and governed datasets so analytics agents never need a production credential.
Why agent queries are invisible in a default Supabase setup
Open the logs after an incident and you'll often see the same role name on hundreds of queries, with developer activity and agent writes mixed together. The statements are there. Who sent them isn't. When an agent connects with the postgres admin role or the project's default database password, Postgres logs its queries under the same role as manual dashboard queries. That's an identity problem before it's a logging problem.
Agents make the ambiguity more expensive. They retry dozens of times, write across several tables in seconds, and run in the background with nobody watching. A developer-facing MCP server at least shows tool calls during an interactive session, but a hosted orchestrator runs on its own schedule with a standing credential. The gap between a bad write and its discovery grows while changes pile up, and more log volume can't reveal an identity the connection never provided.
The four logging surfaces Supabase provides and what each one can't see
Logs Explorer brings together API Gateway, Auth, Postgres, Storage, PostgREST, Realtime, Edge Function, and pooler logs. Its Postgres logs show that a query ran, which role ran it, and when. They don't show row-level changes or what data left the database, and they're only as useful as the role name is specific.
Platform Audit Logs cover the control plane: project settings changes, team member actions, Edge Function changes. They don't record data-plane activity, so a leaked service_role key reading rows or a coding agent querying production is out of scope. Platform Audit Logs are only available on the Team and Enterprise plans.
pgAudit extends Postgres logging with selective tracking of reads, writes, and DDL at session, role, object, or global scope. It records the SQL text and the database role. Requests that arrive through PostgREST usually show up as authenticated or anon, so the person or agent behind them stays unnamed.
Trigger-based audit tables close a different gap. They record each row's before-and-after state, auth.uid(), and custom context like an agent name or task ID.
Retention separates these surfaces too. Log retention depends on your plan, with longer windows on Team and Enterprise. Trigger records live in your database, so the logging plan doesn't limit them. That makes an audit table the right base for a plan-independent history, though it can't recover an identity that a service-key request never carried.

How the October 30 Data API grants change affects agents
As of October 30, 2026, Supabase no longer grants Data API access to new tables in the public schema automatically. A new table stays unreachable through the Data API until you run an explicit GRANT. Existing tables keep their current grants, and direct Postgres connections aren't affected. A request against an ungranted table returns a PostgREST 42501 error whose hint names the missing grant. That helps with debugging, but the hint also confirms the table exists. Supabase’s changelog has the full rollout.
If an agent creates a table meant for the Data API, its instructions need to include the grants, or the agent will hit errors and may retry. Tables that should stay off the API, including audit tables, should skip those grants, since no access is the goal there. And because the change only covers new tables, your audit still has to check the grants on every existing table.
Grants decide whether a role can reach a table. They can't tell two agents apart when both use the same credentials, so the setup starts with identity.
Step 1: Create a dedicated Postgres role for every agent

The Postgres role is the identity database logging sees, so a dedicated agent role gives every statement an attributable name from the moment it connects. Create a separate database user for each tool or assistant, with its own password. Don't hand agents the postgres password: it grants admin access across schemas, and a narrowly scoped agent task doesn't need that.
Start with CREATE ROLE "agent_role" LOGIN NOBYPASSRLS;, then grant only the tables the task needs. A read-only task gets GRANT SELECT ON <required_table> TO "agent_role";. Leave billing, auth, and sensitive log tables out. A write-capable task gets INSERT and UPDATE only where needed, and never permission to alter, drop, or truncate tables.
Role granularity decides how good your attribution is. Five engineers sharing one agent role still look like one user, so where accountability matters, a per-tool role may need to become a per-developer or per-session role. Prefer short-lived credentials where your agent framework supports them, because credentials sitting in local config or shell variables are available to the agent without any human check. The role labels activity on its own connection only. PostgREST requests don't become named-agent sessions just because the role exists. The role names the activity, and pgAudit decides what gets logged under that name.
Step 2: Configure pgAudit to log agent writes and schema changes
For an agent role, start with writes and DDL: ALTER ROLE "agent_role" SET pgaudit.log TO 'write, ddl';. Write logging covers data changes. DDL logging covers schema changes, including new tables that need a check for missing RLS. This scope leaves analytical SELECT statements out of the log and keeps the operations that matter most for data integrity and exposure. To cover writes that arrive through the Data API, Supabase’s docs set the same parameter on the authenticator role, though those records still show anon or authenticated.
pgAudit's documentation warns that global logging can produce so much output that important events get buried, and logging all on a busy database makes logs bigger and more expensive. Role-scoped logging keeps the signal narrow. Each record includes the role, timestamp, SQL text, statement ID, and substatement ID. By default it doesn't include the returned rows, the human who started the task, a task ID, or a conversation thread, and requests through PostgREST still show the generic application role.
To investigate, open Logs Explorer, choose the Postgres source, and filter the event message for AUDIT. You can also query the logs with SQL over a time range to pull events for one role or window. That tells you which statements ran during an incident. It doesn't reconstruct each row's old and new values, and it can't name an end user the session didn't carry.
Step 3: Build a trigger-based audit table that captures row state and agent identity
Attach a generic AFTER INSERT OR UPDATE OR DELETE row trigger that writes every change to one audit.record_version table. Store old and new row state as JSONB, alongside org_id, actor_id, and actor_claims columns. Record auth.uid() directly, since pgAudit doesn't capture the application user, and add an agent name or task ID wherever your application can pass one. Each record then ties the changed data to the identity and context that were present when it ran.
Writes made with the service_role key bypass RLS and carry no user, so auth.uid() returns null and the actor column stays empty. Agents that write through your application should use a user-bound JWT, not a service key. A dedicated database role and a user-bound JWT answer different attribution questions, and neither replaces the other.
Make the audit table append-only for application roles. Keep it in a schema that isn't exposed through the Data API, skip the API grants, and write to it through a SECURITY DEFINER trigger function with search_path = ''. Give application roles no direct INSERT, UPDATE, or DELETE on the table, and add a guard trigger that raises an exception on UPDATE, DELETE, or TRUNCATE. A compromised agent with ordinary application access then can't rewrite its own history.
Triggers complement statement logging rather than replacing it. They capture context and intent, but they can't guarantee coverage if someone disables the trigger or changes the schema. pgAudit records operations outside the row history, within its configured scope. Together, roles, statement logs, and trigger records let you reconstruct writes, though none of them records every value that was read.
Step 4: Write RLS policies that treat agent identity as an input
Prevention starts with RLS on every table an agent touches. When an agent creates a table, ALTER TABLE ... ENABLE ROW LEVEL SECURITY belongs in the same migration. A grant makes access possible, and an RLS policy limits which rows that access reaches. Where the named agent role is the database identity, reference it in policies instead of relying only on the broad authenticated role. Our Supabase Row Level Security guide covers writing and testing these policies as real clients.
For user-bound requests, policies also need to account for the user identity the request carries, and the audit table's actor and organization columns record who used that access. Test access through joins as well as direct reads, because agents often ask for related data instead of following your app's usual query path. What matters is every row a request can reach.
An agent allowed to update rows shouldn't be able to DROP, TRUNCATE, or ALTER the table. Keep those privileges off its role rather than expecting row policies to handle schema operations. Audit tables stay off the Data API through schema exposure settings and missing grants, and their protection depends on that boundary plus the append-only design.
Permitted rows can still hold sensitive fields. A read-only policy may let an agent return every column, PII included, so add column-level restrictions or a view that leaves sensitive fields out. Even a fully configured stack has limits: it constrains access and preserves records, but analytical questions still need a data path built to answer them safely.
How a governed data layer lets agents query analytics without touching production
Analytical questions don't need an operational database connection. Pre-modeled, pre-calculated datasets can give an agent the context it needs while production tables, PII, billing, and auth stay behind a separate boundary. That matters because a read log is written after the read happens. It can't take back what was disclosed.
Dreambase is built on this separation. It models your Supabase Postgres data into governed datasets and pre-calculated metrics, stored as Parquet and queried with DuckDB. People use them through dashboards, and agents query them through a single MCP server. An analytics agent working this way never holds a production write credential, so a bad query has no production blast radius, and your audit covers one governed interface instead of open-ended database access.
How Dreambase compares with Supabase’s own audit tools
Supabase’s tools and Dreambase answer different questions, and you’ll likely want both.
Logs, pgAudit, and audit tables: the authority on what ran and what changed in your database. You set them up per role and query them yourself, and nothing replaces them for attributing writes.
Platform Audit Logs: the record of who changed project settings, members, and Edge Functions, on the Team and Enterprise plans.
Dreambase: a daily check across security, performance, indexing, errors, and capacity on every connected project, with new findings sent to Slack, email, a webhook, or an agent. Analytics agents read governed datasets through the Dreambase MCP instead of production. It doesn’t keep row-level history, so keep the audit table for that.
To route those findings to the right people, see our Supabase alerts guide. To keep agent analytics off the primary altogether, see how to run analytics queries on Supabase without slowing production.
Agents that manage state, work a task queue, or make user-facing writes still need production access, and for those, dedicated roles, pgAudit, trigger records, and agent-aware RLS are the controls to put in place. To move your analytics agents onto governed datasets instead, meet with the team and we'll get your metrics defined and running.
Start for free to connect your Supabase project and see its security findings.
NEXT STEP
MCP Security: Giving an Agent Access to Operational Data Safely
What goes wrong when an agent can query production, and how to put a governed boundary in front of it.