GateSQL

The agent database gateway.

GateSQL sits between your AI agents and PostgreSQL. Short-lived sessions, read-only enforcement, table allowlists, query budgets. Every query governed, every access explained.

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

Agents are great at SQL. The permission model isn't.

Credential exfiltration

Long-lived database passwords can be leaked via prompt injection. One compromised agent exposes everything.

Uncontrolled load

Agents run queries in loops, retry failures, and scan entire tables. No built-in way to cap the damage.

Coarse access control

PostgreSQL roles are static and hard to manage programmatically. You can't spin up per-agent permissions on the fly.

No intent visibility

Queries hit the database but nobody knows why. When something goes wrong, there's no audit trail of intent.

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