Skip to main content
Glama
mcp-postgres-demo

mcp-postgres-demo

by apex36tech
README.md
# MCP server for PostgreSQL, read only

**Demo on sample data.** An open-source Model Context Protocol (MCP) server that lets an AI assistant query a PostgreSQL database in plain English, without being able to change it.

## What it is

An MCP server is a small program that lets an AI assistant such as Claude use your systems through tools you define. This one connects Claude Desktop, Claude Code, Cursor and other MCP clients to a sample PostgreSQL database, read only. Every query passes four separate safety layers before and while it runs.

**The story:** ask Claude "Which 5 film categories earned the most rental revenue in the first quarter of 2026?" Claude reads the schema through this server, writes one read-only SQL query, runs it, and answers with the numbers and the query it used.

## Quick start

Needs Docker and Node 22.

```bash
corepack enable && pnpm install && pnpm build
pnpm db:up      # Postgres 18 with the demo roles, on localhost:5433
pnpm db:seed    # downloads Pagila 4.1.2, checks it, loads it
```

Then connect your assistant: [docs/claude-desktop.md](docs/claude-desktop.md) has the steps for Claude Code, Claude Desktop and Cursor. Questions to try are in [docs/demo-script.md](docs/demo-script.md).

## The hosted endpoint

`https://mcp-postgres-demo.vercel.app/mcp`: read only, no sign-in, 30 requests a minute per visitor (over the limit, the server answers HTTP 429 for the rest of that minute). `GET /healthz` wakes it up after a quiet period. Only the clients tested against it are listed in `docs/claude-desktop.md`.

## How it keeps your database safe

1. **The database login can only read.** The server connects as a role that can read the sample tables and nothing else. Its transactions are read only by default, it has a 5 second time limit, and it cannot see any other schema. Even if every check in the code were skipped, this login could not change data.
2. **Every query is read by Postgres's own parser first.** The server uses the real Postgres parser (`libpg-query`) to turn the SQL into a tree and walks all of it. It refuses anything other than one plain SELECT: writes hidden in a `WITH`, a second statement after a semicolon or a comment, `SELECT INTO`, row locks, system tables, other schemas, and functions that can sleep, read files or change settings. The checked tree is turned back into SQL, so what runs is exactly what was checked.
3. **Limits on every run.** Each query runs in a read-only transaction with a 5 second timeout and returns at most 200 rows (it says when a result was cut). Responses are capped at 1 MB, and very long cells are shortened.
4. **An audit line for every query.** One JSON line per query records the time, the tool, a fingerprint (sha256) of the SQL, the row count, the duration and the outcome. It never records the results or the visitor's IP address.

`test/db-role.test.ts` checks layer 1 on its own: each write the parser refuses also fails when sent straight to the database as the read-only role. One exception: `COPY film TO STDOUT` is a read that Postgres allows for any role that can SELECT the table, so only layer 2 refuses it. It returns nothing a SELECT would not.

## Tools

| Tool | What it does |
|---|---|
| `list_tables` | Tables and views you can query, with approximate row counts and a one-line description |
| `describe_table` | Columns, types, primary and foreign keys, and up to 5 sample rows |
| `run_query` | Runs one SELECT and returns the rows as JSON, the SQL that ran, and whether the result was cut |
| `explain_query` | The query plan for a SELECT (EXPLAIN, FORMAT JSON). Never ANALYZE |

Also a resource, `schema://overview` (tables, columns and keys, read by the assistant first), and a prompt, `analyse-table`.

## Configuration

| Variable | Default | Meaning |
|---|---|---|
| `DATABASE_URL` | none | Connection string for a **read-only** role |
| `ALLOWED_SCHEMAS` | `pagila` | Schemas a query may use (comma-separated) |
| `MAX_ROWS` | `200` | Most rows returned per query |
| `QUERY_TIMEOUT_MS` | `5000` | Time limit per query |
| `MAX_SQL_CHARS` | `5000` | Longest query accepted |
| `PORT` | `8080` | HTTP port (hosted mode) |
| `RATE_LIMIT_PER_MIN` | `30` | Requests a minute per visitor (hosted mode) |
| `DAILY_LIMIT` | `5000` | Requests a day per instance (hosted mode) |
| `LABS_IP_SALT` | none | Salt for hashing visitor IPs (hosted mode) |
| `POOL_MAX` | `5` | Database connections per server copy (2 on Vercel) |

## What it does not do

- It never writes, deletes or changes data.
- No sign-in on this demo. OAuth is available for client builds.
- No multi-tenant hosting: one server, one database.

## Sample data

[Pagila](https://github.com/devrimgunduz/pagila) 4.1.2 (commit `6ea53f812254c91a602f61c0ef62beca3fa676b1`), a fictional DVD rental shop ported from MySQL's Sakila sample, under the PostgreSQL License (Copyright (c) 2015-2026, Devrim Gündüz). `db/seed/fetch-pagila.sh` downloads it at that commit, checks its sha256, and moves it from the `public` schema into a `pagila` schema with a non-superuser owner. The load drops the `film_embedding` table (sample vectors this demo does not use) and blocks Pagila's own functions. No data is stored in this repo.

The `staff` table holds sample password hashes and the `customer` table sample emails. Both are part of the public sample, not real people.

Database size after loading: 36 MB (measured on local Docker, 4 Dec 2026).

## Licence

MIT. Built by [Apex36](https://www.apex36tech.com/labs/mcp-server), a founder-led team.