How to Configure Mastra Neon Read Only Access

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up mastra neon read only access with a restricted Postgres role or a read replica. Then you pass that database URL to your Mastra agent tools. Most teams start by pasting their default project owner string into the app. That shortcut lets an AI agent change or delete live tables by mistake. How mastra neon read only access works in Postgres Mastra runs as a TypeScript framework that builds and runs AI agents. These agents use tools to run queries against your Postgres database. Neon r

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 mastra neon read only access with a restricted Postgres role or a read replica. Then you pass that database URL to your Mastra agent tools. Most teams start by pasting their default project owner string into the app. That shortcut lets an AI agent change or delete live tables by mistake.

How mastra neon read only access works in Postgres

Mastra runs as a TypeScript framework that builds and runs AI agents. These agents use tools to run queries against your Postgres database. Neon runs serverless Postgres and splits compute nodes from storage. You have two native ways to lock down what Mastra can do.

The first way uses a read-only compute endpoint in Neon. Neon lets you add a secondary read replica compute node to any branch. This compute node rejects all write queries at the engine level. You copy the connection string for this read node into your Mastra app. Every query from your agent then runs on a node that cannot write data.

The second way uses standard Postgres role permissions on your primary branch. You log in to your database and create a new user role. You grant this role the ability to connect to the database. Next, you grant usage on the target schema and select rights on the tables. You also set default rules so future tables grant select rights to this role. Finally, you give this role's login string to your Mastra agent setup.

Where native database roles fall short for AI agents

Native roles and read endpoints stop an agent from dropping tables or altering rows. They decide which account can log in and whether that account can write data. Still, they do not inspect what individual queries do while a session is active.

A read-only role still lets a Mastra agent select every row in a user table by default. If that table holds emails, phone numbers, or card numbers, the query returns them in plain text. Those raw fields then flow straight into the language model prompt context. Native Postgres roles have no built-in way to rewrite matched fields in query results on the fly.

Auditor Read-Only Access + Customer Support Access to Production: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Static database strings in your app settings also create key management work. If a key leaks in a log file, anyone with the string can read your database. Also, native database logs only show the shared role name used by the Mastra app. Your team cannot easily see which specific agent workflow or user triggered a heavy read query.

Managing agent queries and sensitive fields at runtime

Teams running AI agents in production need controls that act on each query in flight. First, you should pair your Neon read endpoint with strict row limits in your Mastra tools. This stops an agent from pulling millions of rows into memory at once.

Second, you need a way to handle sensitive columns before results reach the agent. Data masking rewrites matched fields like national IDs, emails, and phone numbers in query results. The agent gets the structure it needs to answer questions without pulling raw personal data into prompts. Combining read-only roles with query-level controls keeps your Neon database safe during agent runs.

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

Should I use a Neon read replica or a Postgres read-only role for Mastra?

Use both together for the best safety. A Neon read replica isolates agent compute loads from your main app traffic. Meanwhile, a scoped Postgres role limits which schemas and tables the agent can query.

Can a read-only role stop Mastra from pulling sensitive customer columns?

Standard table-level select grants return every column in a table by default. You can grant select rights on specific columns only, or use dynamic data masking to rewrite emails, phone numbers, and card numbers in the results.

Why do new tables fail when Mastra queries them with a read-only role?

Postgres table grants only apply to tables that exist when you run the grant command. You must also alter the default privileges for the schema so new tables grant select access to your Mastra role.

The query runs. The emails stay hidden.