Loading...
Loading...
Provision a hardened SELECT-only Postgres role so AI agents can safely read a production database. Works on Supabase and any Postgres. Use when the user wants agents to query prod data, says "read-only role", "safe prod DB access for agents", or is tired of running SQL by hand for agents. Differentiator: this skill CREATES the role and wiring; day-to-day querying belongs in a project-local skill.
npx skill4agent add davidondrej/skills create-readonly-db-rolepublicauthdefault_transaction_read_only = onstatement_timeoutalter role ... bypassrlsselect rolname from pg_roles where rolname = 'agent_reader';agent_readerdocs/database/create-agent-reader-role.sql<role>.<project-ref>psqllibpq-- Example only. Rename the role, timeout, and denylist for your system.
-- 1. role + soft guardrails
create role agent_reader with login password 'REPLACE_ME';
alter role agent_reader set default_transaction_read_only = on;
alter role agent_reader set statement_timeout = '10s'; -- example timeout; change as needed
-- 2. SELECT-only grants, denylist model
grant usage on schema public to agent_reader;
grant select on all tables in schema public to agent_reader;
alter default privileges for role postgres in schema public
grant select on tables to agent_reader; -- future tables auto-readable
-- 3. denylist: keep secrets and PII invisible (replace with your own tables)
revoke select on table public.secrets from agent_reader;
revoke select on table public.private_events from agent_reader;
-- 4. only if RLS is enabled, no policy covers this role, and bypass fits your model
alter role agent_reader bypassrls;drop owned by agent_reader; drop role agent_reader;# Load the connection URL from your secret manager or local environment configuration.
psql "<readonly-connection-url>" -X -c "select current_user;" # -> agent_reader
psql "<readonly-connection-url>" -X -c "show statement_timeout;" # -> matches your chosen timeout
psql "<readonly-connection-url>" -X -c "select count(*) from public.<big_table>;" # -> real number, NOT 0
psql "<readonly-connection-url>" -X -c "delete from public.<any_table> where false;"
# -> ERROR: read-only transaction (soft guardrail)
psql "<readonly-connection-url>" -X -c "begin; set transaction read write; delete from public.<any_table> where false; rollback;"
# -> ERROR: permission denied (the hard wall)
psql "<readonly-connection-url>" -X -c "select * from public.<denylisted> limit 1;" # -> ERROR: permission denied
psql "<readonly-connection-url>" -X -c "select * from auth.<identity_table> limit 1;" # -> ERROR: permission deniedpermission deniedbypassrls<role>.<project-ref>statement timeoutrevoke selectalter role agent_reader with password '...'