Provides tools for querying PostgreSQL databases, inspecting database schemas, listing tables, and retrieving detailed table information with column definitions and constraints.
MCP Postgres Server
A Model Context Protocol server for querying PostgreSQL databases with tools for data retrieval and schema inspection.
Setup
Prerequisites: Python 3.11+ and PostgreSQL
Usage
Development
Run the following command to start the server in development mode and launch the MCP Inspector:
MCP Client Integration
You can also add the following to your MCP client configuration to use the server:
See mcp.json for an example of how to add the server in Cursor IDE.
Available Tools
query_database
- Execute SELECT queries (with SQL injection protection)get_table_schema
- Get table column information and constraintslist_tables
- List all database tablespostgres://schema
- Access complete database schema (resource)
Comparison with Official MCP Postgres Server
This implementation differs from the official @modelcontextprotocol/server-postgres in several key ways:
Feature | This Server (Python) | Official Server (TypeScript) |
---|---|---|
Safety | Guards against SQL injection | No protection |
Connection Pooling | ✅ Built-in (asyncpg) | ❌ Single connection |
Security
- Only
SELECT
statements allowed - Parameterized queries supported
- Environment-based configuration (no hardcoded credentials)
Next Steps
- Implement auth for postgres - currently I'm still relying on env variables which doesn't seem very secure
- Extend this to run on multiple databases and potentially multiple postgres instances as well
- Implement table / column pruning
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
A Model Context Protocol server that enables querying PostgreSQL databases with tools for executing SELECT queries, inspecting database schemas, and listing tables through natural language.
Related MCP Servers
- -securityFlicense-qualityA Model Context Protocol server providing both read and write access to PostgreSQL databases, enabling LLMs to query data, modify records, and manage database schemas.Last updated -4JavaScript
- -securityAlicense-qualityA Model Context Protocol server that enables powerful PostgreSQL database management capabilities including analysis, schema management, data migration, and monitoring through natural language interactions.Last updated -35546TypeScriptAGPL 3.0
- -securityFlicense-qualityA Model Context Protocol server that enables interaction with PostgreSQL databases to list tables, retrieve schemas, and execute read-only SQL queries.Last updated -16,948JavaScript
- -security-license-qualityA Model Context Protocol server that provides read-only access to PostgreSQL databases, enabling LLMs to inspect database schemas and execute read-only queries.Last updated -JavaScript