pg-context
Provides deep PostgreSQL context including live schemas as DDL, index health, foreign key associations, query execution plans, and performance statistics.
Detects TimescaleDB extensions and flags its capabilities within PostgreSQL.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@pg-context Explain the query plan for SELECT * FROM orders WHERE user_id = 42"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
pg-context
Model Context Protocol (MCP) server that gives AI coding assistants deep PostgreSQL context: live schemas as DDL, index health, foreign key associations, query execution plans, and performance statistics.
pg-context connects directly to your PostgreSQL database in read-only mode and exposes tools and resources for AI assistants such as Cursor, Claude Code, Cline, and Antigravity.
Why pg-context?
Generic SQL tools often execute arbitrary queries without giving the AI assistant structural context. This causes models to guess column names, hallucinate relations, and generate invalid joins.
pg-context resolves this problem by providing:
Zero-guess schema context: Exports real table structures formatted as PostgreSQL
CREATE TABLEDDL statements with foreign key comments and index definitions.Read-only security: Automatically sets
default_transaction_read_only = ONon every client connection and rejects mutating SQL statements (such asDROP,DELETE,UPDATE,INSERT,TRUNCATE,ALTER).Optimization insights: Detects unindexed foreign keys, high sequential scan ratios, unused indexes, and slow queries from
pg_stat_statements.Extension awareness: Identifies PostGIS, TimescaleDB, and pgvector extensions.
Related MCP server: PostgreSQL MCP Server
Quick Start
You can run pg-context directly via npx without manual installation:
npx -y pg-context-mcpOr install it globally:
npm install -g pg-context-mcp
pg-context-mcpConfiguration
pg-context reads connection parameters from environment variables or standard PostgreSQL connection strings:
Variable | Description | Default |
| Standard PostgreSQL connection URI |
|
| Database host |
|
| Database port |
|
| Database name |
|
| Database user |
|
| Database password |
|
| Enable SSL connection ( |
|
| Comma-separated list of target schemas |
|
| Connection pool size |
|
| Schema cache TTL in seconds |
|
| Default row limit for |
|
| Hard upper limit for |
|
Client Integration
1. Claude Desktop & Antigravity
Add the following entry to your claude_desktop_config.json or Antigravity MCP settings:
{
"mcpServers": {
"pg-context": {
"command": "npx",
"args": ["-y", "pg-context-mcp"],
"env": {
"DATABASE_URL": "postgresql://user:password@localhost:5432/my_database"
}
}
}
}2. Cursor
Add the following to your Cursor MCP configuration (.cursor/mcp.json or Cursor Settings > Features > MCP):
{
"mcpServers": {
"pg-context": {
"command": "npx",
"args": ["-y", "pg-context-mcp"],
"env": {
"DATABASE_URL": "postgresql://user:password@localhost:5432/my_database"
}
}
}
}3. Cline (VS Code Extension)
Add this configuration into cline_mcp_settings.json:
{
"mcpServers": {
"pg-context": {
"command": "npx",
"args": ["-y", "pg-context-mcp"],
"env": {
"DATABASE_URL": "postgresql://user:password@localhost:5432/my_database"
}
}
}
}Tools Reference
pg-context exposes 12 MCP tools:
Tool | Parameters | Description |
| none | Lists all non-system schemas in the database. |
|
| Lists tables and views with sizes, estimated row counts, and comments. |
|
| Returns full structural DDL of a table including columns, types, defaults, NOT NULL, foreign keys, and indexes. |
|
| Displays foreign key relationships as readable |
|
| Displays indexes, definitions, sizes, and scan counts. |
|
| Identifies unindexed foreign keys, sequential scan warnings, and unused indexes. |
|
| Executes read-only queries with enforced limits and returns Markdown tables. |
|
| Returns query execution plans from PostgreSQL |
| none | Returns PostgreSQL version, total database size, connection counts, and buffer settings. |
|
| Returns sequential vs index scans, live rows, dead rows, and maintenance dates. |
|
| Fetches top slowest queries recorded by |
| none | Lists installed PostgreSQL extensions and flags capabilities for PostGIS, TimescaleDB, and pgvector. |
Resources Reference
pg-context exposes 3 MCP resources:
Resource URI | MIME Type | Description |
|
| Full DDL dump of all tables across configured schemas. |
|
| DDL definition for an individual table. |
|
| Database health overview: storage sizes, dead rows, and optimization alerts. |
Development
# Clone the repository
git clone https://github.com/mewsyy/pg-context.git
cd pg-context
# Install dependencies
npm install
# Run test suite
npm test
# Build TypeScript
npm run build
# Start local server
npm startLicense
MIT (c) Semen
Available Tools
12 toolsdescribe_tableB
Get full structural details of a table as CREATE TABLE DDL, including columns, data types, constraints, foreign keys, and indexes
Inputs: table (required), schema.
explain_queryB
Get query execution plan via PostgreSQL EXPLAIN (or EXPLAIN ANALYZE) to debug performance bottlenecks
Inputs: sql (required), analyze.
get_database_infoA
Get high-level PostgreSQL database runtime information: version, size, active connection count, and server settings
Inputs: no named parameters.
get_slow_queriesB
Retrieve top slowest queries recorded by pg_stat_statements extension (if enabled in PostgreSQL)
Inputs: limit.
get_table_statsA
Get scan statistics, tuple counts, dead row accumulation, and maintenance history for tables
Inputs: table, schema.
list_extensionsA
List installed PostgreSQL extensions with version information and specialized framework detection (PostGIS, TimescaleDB, pgvector)
Inputs: no named parameters.
list_foreign_keysA
List foreign key relationships between tables to understand data model associations and joins
Inputs: table, schema.
list_indexesA
List indexes on tables with sizes and scan usage statistics to evaluate index health
Inputs: table, schema.
list_schemasA
List all available database schemas (excluding system schemas like pg_catalog and information_schema)
Inputs: no named parameters.
list_tablesA
List all tables and views in a given schema with estimated row counts, sizes, and comments
Inputs: schema.
read_queryA
Execute a safe read-only SQL query (SELECT, WITH, SHOW) with strict LIMIT enforcement and formatting
Inputs: sql (required), limit.
suggest_indexesB
Analyze query patterns, scan ratios, and missing foreign key indexes to provide index recommendations
Inputs: table, schema.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 12 tools
Each tool targets a distinct aspect of PostgreSQL database introspection: schemas, tables, relationships, indexes, query execution, performance stats, and extensions. There is no overlap in purpose.
All tools follow a consistent verb_noun pattern in snake_case (e.g., list_schemas, describe_table, read_query), making the API predictable and easy to navigate.
With 12 tools, the server covers the key areas of database analysis without being bloated. Each tool serves a distinct role, and the count is appropriate for the domain.
The set covers schema exploration, table details, indexing, query analysis, performance monitoring, and extension information. No obvious gaps for a read-only database context server.
Related MCP Connectors
Hosted PostgreSQL MCP server for Claude, ChatGPT and Cursor, with each action switched per account.
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Hosted MCP server for PostgreSQL diagnostics: slow queries, missing indexes, connection pressure.
Analytical memory for AI agents: a real Postgres queried in plain English over MCP. One command.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceA Model Context Protocol server that provides AI assistants with secure, read-only access to PostgreSQL databases while offering comprehensive tools for schema exploration, query validation, and performance optimization.MIT
- FlicenseNot gradedqualityDmaintenanceAn MCP server that enables AI assistants to interact with PostgreSQL databases by executing SQL queries and inspecting database schemas. It provides tools for standardized database exploration and management through the Model Context Protocol.-
- AlicenseAqualityCmaintenanceAn MCP server that gives AI agents direct read-only access to PostgreSQL databases, enabling natural language analytics through tools for schema exploration, querying, trend analysis, and data quality checks.115MIT
- AlicenseAqualityDmaintenanceA Model Context Protocol (MCP) server that gives AI assistants deep visibility into databases, inspecting schemas, detecting index problems, analyzing table bloat, and explaining query plans across PostgreSQL, MySQL, and SQLite.927 npm1MIT