PostgreSQL MCP Server

by assadnasser

155 downloads Not rated yet

About

# PostgreSQL MCP Server A Model Context Protocol (MCP) server that allows users to connect to PostgreSQL databases. This server exposes resources, tools, and prompts for interacting with PostgreSQL databases through the MCP protocol. ## Overview This MCP server allows external applications to share database…

Explore

- Database Connections
- Secure PostgreSQL connections with connection pooling
- SSL support for encrypted connections
- Connection validation

- Query Execution
- SQL query execution with parameter binding
- Query history tracking
- Statistics collection

- Schema Management
- Table listing and description
- Column metadata with types, constraints, and descriptions
- Primary key, foreign key, and index information

- Database Analytics
- Database size information
- Current database activity monitoring
- Query performance tracking

- Security
- API rate limiting
- CORS support and secure headers

- Server Features
- Graceful shutdown handling
- Cross-origin resource sharing (CORS) support


npm install

npm install --save-dev @types/uuid @types/redis

The server is fully compatible with Vercel deployment:


npm install -g vercel

vercel

vercel --prod

| Variable | Description | Default |
|----------|-------------|---------|
| PORT | Server port | 3000 |
| NODE_ENV | Environment mode | development |
| REDIS_URL | Redis connection URL | none (uses in-memory storage) |
| PGHOST | PostgreSQL host | localhost |
| PGUSER | PostgreSQL user | postgres |
| PGDATABASE | PostgreSQL database | postgres |
| PGPASSWORD | PostgreSQL password | password |
| PGPORT | PostgreSQL port | 5432 |

npm install -g @modelcontextprotocol/inspector

1. test-connection

Tests a connection to a PostgreSQL database.

Parameters:
- host: string
- port: number
- database: string
- user: string
- password: string
- ssl: boolean (optional)

2. query

Executes a SQL query on a PostgreSQL database.

Parameters:
- host: string
- port: number
- database: string
- user: string
- password: string
- ssl: boolean (optional)
- query: string
- params: any[] (optional)

3. list-tables

Lists tables in a PostgreSQL database.

Parameters:
- host: string
- port: number
- database: string
- user: string
- password: string
- ssl: boolean (optional)

4. describe-table

Describes the schema of a table in a PostgreSQL database.

Parameters:
- host: string
- port: number
- database: string
- user: string
- password: string
- ssl: boolean (optional)
- tableName: string
- schemaName: string (optional)

5. get-database-stats

Gets statistics about a PostgreSQL database.

Parameters:
- host: string
- port: number
- database: string
- user: string
- password: string
- ssl: boolean (optional)

6. get-query-history

Gets the history of executed queries.

Parameters:
- host: string
- port: number
- database: string
- user: string
- password: string
- ssl: boolean (optional)
- limit: number (optional)

7. clear-query-history

Clears the query history for a database connection.

Parameters:
- host: string
- port: number
- database: string
- user: string
- password: string
- ssl: boolean (optional)

A Model Context Protocol (MCP) server that allows users to connect to PostgreSQL databases. This server exposes resources, tools, and prompts for interacting with PostgreSQL databases through the MCP protocol.

Overview

This MCP server allows external applications to share database credentials with the server via POST requests to an SSE (Server-Sent Events) endpoint. The server then establishes a connection to the specified PostgreSQL database and provides various ways to interact with it.

Architecture

The server follows a modular architecture with clear separation of concerns:

- Server Core - Handles MCP protocol implementation and coordination
- Resources - Provide data and metadata about database entities
- Tools - Implement database operations and actions
- Prompts - Define natural language interface templates

The code is organized into these main directories:

src/
├── resources/   # Database schema, table structure, query history resources
├── tools/       # Database operations (queries, connections, table management)
├── prompts/     # Natural language templates for database operations
├── server.ts    # Main MCP server implementation and configuration
├── database.ts  # Database connection and query handling
└── query-history.ts # Query history tracking and storage

Features

- Database Connections
- Secure PostgreSQL connections with connection pooling
- SSL support for encrypted connections
- Connection validation

- Query Execution
- SQL query execution with parameter binding
- Query history tracking
- Statistics collection

- Schema Management
- Table listing and description
- Column metadata with types, constraints, and descriptions
- Primary key, foreign key, and index information

- Database Analytics
- Database size information
- Current database activity monitoring
- Query performance tracking

- Security
- API rate limiting
- CORS support and secure headers

- Server Features
- Graceful shutdown handling
- Cross-origin resource sharing (CORS) support

Installation

```bash

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.