
MCP PostgreSQL Server
A server that connects to PostgreSQL databases and provides tools for safely exploring schemas, running read-only SQL queries, and performing data analysis with pre-built templates.
Tools
execute
Execute an INSERT, UPDATE, or DELETE query
list_tables
List all tables in the database
connect_db
Connect to PostgreSQL database. NOTE: Default connection exists - only use when requested or if other commands fail
query
Execute a SELECT query
describe_table
Get table structure
README
MCP PostgreSQL Server
A Model-Controller-Provider (MCP) server that:
- Connects to a PostgreSQL database
- Exposes table schemas as resources
- Provides tools for running read-only SQL queries
- Includes prompts for common data analysis tasks
Features
- Schema Exploration: Browse database schemas, tables, and columns
- Read-only Query Execution: Safely run SQL queries against your database
- Data Analysis Prompts: Pre-built SQL templates for common analysis tasks
- Data Visualization: Generate data for visualization
- Relationship Exploration: Visualize table relationships and foreign keys
- API Documentation: Auto-generated OpenAPI specification
Architecture
This application follows the Model-Controller-Provider (MCP) pattern:
- Model Layer: Direct interaction with the database
- Provider Layer: Business logic and data processing
- Controller Layer: API endpoints and request handling
Security Features
- Read-only query validation
- SQL injection protection
- Rate limiting
- Parameterized queries
- Authentication support
- CORS configuration
Installation
-
Clone the repository:
git clone <repository-url> cd mcp-postgres-server
-
Install dependencies:
npm install
-
Create a
.env
file based on the.env.template
:cp .env.template .env
-
Update the
.env
file with your PostgreSQL database credentials. -
Start the server:
npm start
Configuration
All configuration is managed through environment variables:
- Server: Port, environment, CORS settings
- Database: Connection details, pool settings
- Security: JWT settings, rate limiting
- Query: Execution limits, result size limits
API Endpoints
Schema Endpoints
GET /api/schemas
- List all schemasGET /api/schemas/:schema/tables
- List tables in a schemaGET /api/schemas/:schema/tables/:table
- Get table schema detailsGET /api/schemas/:schema/relationships
- Get table relationshipsGET /api/structure
- Get complete database structureGET /api/search?q=term
- Search tables and columns
Query Endpoints
POST /api/query
- Execute a SQL queryPOST /api/query/explain
- Get query execution planGET /api/schemas/:schema/tables/:table/sample
- Get sample dataGET /api/schemas/:schema/tables/:table/stats
- Get table statistics
Analysis Prompt Endpoints
GET /api/prompts
- List analysis prompt templatesGET /api/prompts/:templateId
- Get prompt template detailsPOST /api/prompts/:templateId/generate
- Generate SQL from templateGET /api/schemas/:schema/tables/:table/analysis/suggest
- Get analysis suggestions
Example Queries
Basic Table Query
// API request
fetch('/api/query', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
sql: 'SELECT * FROM users LIMIT 10'
})
})
.then(response => response.json())
.then(data => console.log(data));
Using Analysis Prompts
// Get suggested analysis for a table
fetch('/api/schemas/public/tables/orders/analysis/suggest')
.then(response => response.json())
.then(suggestions => {
// Use a suggestion
const suggestionId = suggestions.data[0].templateId;
const params = suggestions.data[0].params;
// Generate SQL from the template
return fetch(`/api/prompts/${suggestionId}/generate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ params })
});
})
.then(response => response.json())
.then(data => {
// Execute the generated SQL
return fetch('/api/query', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ sql: data.data.sql })
});
})
.then(response => response.json())
.then(results => console.log(results));
Development
- Run in development mode:
npm run dev
- Run tests:
npm test
- Lint code:
npm run lint
License
MIT
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.