Read-only PostgreSQL for AI
What to grant
Privilege
Why
Example: create a read-only role
-- Create a login role for AI / MCP exploration (set your own password).
DO $$
BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'prest_readonly') THEN
CREATE ROLE prest_readonly LOGIN PASSWORD 'change-me';
END IF;
END
$$;
-- Allow connection to your database (replace `mydb`).
GRANT CONNECT ON DATABASE mydb TO prest_readonly;
-- Allow schema usage (replace `public` if needed).
GRANT USAGE ON SCHEMA public TO prest_readonly;
-- Grant SELECT on specific tables:
GRANT SELECT ON TABLE public.users TO prest_readonly;
-- GRANT SELECT ON TABLE public.orders TO prest_readonly;
-- Or grant SELECT on all existing tables in the schema:
-- GRANT SELECT ON ALL TABLES IN SCHEMA public TO prest_readonly;
-- Optional: future tables created by the current role get SELECT for prest_readonly
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO prest_readonly;Verify
Tips
Next steps
Related documentation
Last updated