A Docker-based Model Context Protocol (MCP) server that provides comprehensive PostgreSQL database interaction capabilities. This server enables AI assistants and other MCP clients to query, inspect, and manage PostgreSQL databases through a standardized interface.
- Execute raw SQL queries with parameter binding
- Run multiple queries in transactions
- Get query execution plans with EXPLAIN/EXPLAIN ANALYZE
- List databases, schemas, tables, views, and functions
- Describe table structure with columns, types, and constraints
- View indexes and their definitions
- Get table size and row count statistics
- Insert single or multiple rows
- Update and delete with WHERE clauses
- Bulk insert from JSON arrays
- Export table data as JSON
- View database statistics and connection info
- List and terminate active connections
- Run VACUUM and ANALYZE operations
- Check for table bloat
- Docker and Docker Compose
- An MCP client (e.g., Claude Desktop, VS Code with MCP extension)
-
Clone the repository
git clone <repository-url> cd postgresql-mcp-server
-
Configure environment variables
cp .env.example .env # Edit .env with your preferred settings -
Start the services
docker-compose up -d
-
Verify the server is running
docker-compose logs mcp-server
The server is configured via environment variables. See .env.example for all available options:
| Variable | Description | Default |
|---|---|---|
POSTGRES_HOST |
PostgreSQL host | postgres |
POSTGRES_PORT |
PostgreSQL port | 5432 |
POSTGRES_DB |
Database name | postgres |
POSTGRES_USER |
Database user | postgres |
POSTGRES_PASSWORD |
Database password | postgres |
POSTGRES_MAX_CONNECTIONS |
Max connection pool size | 20 |
Add to your Claude Desktop configuration (~/Library/Application Support/Claude/claude_desktop_config.json on macOS):
{
"mcpServers": {
"postgresql": {
"command": "docker",
"args": ["exec", "-i", "postgresql-mcp-server", "node", "dist/index.js"]
}
}
}Add to your VS Code MCP settings:
{
"mcp.servers": {
"postgresql": {
"command": "docker",
"args": ["exec", "-i", "postgresql-mcp-server", "node", "dist/index.js"]
}
}
}The server provides 20+ commands across 4 categories. See COMMANDS.md for detailed documentation.
execute_query- Execute SQL queries with parameter bindingexecute_transaction- Run multiple queries in a transactionexplain_query- Get query execution plans
list_databases- List all databaseslist_schemas- List schemas in current databaselist_tables- List tables in a schemadescribe_table- Get table structure and constraintslist_indexes- List indexes for a tablelist_views- List views in a schemalist_functions- List stored functions/proceduresget_table_size- Get table size and row count
insert_data- Insert a single rowupdate_data- Update rows with WHERE clausedelete_data- Delete rows with WHERE clausebulk_insert- Insert multiple rows from JSONexport_table- Export table data as JSON
get_database_stats- Get database statisticslist_active_connections- Show active connectionsterminate_connection- Terminate a connection by PIDvacuum_table- Run VACUUM on a tableanalyze_table- Run ANALYZE on a tablecheck_table_bloat- Check for table bloat
# Install dependencies
npm install
# Build TypeScript
npm run build
# Run locally (requires PostgreSQL)
npm startpostgresql-mcp-server/
βββ src/
β βββ index.ts # Main server entry point
β βββ database.ts # Database connection manager
β βββ tools/
β βββ query.ts # Query execution tools
β βββ schema.ts # Schema inspection tools
β βββ data.ts # Data manipulation tools
β βββ admin.ts # Administrative tools
βββ Dockerfile # Multi-stage Docker build
βββ docker-compose.yml # Docker Compose configuration
βββ package.json # Node.js dependencies
# Start services
docker-compose up -d
# Stop services
docker-compose down
# View logs
docker-compose logs -f mcp-server
# Rebuild after code changes
docker-compose up -d --build
# Access PostgreSQL directly
docker-compose exec postgres psql -U postgres- Credentials: Never commit
.envfiles with real credentials - Network: The MCP server and PostgreSQL communicate over a private Docker network
- Permissions: The server runs as a non-root user in the container
- SQL Injection: Always use parameterized queries (the tools support parameter binding)
- Check that PostgreSQL is healthy:
docker-compose ps - Verify environment variables in
.env - Check logs:
docker-compose logs postgres
- Ensure the container is running:
docker ps - Verify the command in your MCP client configuration
- Check server logs:
docker-compose logs mcp-server
- Ensure your database user has appropriate permissions
- Check PostgreSQL logs for authentication errors
Contributions are welcome! Please feel free to submit a Pull Request.
If you find this MSSQL MCP Server useful for your projects, please consider supporting its development!
This project is maintained by independent developers. Your support helps us:
- π Add new features and improvements
- π Fix bugs and improve stability
- π Create better documentation and tutorials
- π Enhance security features
- π― Provide faster support and updates
Patrons get access to:
- π― Priority Support - Get help faster when you need it
- π Early Access - Try new features before public release
- π¬ Direct Communication - Influence the project roadmap
- π Exclusive Tutorials - Advanced usage guides and examples
- π Recognition - Your name in our SUPPORTERS.md file
- β Star this repository on GitHub
- π Report bugs and request features via GitHub Issues
- π Improve documentation by submitting pull requests
- π¬ Spread the word - Share with colleagues and on social media
This project is licensed under the MIT License - see the LICENSE file for details.