Give Claude Code read-only access to your Postgres database (and approve every write)
A coding agent that cannot see your database must guess the schema. It writes users.full_name when the column is display_name. It uses an enum value that does not exist. The code looks correct, and it fails on the first run.
I build pglens, an open-source Postgres tool. In version 3.8, I added a way to give a coding agent access to one database. The agent can read. It cannot write. When it wants a change, it sends a proposal, and the change runs only after a person approves it.
This guide shows the setup, and then explains how each part of the safety design works.
The usual options, and their cost
There are three usual ways to give an agent database context:
- Paste the schema into the chat. The schema is old after the next migration. The agent cannot see the data. You must do it again each session.
- Give the agent a
DATABASE_URL, or let it runpsql. The agent can now do everything that the role can do. The password is in a config file. - Use a generic Postgres MCP server. Most of these are read-only, so the agent cannot help with a change. Some have no limits at all.
I wanted the reads of the first two options, without the write access of the second.
Step 1: Install pglens and save a connection
curl -fsSL https://pglens.org/install.sh | bash pglens start On Windows, use iwr https://pglens.org/install.ps1 -useb | iex.
pglens start prints a local URL with an auth token. Open it, click Add Connection, and save your database with a name, for example mydb. pglens stores the password in the OS keychain, not in a file.
Step 2: Register the MCP server
For Claude Code:
claude mcp add pglens -- pglens mcp --connection mydb For Cursor, in .cursor/mcp.json:
{ "mcpServers": { "pglens": { "command": "pglens", "args": ["mcp", "--connection", "mydb"] } } } For Codex, in ~/.codex/config.toml:
[mcp_servers.pglens] command = "pglens" args = ["mcp", "--connection", "mydb"] The agent config contains a connection name. It does not contain a password. Thus, nothing secret goes into a dotfile or into your repository.
What the agent can do
The server gives the agent five tools:
-
list_tables: the tables in the schema. -
describe_table: columns, keys, foreign keys in both directions, and indexes. -
query: one read-only statement. It returns 100 rows by default, and 1,000 rows at most. -
propose_write: SQL and a reason, sent to a person for approval. -
proposal_status: the result of a proposal.
Now, when you ask for "an endpoint that returns the last 10 unshipped orders of a customer," the agent first calls describe_table on orders and customers. It reads the real column names before it writes the code.
How reads stay read-only
A prompt that says "do not write" is not a control. Thus, pglens enforces read-only in two layers.
First, a check examines the statement. It permits one SELECT, WITH, EXPLAIN, SHOW, VALUES or TABLE statement. It rejects writes, DDL and functions with side effects.
Second, the statement runs in a Postgres READ ONLY transaction, and pglens always rolls the transaction back. If a function hides a write, Postgres itself refuses it. The first check can be wrong. The second check is the database.
Three more limits apply:
- Each query stops after 15 seconds.
- Columns with names like password, hash, token, secret or API key come back as
[redacted]. - pglens logs each tool call. You can read the log with
pglens logs.
How writes work
The agent cannot run an UPDATE. It calls propose_write with the SQL and a reason. The proposal appears on the Agent requests page in the pglens UI. The sidebar shows the number of pending proposals.
You read the SQL and the reason. Then you select one of two actions:
- Approve. pglens runs all statements of the proposal in one transaction. If one statement fails, all of them roll back.
- Reject. You can write a note. The agent receives the note and can change its proposal.
Most agent calls are reads, and reads need no approval. The writes are usually migrations and data fixes. You would examine these before they run in any case.
A fault I found after the release
Version 3.8.0 had a fault. An agent proposed CREATE INDEX CONCURRENTLY. I approved it, and it failed. Postgres does not permit this statement in a transaction, and pglens ran each approved proposal in a transaction.
In 3.8.1, a proposal that is one such statement (CREATE INDEX CONCURRENTLY, DROP INDEX CONCURRENTLY, REINDEX CONCURRENTLY or VACUUM) runs outside a transaction. If the agent mixes one of these with other statements, pglens refuses the proposal when the agent sends it, not when you approve it.
What this setup does not do
This design has limits, and I do not want to hide them.
- The rows that the agent reads go to the agent's model provider, like everything else the agent reads. Redaction catches only the obvious credential columns. For sensitive tables, connect with a role that cannot read them:
CREATE ROLE agent_ro LOGIN PASSWORD '...'; GRANT USAGE ON SCHEMA public TO agent_ro; GRANT SELECT ON orders, customers, products TO agent_ro; - The pglens server must run when you approve a proposal.
- With
--urlinstead of a saved connection, the agent cannot propose writes. - pglens works only with Postgres.
Links
- Site: https://pglens.org
- Source (MIT): https://github.com/tsvillain/pglens