postgres-schema-mcp

Schema access for an agent is a retrieval problem, not a dump.
An MCP server that gives an AI agent the relevant slice of a wide Postgres schema instead of all of it, and enforces read-only in three layers rather than annotating it.
The number
Measured on the seeded AdventureWorks database in this repo: 68 tables across 5 schemas, over the 26 questions in evals/questions.yaml.
|
Tokens |
| Baseline: the full schema DDL, dumped in one blob |
10,927 |
| Retrieved slice, mean over the questions (k=8) |
1,816 |
|
6.0× less schema context (83% fewer tokens) |
Median slice 1,858, range 1,071–2,368. Tokenizer: tiktoken/cl100k_base. Ranking backend: bm25.
Reproduce: make evals prints the numbers as one JSON object. It needs no database: it reads the frozen schema in evals/fixtures/catalog.json, which is names, comments and keys only, no rows. CI re-measures on every push and fails if any figure in this README differs from the measurement, and checks the frozen schema against a freshly seeded database.
What the number leaves out: it counts schema context only, the DDL a model is given before it writes SQL. It excludes tool descriptions and the rows a query returns.
The ratio is the boring half of the argument. The interesting half is a schema too wide to dump at all. A dump-everything server truncates, and if the table you need falls outside the cutoff, the model has no way to ask for it. describe_table, below, is this server's answer.
Quickstart
git clone https://github.com/akshayyswami/postgres-schema-mcp && cd postgres-schema-mcp
docker compose up -d --wait
uv sync
You need Docker and uv. The first docker compose up builds Postgres, downloads the AdventureWorks OLTP sample, seeds 68 tables and creates both database roles. It takes a few minutes; afterwards it is instant.
Then point your MCP client at it:
{
"mcpServers": {
"postgres-schema-mcp": {
"command": "uv",
"args": ["run", "--directory", "/path/to/postgres-schema-mcp", "postgres-schema-mcp"],
"env": {
"PGSS_DSN": "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
}
}
}
}
The package is also on PyPI, so a client can run it without a clone. Set "command": "uvx" and "args": ["postgres-schema-mcp"], with the same env, and point PGSS_DSN at your own database through a role that can only SELECT, and set PGSS_SCHEMAS. Add it to your client and Your own database have the steps.
stdio by default. postgres-schema-mcp --http serves Streamable HTTP on port 3000 instead. The credentials above belong to a local throwaway container holding public sample data. There are no real credentials in this repo.
Add it to your client
The server is a local command that talks over stdio, so every client needs the same three things: the command uvx postgres-schema-mcp, the environment variable PGSS_DSN, and on your own database PGSS_SCHEMAS (see Your own database). Only Claude Code was run by me. The other snippets follow each client's own documentation as I read it on 2026-11-22, and each has a link, so check the page if a client has changed. Any other client that can start a stdio MCP server takes the same command, args and env.
The DSN below is the demo database from the Quickstart. Swap in your own.
Claude Code
claude mcp add postgres-schema-mcp \
-e PGSS_DSN=postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks \
-- uvx postgres-schema-mcp
The server is added for you in the current project only. Add --scope user to have it in every project. Do not use --scope project with a password in the DSN: it writes .mcp.json, which is meant to be committed. claude mcp list should show ✔ Connected. The first start downloads the package. One cold check I ran reported a 30 second timeout and the next one connected, so run it again if the first fails.
Claude Desktop
Open Settings, Developer, Edit Config. That opens claude_desktop_config.json: ~/Library/Application Support/Claude/ on macOS, %APPDATA%\Claude\ on Windows, ~/.config/Claude/ on Linux. Add the server, then quit and restart Claude Desktop (docs).
{
"mcpServers": {
"postgres-schema-mcp": {
"command": "uvx",
"args": ["postgres-schema-mcp"],
"env": {
"PGSS_DSN": "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
}
}
}
}
Codex (CLI, desktop app and IDE extension)
codex mcp add postgres-schema-mcp \
--env PGSS_DSN=postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks \
-- uvx postgres-schema-mcp
Or put it in ~/.codex/config.toml, or in .codex/config.toml for a trusted project (docs). Codex waits 10 seconds for a server to start by default, and a first uvx run can take longer, so the example raises it:
[mcp_servers.postgres-schema-mcp]
command = "uvx"
args = ["postgres-schema-mcp"]
startup_timeout_sec = 60
[mcp_servers.postgres-schema-mcp.env]
PGSS_DSN = "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
Check it with codex mcp list, or /mcp inside Codex.
Cursor
Put this in ~/.cursor/mcp.json for every project, or .cursor/mcp.json for one (docs):
{
"mcpServers": {
"postgres-schema-mcp": {
"type": "stdio",
"command": "uvx",
"args": ["postgres-schema-mcp"],
"env": {
"PGSS_DSN": "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
}
}
}
}
Cursor's docs let you write "${env:PGSS_DSN}" in env so the password stays out of the file. Server output is in the Output panel under "MCP Logs".
VS Code (GitHub Copilot)
Put this in .vscode/mcp.json in the workspace, or run "MCP: Add Server" from the Command Palette (docs). The top-level key here is servers, not mcpServers:
{
"servers": {
"postgres-schema-mcp": {
"type": "stdio",
"command": "uvx",
"args": ["postgres-schema-mcp"],
"env": {
"PGSS_DSN": "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
}
}
}
}
"MCP: List Servers" shows whether it connected. VS Code's docs also describe a newer Copilot location, .mcp.json at the workspace root with the mcpServers key, so try that if .vscode/mcp.json is not picked up.
Use it
Your own database
Give the server a role that can read and nothing else. It is the layer that holds, so do not skip it. Run this as a superuser, with your own names. Creating a role needs CREATEROLE or superuser, and only a superuser (or, from PostgreSQL 15, a role granted SET on the setting) can set temp_file_limit, so the database owner is not enough. Run it with psql -v ON_ERROR_STOP=1 so one failed line stops the rest instead of leaving a half-made role:
CREATE ROLE scout_ro LOGIN PASSWORD 'change-me' NOSUPERUSER NOCREATEDB NOCREATEROLE;
GRANT CONNECT ON DATABASE shop TO scout_ro;
GRANT USAGE ON SCHEMA public, billing TO scout_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public, billing TO scout_ro;
-- Tables that app_owner creates later are readable too:
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA public, billing GRANT SELECT ON TABLES TO scout_ro;
-- What one connection can cost:
ALTER ROLE scout_ro CONNECTION LIMIT 8;
ALTER ROLE scout_ro SET temp_file_limit = '256MB';
ALTER ROLE scout_ro SET work_mem = '16MB';
GRANT SELECT ON ALL TABLES lets the model read every row of every table in those schemas, and the rows go to the model provider (see Privacy). If a schema also holds password hashes, tokens or other secrets, grant SELECT on the tables you want to expose instead, one by one.
What this does not block: on a default Postgres any role can create temp tables and call pg_sleep, and before PostgreSQL 15 any role can create tables in the public schema. The AST guard refuses the first two. init/20-grants.sql shows how the demo database refuses them at the database too, but those revokes apply to PUBLIC, so they change what every role on that database can do. Use them only on a database that is dedicated to this.
Set PGSS_SCHEMAS to the schemas to index, comma separated, for example PGSS_SCHEMAS=public,billing. The default is the five AdventureWorks schemas, so on any other database the server finds no tables until you set it. It chooses what is indexed, not what the role can read.
The server reads the schema once at start-up, so restart it after a migration.
Each time
There is nothing to type. Ask the question in plain words and Claude uses the tools in the same loop every time: search_schema for the relevant tables, describe_table for one it needs by name, explain_query for anything large, then run_query. On the demo database, try:
- Which sales orders had the highest total due in 2013?
- What products were on each sales order, with quantities and unit prices?
- Which employees have had their pay rate changed, and when?
Your client may ask you to approve the tools the first time. Ask it to change data and run_query refuses with a message saying the server is read-only.
Configuration
Every setting is an environment variable. The audit and a test in this repo fail if one the code reads is missing from this table.
| Variable |
Default |
What it changes |
PGSS_DSN |
the demo database, postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks |
The database to connect to. Use a role that can only SELECT |
PGSS_SCHEMAS |
person,humanresources,production,purchasing,sales |
The schemas to index. It chooses what is indexed, not what the role can read |
PGSS_STATEMENT_TIMEOUT_MS |
10000 |
Cancels a query that runs longer than this |
PGSS_IDLE_TX_TIMEOUT_MS |
10000 |
Ends a session left idle inside a transaction |
PGSS_MAX_ROWS_CAP |
1000 |
The most rows run_query returns, whatever max_rows asks for |
PGSS_MAX_CELL_CHARS |
2000 |
A longer text value is cut, with a note of how much was dropped |
PGSS_MAX_RESULT_BYTES |
200000 |
Cap on the JSON size of all rows from one query |
PGSS_POOL_MIN |
1 |
Connections kept open |
PGSS_POOL_MAX |
4 |
The most connections open at once |
PGSS_TRANSPORT |
stdio |
stdio, or streamable-http (the same as the --http flag) |
PGSS_HOST |
127.0.0.1 |
The address the HTTP transport binds. A non-loopback address is refused unless PGSS_ALLOW_REMOTE=1 |
PGSS_PORT_HTTP |
3000 |
The port for the HTTP transport |
PGSS_ALLOW_REMOTE |
unset |
Set to 1 to allow a non-loopback bind, only behind a proxy that does the authentication |
PGSS_RETRIEVAL_BACKEND |
bm25 |
bm25, or titan (needs the bedrock extra) |
PGSS_TITAN_MODEL_ID |
amazon.titan-embed-text-v2:0 |
The embedding model when the backend is titan |
AWS_REGION |
us-east-1 |
The Bedrock region when the backend is titan |
Troubleshooting