A PostgreSQL Lab for AI Tests: Docker, pgAdmin, an MCP Server and VS Code

Copilot's answer after 25 read-only steps through the MCP server
DBA AI Lab · Lab setup

I Built a PostgreSQL Lab So Copilot Could Poke at It (Read-Only)

PostgreSQL 17, pgAdmin and an MCP server in Docker on a 16 GB laptop, and Copilot in VS Code asking one question. What I expected, what broke, and what I fixed.

3containers
16 GBRAM laptop
9MCP tools
0rows changed by the AI

Okay friends, today's job was simple on paper: a PostgreSQL box on my own laptop that I can break while I try AI tools on it. No cloud database, no shared dev server. Something that starts when I log in, that I can throw away and rebuild in a few minutes, and that an AI can read but never change.

By the end of the afternoon I had PostgreSQL 17 in Docker, pgAdmin next to it, an MCP server in front of it, and Copilot in VS Code answering a real question about the data. It did not go smoothly. VS Code turned out to be too new for the MCP server, and that alone ate most of my afternoon. I kept all of it in, because you'll probably hit some of the same things.

1. The test environment, command by command

Four pieces, all on localhost
Four pieces, all on localhost
Piece What I used Where it listens
PostgreSQL 17 pgvector image plus hypopg 127.0.0.1:5433
pgAdmin 4 desktop mode, server already registered 127.0.0.1:5050
Postgres MCP Pro MCP server, restricted mode, always on 127.0.0.1:8000
VS Code + Copilot agent mode, where I ask the questions through a small stdio bridge
Laptop 16 GB RAM, about 2 GB free, 4 cores Windows 11 + Docker Desktop

Only about 2 GB of RAM were free with everything else running, and Docker's VM takes 6 GB. So every container got a memory limit: 1 GB for Postgres, 512 MB for pgAdmin, 256 MB for the MCP server. For a lab with a few hundred thousand rows that's plenty.

The tools. On my laptop they were already installed, so I only checked versions with winget. On a fresh Windows machine it's this, in an admin PowerShell:

wsl --install                                        # Docker Desktop runs its Linux VM in WSL 2, reboot after
winget install -e --id Docker.DockerDesktop
winget install -e --id Microsoft.VisualStudioCode    # then sign in to GitHub Copilot inside VS Code
winget install -e --id Git.Git
winget install -e --id Python.Python.3.12
On my laptop the tools were already in, so the script only checks versions
On my laptop the tools were already in, so the script only checks versions

Passwords go outside the project folder. My first version had a git-ignored .env in the project, like everyone does. Then a test in my SQL Server lab showed me that Copilot in agent mode reads workspace files without asking. It read the .env and happily used the connection string in a command it suggested. So now a small script writes random passwords to %USERPROFILE%\.pglab, a folder only my user can read, and the compose file points there. I changed every password after the move. Git-ignored isn't the same as hidden from the AI.

The secrets script writes to my user profile, not to the repo
The secrets script writes to my user profile, not to the repo

One compose file. Postgres is built from the official pgvector image, so vector search is there for later, and I add hypopg from the PostgreSQL apt repository on top. hypopg lets the AI test an index without building it, and that turned out to be the best part of the whole demo. pg_stat_statements is preloaded, and every port binds to 127.0.0.1.

Postgres, pgAdmin and the MCP server in one compose file
Postgres, pgAdmin and the MCP server in one compose file

On first start Postgres runs two scripts. One builds a small library database: 5 branches, 20,000 books, 8,000 members and 400,000 loans. I left the indexes off the loans table on purpose, so the AI has something real to find. The other creates mcp_reader, the only account the AI ever gets:

CREATE ROLE mcp_reader LOGIN PASSWORD :'pw' CONNECTION LIMIT 5;
GRANT pg_read_all_data TO mcp_reader;   -- read every table, write nothing
GRANT pg_monitor TO mcp_reader;         -- pg_stat_* views
ALTER ROLE mcp_reader SET default_transaction_read_only = on;
ALTER ROLE mcp_reader SET statement_timeout = '30s';

Then two commands. The pull is about 1.5 GB, and the first start takes a minute because it loads the sample data:

docker compose pull --ignore-buildable
docker compose up -d --build --wait

pgAdmin is registered from a servers.json, so the only thing I typed there was the postgres password, once. The statistics tab for loans already told the story: 23 sequential scans, 4.4 million rows read, zero index scans.

Index scans: 0. The loans table has never used an index
Index scans: 0. The loans table has never used an index

The MCP server. I picked Postgres MCP Pro. It's open source (MIT), ships as a Docker image and gives the AI nine tools: schemas, objects, explain plans, top queries, index advice, health checks and execute SQL. In restricted mode every query runs in a read-only transaction. Before any model touched it I tested it with a tiny Python client: it listed the nine tools, ran a count, and refused a DELETE. Good, that's what I wanted.

VS Code. The final mcp.json only starts a bridge script (more on why in section 4). No password, no connection string in it:

