Folio Rancher PostgreSQL MCP How-To
Overview
PostgreSQL MCP is a Model Context Protocol server that connects your AI assistant (Claude, GitHub Copilot, Cursor, etc.) directly to the PostgreSQL instance running inside a Folio Rancher environment. It gives your AI a full DBA toolkit — you can query data, inspect schemas, analyze query plans, find missing indexes, check locks, manage roles, and much more — all from a natural-language chat, without leaving your IDE.
The server is deployed as a Kubernetes workload inside every Rancher project that has pg_mcp_server = true in its Terraform configuration. It is protected by a Bearer token stored in the db-credentials Kubernetes secret of that project.
Architecture
Your IDE / AI Client
│
│ HTTPS (Authorization: Bearer <token>)
▼
ALB (shared group)
│
▼
Ingress → pg-mcp Service → pg-mcp Pod
│
│ asyncpg
▼
PostgreSQL (embedded Bitnami
or AWS RDS Aurora)Endpoint pattern:
https://<cluster-name>-<project-name>-pg-mcp.ci.folio.org/sseExamples: - https://folio-snapshot-pg-mcp.ci.folio.org/sse - https://folio-testing-pg-mcp.ci.folio.org/sse
Prerequisites
Access to the Rancher UI for the target environment or
kubectlconfigured for that cluster/namespaceVS Code with GitHub Copilot, Claude Desktop, Cursor, or any other MCP-compatible AI client
The
DB_MCP_TOKENfor the target environment (see Step 1 below)
Step 1: Retrieve the Bearer Token
The token is generated by Terraform and stored in the db-credentials Kubernetes secret of the target project namespace. There are two ways to retrieve it.
Option A — Rancher UI
Log in to https://rancher.ci.folio.org
Navigate to your cluster → Projects/Namespaces → select the project (e.g.
snapshot)In the left sidebar go to Storage → Secrets
Find the secret named
db-credentialsClick it, then click the eye icon next to
DB_MCP_TOKENto reveal the valueCopy the raw value — this is your Bearer token
Option B — kubectl
Run the following command, replacing <namespace> with your project name (e.g. snapshot, testing):
kubectl get secret db-credentials -n <namespace> \
-o jsonpath='{.data.DB_MCP_TOKEN}' | base64 -dThe decoded string (32 alphanumeric characters) is your Bearer token. Store it somewhere safe — treat it like a password.
Step 2: Configure Your MCP Client
VS Code (GitHub Copilot / Copilot Chat)
Open VS Code settings with Cmd+Shift+P → Open User Settings JSON and add the following block, replacing the placeholders:
{
"mcp": {
"servers": {
"pg-mcp-snapshot": {
"type": "sse",
"url": "https://<cluster-name>-<project-name>-pg-mcp.ci.folio.org/sse",
"headers": {
"Authorization": "Bearer <your_token>"
}
}
}
}
}You can add multiple servers for different environments:
{
"mcp": {
"servers": {
"pg-mcp-snapshot": {
"type": "sse",
"url": "https://folio-snapshot-pg-mcp.ci.folio.org/sse",
"headers": { "Authorization": "Bearer <snapshot_token>" }
},
"pg-mcp-testing": {
"type": "sse",
"url": "https://folio-testing-pg-mcp.ci.folio.org/sse",
"headers": { "Authorization": "Bearer <testing_token>" }
}
}
}
}Claude Desktop
Edit ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"pg-mcp-snapshot": {
"type": "sse",
"url": "https://folio-snapshot-pg-mcp.ci.folio.org/sse",
"headers": {
"Authorization": "Bearer <your_token>"
}
}
}
}Restart Claude Desktop after saving.
Cursor
In Cursor open Settings → MCP and add a new server entry with the same url and headers as above.
Step 3: Start Using It
Once configured, open your AI chat and start talking to your database naturally. The AI will automatically choose and call the right tool based on your request.
Example prompts to get started:
"List all databases in this PostgreSQL cluster"
"Show me all tables in the
publicschema offolio_modules""Describe the
loantable — columns, indexes, constraints""Explain this query: SELECT * FROM loan WHERE status = 'Open'"
"Are there any long-running queries right now?"
"Find tables with missing indexes"
"What is the buffer cache hit ratio?"
"Show me all blocked sessions and what's blocking them"
"Vacuum analyze the
itemtable""How big are all my databases?"
Available Tools
Tool | Category | What it does | Example prompt |
|---|---|---|---|
| SQL | Execute any SQL — SELECT by default; pass | "Insert a test row into the config table" |
| SQL | EXPLAIN or EXPLAIN ANALYZE with optional buffer stats and JSON output | "Explain analyze this query: SELECT * FROM holdings WHERE permanent_location_id = '...'" |
| Schema | List all databases in the cluster | "What databases exist?" |
| Schema | List schemas in a database | "List schemas in folio_modules" |
| Schema | List tables, views, and sequences in a schema | "Show all tables in the public schema" |
| Schema | Full table description: columns, types, nullability, defaults, constraints, indexes | "Describe the loan table" |
| Stats | pg_stat_user_tables stats — seq scans, row counts, dead tuples, last vacuum | "Show stats for the item table" |
| Stats | pg_stat_user_indexes — index scans and usage per index | "Show index usage for the public schema" |
| Monitoring | Currently held locks joined with blocking/waiting sessions | "Are there any lock conflicts right now?" |
| Monitoring | Queries running longer than N seconds (default 30s) | "List all queries running more than 60 seconds" |
| Monitoring | Connection counts grouped by state and user | "How many connections are open?" |
| Monitoring | Terminate a backend by PID ( | "Kill PID 12345" |
| Maintenance | VACUUM, VACUUM ANALYZE, or VACUUM FULL on a table | "Vacuum analyze the holdings table" |
| Maintenance | ANALYZE a specific table or entire database | "Analyze all tables in folio_modules" |
| Maintenance | REINDEX TABLE | "Reindex the loan table" |
| Health | Estimate dead-tuple bloat using pg_stat_user_tables | "Which tables have the most bloat?" |
| Health | Suggest missing indexes based on sequential scan statistics | "Find tables that need indexes" |
| Health | List indexes that have zero or very few scans | "Show me indexes that are never used" |
| Health | Buffer cache hit ratio for tables and indexes | "What is the cache hit ratio?" |
| Health | pg_stat_bgwriter checkpoint and WAL write statistics | "Show checkpoint and WAL stats" |
| Health | Size of every database in the cluster | "How big are all the databases?" |
| Health | Table + TOAST + index size per table in a schema | "What are the largest tables in public schema?" |
| Replication | pg_stat_replication and pg_replication_slots | "Show replication lag" |
| Roles | List all PostgreSQL roles and users | "List all database users" |
| Roles | Create a new role with optional password, LOGIN, SUPERUSER, CREATEDB | "Create a read-only user called analytics" |
| Roles | Drop a role | "Drop the role old_service" |
| Databases | Create a new database with optional owner | "Create a database called feature_test owned by postgres" |
| Databases | Drop a database; optionally force-terminate connections first | "Drop the database scratch_test" |
| Databases | List installed extensions; optionally also list available ones | "What extensions are installed? Any useful ones available?" |
Authentication Notes
The server uses Bearer token authentication — every request (except
/health) must carry the headerAuthorization: Bearer <token>.The token is a 32-character alphanumeric string generated by Terraform (
random_password) and stored in thedb-credentialsKubernetes secret. It never leaves the cluster except when you explicitly retrieve it.The token is environment-specific — each Rancher project has its own independent token. A token from
snapshotwill not work againsttesting.The comparison is performed with
secrets.compare_digest— it is timing-safe and not vulnerable to timing attacks.The server connects to PostgreSQL using the
postgressuperuser credentials fromdb-credentials. All operations run with full DBA privileges — use write operations carefully in shared environments./healthis exempt from auth so Kubernetes liveness/readiness probes work without a token.
Troubleshooting
Problem | Solution |
|---|---|
| The |
| The token is wrong. Re-fetch it from the |
SSE connection drops immediately | The server pod may be restarting. Check pod status: |
Tool returns a database error | The target database or schema may not exist. Use |
| DML/DDL requires |
| VACUUM FULL holds an exclusive lock and rewrites the table. Only run during a maintenance window on live environments. |
Pod not deployed in a project | The project may have |
Cannot find the secret in Rancher UI | Ensure you are in the correct project → namespace. The secret is named |