GateSQL

The agent database gateway.

AI agents are at their most powerful when they can query your database directly. But giving them database credentials is a security nightmare: passwords get leaked, queries spiral out of control, and you have no idea why an agent ran a particular query.

GateSQL gives each agent a short-lived, scoped session instead. You control what they can access, how much they can query, and every action is logged with intent.

Get Started GitHub Learn More
Works with any PostgreSQL (MD5, SCRAM-SHA-256, SSL/TLS)
More databases coming soon ↓

Parent creates session, agent connects

The entity spawning the agent gets a short-lived JWT. The agent uses it as a standard PostgreSQL password.

gatesql
gatesql :15432 → :5432

How it works

Native PostgreSQL wire protocol from end to end. No ORMs, no middleware.

AI Agent
psycopg / asyncpg / any PG client
PG wire →
GateSQL
JWT auth + policy
PG wire →
PostgreSQL
real credentials stay here

Every query needs a reason

Agents declare intent with every query. No purpose, no results.

Allowed

"check low-stock items for reorder alert"

/* <agent_purpose>check low-stock items for reorder alert</agent_purpose> */
SELECT product, qty FROM inventory WHERE qty < 10

Rejected

No purpose provided

SELECT * FROM users

Built for agent workloads

Short-lived JWTs

Parent creates a session, gets a JWT with hard expiry. Agent never sees real database credentials.

Read-only sessions

Enforce read-only access per session. Writes are rejected at the proxy before reaching the database.

Table allowlists

Restrict which tables an agent can access per session. Enforced via AST analysis. Covers JOINs, subqueries, and CTEs.

Dangerous query detection

Block DROP, TRUNCATE, DELETE without WHERE, UPDATE without WHERE. Configurable: block, warn, or allow.

Purpose enforcement

Every query must include a purpose comment. Queries without one are rejected. Builds an audit trail of why, not just what.

Query budgets

Set a max query count per session. When the budget is exhausted, the connection is closed.

AST-powered analysis

Every query parsed by PostgreSQL's actual parser (libpg_query). Catches CTEs with hidden writes, multi-statement injections, and more.

Full audit log

JSON-lines query logs with agent ID, session ID, purpose, query text, and timing. Every query attributed.

Admin API + Dashboard

Create, list, and revoke sessions over HTTP. Live dashboard shows active sessions, queries, and governance rejections.

Idle & hard cap timeout

Sliding-window idle timeout with configurable duration. Plus a hard cap on total session lifetime.

Standard clients

Works with psql, psycopg, asyncpg, pgx, node-postgres, JDBC, or any PostgreSQL client. No custom SDK required.

2.8x overhead

Zero-allocation relay with ArrayPool. Flush at sync points only. Less overhead than the REST API you'd build instead.

Coming soon

Query impact analysis

Run EXPLAIN before forwarding. Estimate affected rows before UPDATE/DELETE. Reject queries that would be too expensive or touch too many rows.

Human-in-the-loop approvals

Agent hits a permission boundary, proxy pauses and asks a human. Approve or deny from the dashboard or Slack.

MCP server

GateSQL as an MCP server. Any MCP client (Claude, Cursor, Windsurf) gets governed database access with zero custom code.

Quick start

1

Start the proxy

Point it at your PostgreSQL and set an API key.

docker run -p 15432:15432 -p 8080:8080 \
  -e GATESQL_UPSTREAM_HOST=my-pg.example.com \
  -e GATESQL_UPSTREAM_PASSWORD=secret \
  -e GATESQL_API_KEY=pk_prod_abc123 \
  gatesql/gatesql
2

Create a session

Define what the agent can do. Read-only, specific tables, query budget.

curl -X POST http://localhost:8080/api/sessions \
  -H "X-Api-Key: pk_prod_abc123" \
  -d '{"agentId":"my-agent", "task":"inventory-check",
       "readOnly":true, "allowedTables":["inventory","products"]}'
3

Agent queries

Pass the token to your agent. It connects with any PG client and writes SQL.

DATABASE_URL="postgresql://agent:eyJhbG...@127.0.0.1:15432/mydb" \
  opencode run "What sales trends do we have over the past 2 weeks?"

Database support

PostgreSQL

Full support. MD5, SCRAM-SHA-256, SSL/TLS. 14+ tested.

Available

ClickHouse

Via ClickHouse's PostgreSQL-compatible wire interface.

Available

MySQL

Wire protocol proxy with the same governance model.

Get notified

SQL Server

TDS protocol proxy for Microsoft SQL Server.

Get notified

Need a different database? Let us know