How to Configure Mastra Postgres Sandbox

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a mastra postgres sandbox by creating a restricted database role for your agent. Then you grant that role read permissions on specific tables or columns in a test database. Teams often start by giving an AI agent a broad database login during early tests. That shortcut becomes risky when the test database holds copied production records or wide permissions. A single bad prompt can cause the agent to drop tables or read private fields. Building a strict sandbox keeps agent tests safe a

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 mastra postgres sandbox by creating a restricted database role for your agent. Then you grant that role read permissions on specific tables or columns in a test database. Teams often start by giving an AI agent a broad database login during early tests. That shortcut becomes risky when the test database holds copied production records or wide permissions. A single bad prompt can cause the agent to drop tables or read private fields. Building a strict sandbox keeps agent tests safe and predictable.

How a native mastra postgres sandbox works

A safe sandbox starts inside the database permission system. Admins create a dedicated group role to manage access for one or more agent accounts. Individual agent logins then join that group role to inherit its permissions. This setup keeps role management clean as you add more tools and workflows.

Next, you lock down what the public role can do in the database. By default, the special public role gets permission to connect and create temporary tables. It also gets permission to run functions and procedures. Object owners can revoke those defaults so the agent only runs approved logic. In PostgreSQL 15 and newer, regular users cannot create new objects in the public schema. If your sandbox runs an older version, you must revoke create rights from the public schema.

After removing broad defaults, you grant read rights where the agent needs them. You can grant the select privilege on whole tables, views, or materialized views. You can also grant select on specific columns of a table. Column permissions stop the role from querying unapproved columns on that table. Note that the select privilege also allows the role to export data with copy commands.

What native roles solve and what they leave open

Native database permissions do a great job of controlling who can log in. They also control which tables and columns a given role may read or change. If an agent tries to read a forbidden column, the database blocks the query. If an agent tries to delete rows without permission, the database rejects the write. For basic schema isolation, native roles form a solid first line of defense.

Still, static database roles only check permissions at the table or column level. They do not inspect what individual statements do in flight once a session starts. If an agent has select rights on a customer table, it can pull every row. If that table holds emails, phone numbers, card numbers, or national IDs, the query returns them. You have to manage column grants by hand across every single table and view. Native roles also do not pause risky queries for human review before they run.

AI Sandbox Environments + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Managing test data and schema boundaries

Teams often refresh their sandbox by copying data from a live database. When you copy live data, old grants and sensitive columns travel with the dump. A sandbox role might gain read rights through inherited defaults on those copied objects. You must audit the schema after every refresh to keep the sandbox tight.

You should also separate the agent login from the role that runs schema migrations. If the agent uses the owner role, it can alter tables or wipe test fixtures. Giving the Mastra workflow its own limited login prevents accidental damage during test runs. Still, managing static credentials and column grants across many test databases takes constant effort. When schemas change often, teams need controls that work on live queries without manual grants.

Run guardrails and masking on the Postgres connection

Role grants and firewall rules are needed setup. Setup alone cannot check single SQL statements or mask sensitive rows in a response. By design, hoop.dev is an open-source proxy that sits beside Postgres, RDS, or Aurora as a sidecar for your Postgres. Clients and agent tools keep their credentials and point their connection string at the listener.

On each query, hoop.dev guardrails refuse disallowed drops, deletes, or table reads before they run. Next, hoop.dev data masking rewrites sensitive fields in returned rows that match your rules. Furthermore, hoop.dev holds SOC 2 Type II and logs every statement and verdict per resource. Check out the project on GitHub.

Frequently asked questions

Can a mastra postgres sandbox use column-level permissions?

Yes, PostgreSQL lets you grant select permissions on specific columns of a table or view. If an agent queries a column outside its grant, the database rejects the whole query.

Why can my sandbox agent still create objects in the public schema?

Databases created before PostgreSQL 15 allow the public role to create objects in the public schema. An admin must revoke the create privilege on that schema from the public role.

How does data masking handle sensitive fields in a sandbox?

Data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in results. The agent gets valid query results without receiving the raw values from those fields.

The query runs. The emails stay hidden.