Supports containerized deployment through Docker with pre-built images available on Docker Hub, simplifying installation and environment setup.
Built on the FastAPI framework for creating the MCP server API endpoints that interface with PostgreSQL databases.
Provides a simple interface to interact with PostgreSQL databases, allowing SQL query execution, database operations (INSERT/UPDATE/DELETE), database creation, table schema management, and connection debugging.
Supports testing through pytest for validating server functionality and database interactions.
MCP Server for PostgreSQL
A Model Context Protocol (MCP) server implementation for PostgreSQL, providing a simple interface to interact with PostgreSQL databases through MCP.
Features
- Execute SQL queries with parameterized inputs
- Run INSERT/UPDATE/DELETE operations
- Create new databases
- Create or update table schemas
- Debug PostgreSQL connections
- Containerized with Docker for easy deployment
- Environment-based configuration
Prerequisites
- Python 3.8+
- PostgreSQL 10+
- Docker (optional, for containerized deployment)
- Docker Compose (optional, for development)
Installation
Using Docker (Recommended)
- Clone the repository:
- Copy the example environment file:
- Update the
.env
file with your PostgreSQL credentials: - Start the service using Docker Compose:
Manual Installation
- Clone the repository:
- Create and activate a virtual environment:
- Install dependencies:
- Copy the example environment file and update it:
- Run the server:
Usage
The MCP server provides the following endpoints:
sql_query
Run a SELECT query and return results as JSON.
Parameters:
query
: SQL query stringparams
: Optional JSON string of query parameters (list or dict)
Example:
sql_execute
Execute an INSERT/UPDATE/DELETE statement.
Parameters:
query
: SQL statementparams
: Optional JSON string of query parameters (list or dict)
Example:
create_database
Create a new PostgreSQL database.
Parameters:
database_name
: Name of the database to create
create_or_update_table
Create or update a table schema.
Parameters:
sql
: CREATE TABLE or ALTER TABLE SQL statement
debug_postgres_connection
Debug the PostgreSQL connection.
Environment Variables
Variable | Default | Description |
---|---|---|
PG_HOST | localhost | PostgreSQL host |
PG_PORT | 5432 | PostgreSQL port |
PG_USER | postgres | PostgreSQL username |
PG_PASSWORD | PostgreSQL password | |
PG_DATABASE | postgres | Default database name |
HOST | 0.0.0.0 | Host to bind the MCP server to |
PORT | 8056 | Port to run the MCP server on |
Development
Running Tests
Building the Docker Image
API Documentation
For detailed API documentation, refer to the OpenAPI specification.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
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 implementation that provides a simple interface to interact with PostgreSQL databases, enabling SQL queries, database operations, and schema management through MCP.
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 -1417JavaScript
- AsecurityAlicenseAqualityA 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 -1843889TypeScriptAGPL 3.0
- -securityFlicense-qualityA Model Context Protocol server that enables performing PostgreSQL database operations (create, read, update, delete) on User and Post entities through MCP tools.Last updated -TypeScript
- -securityAlicense-qualityA Model Context Protocol server that enables interaction with PostgreSQL databases to list tables, retrieve schemas, and execute read-only SQL queries.Last updated -35JavaScriptMIT License