{
  "servers": {
    "postgres-lab": {
      "type": "stdio",
      "command": "${workspaceFolder}/.venv-bridge/Scripts/python.exe",
      "args": ["${workspaceFolder}/tools/mcp_bridge.py"]
    }
  }
}

The bridge needs its own little Python environment with the 1.x MCP library. VS Code also opens a new folder in restricted mode, where MCP servers don't run, so I had to trust the folder. I wrote everything in it myself. Only do that for folders you know.

2. What we tested

One question, typed into Copilot Chat in agent mode:

Which branch in the postgres lab has the most overdue loans, and would an index help that query?

The point wasn't the number itself. I wanted to see if the AI would find the tools on its own, stay read-only, and say something honest about the index, on a table I had deliberately left without one.

Copilot asks before every tool call
Copilot asks before every tool call

It found the postgres-lab tools by itself, listed the schemas, then the tables in library, checked columns and keys, wrote the count query and ran it through execute_sql as mcp_reader. Then it asked for the plan and used hypopg to try an index that doesn't exist. Before every single tool call it stopped and asked me. I clicked allow each time.

3. What I expected vs. what actually happened

Step What I expected What actually happened
First docker compose up Container comes up with the sample data Data script died with integer out of range
Health check Healthy only when the data is loaded Said healthy while the init scripts were still running
VS Code envFile for the MCP server Connection string reaches the container "No database URL provided"
VS Code connects to the MCP server Normal handshake Server crashed on VS Code's server/discover request
DELETE through the MCP server Blocked Blocked
Copilot finds the tools Maybe needs a hint Found them by itself
The answer One branch on top Hilltop and Tech Park tied, 669 overdue loans each (2,676 of 400,000). I checked it myself, it's right
Would an index help? Some vague advice Index on (returned_on, due_on), plan from sequential scan to bitmap index scan, estimated cost 8,152 → 3,962
Database after the run Unchanged Unchanged. 25 steps, about a minute and a half

The part I liked most: it said clearly that the cost drop is a planner estimate, not a measured speed-up. That's exactly the sentence I'd want from a junior DBA too.

4. Where it went wrong, and how it could be better

The integer overflow was my own fault. My loan generator multiplied numbers past two billion. A few bigint casts fixed it.

The health check was sneakier. pg_isready talks to the Unix socket, and the temporary server that runs the init scripts listens on that socket too. So Docker said healthy while data was still loading. Checking over TCP (pg_isready -h 127.0.0.1) fixed it, because that temporary server doesn't listen on TCP.

VS Code was too new for the server. This is the one that ate my afternoon. First, VS Code's envFile setting never reached the container. Docker's own --env-file fixed that, with a separate file that holds only the connection string, not the admin password. Then the real problem: VS Code 1.148 opens with a newer MCP request, server/discover, and the older MCP library inside the current Postgres MCP Pro image crashed on it. VS Code didn't fall back to the server's SSE endpoint either, and the newer release on PyPI wouldn't install for me that day.

The MCP server's old library crashed on VS Code's newer handshake
The MCP server's old library crashed on VS Code's newer handshake

What works now: the MCP server runs all the time as its own container, on localhost only, and VS Code starts a small bridge script. The bridge answers server/discover with "method not found", VS Code falls back to the normal handshake, and everything else passes straight through. It adds no tools and no logic. It's a workaround though, not something I'd want to keep forever.

Security, so it doesn't go wrong later. Two locks, not one: the AI's role is read-only and the MCP server is in restricted mode too. Secrets live outside the workspace. Everything binds to localhost, which really matters here, because pgAdmin has no login in desktop mode and the MCP SSE endpoint has no authentication. And I didn't turn on auto-approve, so Copilot asks before each tool call.

What keeps this lab safe
What keeps this lab safe

Always on. Docker Desktop starts when I log in and all three containers have restart: unless-stopped, so the lab is just there. When I need the memory back, docker compose stop, and it stays stopped until docker compose start. docker compose down -v removes everything, data included.

5. What I'd take from this, and what would be worth testing later

My conclusion: getting an AI to talk to Postgres is easy now. Getting it to talk safely takes a bit more thought, and most of that is boring stuff: a read-only role, secrets out of the folder, localhost only, and approving tool calls yourself. The AI part itself worked better than I expected. It found the tools, stayed read-only, got the numbers right and didn't oversell the index.

A few things would be worth testing later, because this run left them open:

Idea Why it would be interesting
Actually create the suggested index and time the query To see how close the planner's 8,152 → 3,962 estimate is to a real measurement
Drop the bridge once a newer Postgres MCP Pro image ships To check if VS Code and the server talk directly without the workaround
Same question with a local model through the same MCP server To see if a private model can follow the same 25 steps
A heavier workload in pg_stat_statements To see if the AI picks the right query to fix without being told which one

Have you hooked an AI up to a database yet? I'm curious what kind of guardrails you put around it, or if you just gave it the admin account and hoped for the best. Tell me in the comments.

DBA AI Lab
AI for DBAs · next: tuning those missing indexes with the AI

Comments