MCP server for PostgreSQL database operations
Project description
PostgreSQL MCP Server
A Model Context Protocol (MCP) server for PostgreSQL database operations. This server provides AI assistants with the ability to perform CRUD operations and manage PostgreSQL databases through a standardized interface.
Features
- Entity CRUD Operations: Create, read, update, and delete entities in PostgreSQL tables
- Dynamic Table Support: Work with any table in your database without pre-configuration
- Secure Connection Management: Environment variable-based configuration with validation
- Parameterized Queries: Protection against SQL injection attacks
- Flexible Querying: Support for complex conditions and result limiting
Available Tools
CRUD Operations
create_entity: Insert new rows into tablesread_entity: Query tables with optional conditionsupdate_entity: Update existing rows based on conditionsdelete_entity: Remove rows from tables
Schema Operations
get_tables: Get list of all tables in the databaseget_table_schema: Get detailed schema information for a specific tableget_database_info: Get database metadata and version information
Available Resources
Database Resources
database://tables: List of all tables in the databasedatabase://info: Database metadata and version informationdatabase://schema/{table_name}: Schema information for specific tables
Quick Start
Prerequisites
- Python 3.10 or higher
- PostgreSQL database (version 12 or higher)
- uv package manager (latest version)
Installation
-
Configure your MCP client (e.g., Claude Desktop): Add the server configuration to your MCP client settings using
uvx:Claude Desktop Configuration Example:
{ "mcpServers": { "postgres-mcp": { "command": "uvx", "args": ["mcp-postgres-duwenji"], "env": { "POSTGRES_HOST": "localhost", "POSTGRES_PORT": "5432", "POSTGRES_DB": "your_database", "POSTGRES_USER": "your_username", "POSTGRES_PASSWORD": "your_password", "POSTGRES_SSL_MODE": "prefer" } } } }
Usage Examples
Once configured, you can use the MCP tools through your AI assistant:
Create a new user:
{
"table_name": "users",
"data": {
"name": "John Doe",
"email": "john@example.com",
"age": 30
}
}
Read users with conditions:
{
"table_name": "users",
"conditions": {
"age": 30
},
"limit": 10
}
Update user information:
{
"table_name": "users",
"conditions": {
"id": 1
},
"updates": {
"email": "newemail@example.com"
}
}
Delete users:
{
"table_name": "users",
"conditions": {
"id": 1
}
}
Development
Project Structure
mcp-postgres/
├── src/
│ └── mcp_postgres_duwenji/ # Main package
│ ├── __init__.py # Package initialization
│ ├── main.py # MCP server entry point
│ ├── config.py # Configuration management
│ ├── database.py # Database connection and operations
│ ├── resources.py # Resource management
│ └── tools/ # MCP tool definitions
│ ├── __init__.py
│ ├── crud_tools.py # CRUD operation tools
│ └── schema_tools.py # Schema operation tools
├── test/ # Testing related
│ ├── unit/ # Unit tests
│ ├── integration/ # Integration tests
│ ├── docker/ # Docker test environment
│ └── docs/ # Test documentation
├── docs/ # Project documentation
├── examples/ # Configuration examples
├── scripts/ # Utility scripts
├── memory-bank/ # Project memory bank
├── pyproject.toml # Project configuration and dependencies
├── uv.lock # uv dependency lock file
├── .env.example # Environment variables template
├── README.md # English README
└── README_ja.md # Japanese README
Running the Server
To run the server directly for testing:
uvx mcp-postgres-duwenji
Adding New Tools
- Create a new tool definition in
src/mcp_postgres_duwenji/tools/ - Add the tool handler function
- Register the tool in
get_crud_tools()andget_crud_handlers() - The tool will be automatically available through the MCP interface
Security Considerations
- Always use environment variables for sensitive connection information
- The server uses parameterized queries to prevent SQL injection
- Limit database user permissions to only necessary operations
- Consider using SSL/TLS for database connections in production
License
Apache 2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcp_postgres_duwenji-1.1.0.tar.gz.
File metadata
- Download URL: mcp_postgres_duwenji-1.1.0.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9dbd9ff84b0f94366d7e8a66bcdd9fc131126f0dfd2b734fd5180b5bfa61a4d
|
|
| MD5 |
d1c581964326ea3849211fe078939403
|
|
| BLAKE2b-256 |
55fda501f52bcfeb6c50e5be454a0b67b551218181e34f7c91a0374d0d7b758f
|
Provenance
The following attestation bundles were made for mcp_postgres_duwenji-1.1.0.tar.gz:
Publisher:
publish.yml on duwenji/mcp-postgres
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_postgres_duwenji-1.1.0.tar.gz -
Subject digest:
e9dbd9ff84b0f94366d7e8a66bcdd9fc131126f0dfd2b734fd5180b5bfa61a4d - Sigstore transparency entry: 622095276
- Sigstore integration time:
-
Permalink:
duwenji/mcp-postgres@c040c04b6d127da2b554a2ee67a0c7a2c4fa486c -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/duwenji
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c040c04b6d127da2b554a2ee67a0c7a2c4fa486c -
Trigger Event:
release
-
Statement type:
File details
Details for the file mcp_postgres_duwenji-1.1.0-py3-none-any.whl.
File metadata
- Download URL: mcp_postgres_duwenji-1.1.0-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
261e6e43025d5422a6802ff1e250d2afd766d256b4c191ab5558b4b955ed2828
|
|
| MD5 |
21935aa794d0c8ac654304962bc3c10d
|
|
| BLAKE2b-256 |
efb034ac2fed3ee9be10a3895a26392c486109b342d277694a1ba8c4864a53e9
|
Provenance
The following attestation bundles were made for mcp_postgres_duwenji-1.1.0-py3-none-any.whl:
Publisher:
publish.yml on duwenji/mcp-postgres
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_postgres_duwenji-1.1.0-py3-none-any.whl -
Subject digest:
261e6e43025d5422a6802ff1e250d2afd766d256b4c191ab5558b4b955ed2828 - Sigstore transparency entry: 622095277
- Sigstore integration time:
-
Permalink:
duwenji/mcp-postgres@c040c04b6d127da2b554a2ee67a0c7a2c4fa486c -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/duwenji
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c040c04b6d127da2b554a2ee67a0c7a2c4fa486c -
Trigger Event:
release
-
Statement type: