How to Configure Vercel AI SDK Postgres Sandbox

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a vercel ai sdk postgres sandbox by creating a strict database role. Then you wire that scoped role to your model tool calls. Many teams start by giving their app a shared admin database user. This wide permission lets a bad model prompt change or delete live data. A safe sandbox isolates the model and limits what tables it can read. Setting up a vercel ai sdk postgres sandbox with roles First, you create a dedicated group role inside your database for all model tasks. PostgreSQL l

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up a vercel ai sdk postgres sandbox by creating a strict database role. Then you wire that scoped role to your model tool calls. Many teams start by giving their app a shared admin database user. This wide permission lets a bad model prompt change or delete live data. A safe sandbox isolates the model and limits what tables it can read.

Setting up a vercel ai sdk postgres sandbox with roles

First, you create a dedicated group role inside your database for all model tasks. PostgreSQL lets you set up group roles to manage permissions for many users. You then create a specific login user for your app and add it to that group. Next, you lock down what the public role can do by default. In PostgreSQL, the special PUBLIC role gives permissions to every role on the system. By default, PUBLIC gets the right to connect and make temporary tables on databases. It also gets the right to run functions and procedures. The database owner can revoke these default rights to tighten the sandbox.

You also need to check schema rights if you run an older database. In PostgreSQL 15 and later, regular users cannot create objects in the public schema by default. On older versions or upgraded databases, an admin must revoke create rights from PUBLIC. After you lock down the defaults, you grant read access to your sandbox role. PostgreSQL lets you grant the select privilege on whole tables or views. You can also grant select rights on specific columns of a table. This column rule stops the model tool from reading secret fields in a shared table.

Integrating tool calls with database sessions

Once the database role is ready, you connect it to your model code. The Vercel AI SDK lets language models call custom tools during a chat. You define a query tool that accepts structured inputs or raw query strings. When the model calls this tool, your server opens a session with the sandbox role. The database checks the role permissions before it runs the query. If the model tries to write data or read a blocked column, the database stops it. The tool then sends the safe rows back to the model to finish the reply.

What native database roles solve and what they miss

Native database roles do a great job of blocking unauthorized table access. They decide which user can log in and which columns that user can read. Still, static roles cannot inspect what an individual statement does in flight. A read role still lets a model run a huge query that slows the server. Also, if a model needs access to a customer table, it reads every row. Native permissions do not rewrite sensitive fields like emails or phone numbers in the results. They also do not tie a specific database query back to the user who prompted the model. Teams still need a way to mask live data and audit every model query.

AI Sandbox Environments: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Add query and result controls beside Postgres

Grants and pools govern who logs into PostgreSQL. Yet every query in an open session still runs with that role's full scope. In practice, hoop.dev runs beside your database as a sidecar for your Postgres. It reads plain and prepared queries on the wire with no change to your users or drivers.

When a tool or agent sends SQL, hoop.dev Postgres controls check each statement against your deny rules. An AI Analyzer you configure can also score intent or hold a risky query for human review. Matched sensitive columns in result rows are masked in memory before they reach the caller. Each query and decision is logged per listener, which generates evidence for SOC 2, HIPAA, and GDPR audits. View the binary on GitHub.

Frequently asked questions

Why should a model tool use a separate Postgres role?

A separate role limits damage if a prompt tricks the model into running bad queries. The database blocks any write or delete commands that go beyond the role permissions.

Can PostgreSQL restrict a model to specific columns?

Yes, PostgreSQL lets an admin grant the select privilege on specific columns of a table. The model tool can then read safe columns while secret columns stay blocked.

Why do default PUBLIC privileges matter for a database sandbox?

The PUBLIC role grants default database connect and function execution rights to every new user. Revoking these defaults stops your sandbox user from running internal functions it does not need.

The query runs. The emails stay hidden.