A simple Model Context Protocol (MCP) Server implementation for PostgreSQL database access. This is a pet project that demonstrates how to connect AI assistants to PostgreSQL databases through the MCP specification.
This was build specifically to connect to the Claude Desktop. If you are on Mac, you can edit the
.../Library/Application Support/Claude/claude-desktop.json file and add the following:
{
"mcpServers": {
"pg-rs": {
"command": "/path/to/postgresql-mcp/target/release/postgresql-mcp",
"args": [
"postgres://postgres:postgres@localhost:5432/your_database_name"
],
"env": {
"RUST_LOG": "debug"
}
}
}
}Then you can use the query tool in Claude to execute SQL queries against your PostgreSQL database.
⚠️ Warning: This is an experimental project and should NOT be used in production environments.
- Basic PostgreSQL connectivity through standard connection strings
- Simple query execution with JSON result formatting
- MCP v2024-11-05 implementation using stdin/stdout communication
- Async operations with Tokio
- Rust 1.70+ (uses Rust 2024 edition)
- PostgreSQL Database (local or remote)
- Access credentials for your PostgreSQL instance
-
Clone the repository:
git clone <repository-url> cd postgresql-mcp
-
Build the project:
cargo build --release
-
The binary will be available at:
target/release/postgresql-mcp
Run the MCP server with a PostgreSQL connection string:
./target/release/postgresql-mcp "postgres://username:password@localhost:5432/database_name"# Local PostgreSQL with password
./postgresql-mcp "postgres://myuser:mypassword@localhost:5432/mydatabase"
# Remote PostgreSQL
./postgresql-mcp "postgres://user:pass@remote-host:5432/production_db"
# Local PostgreSQL without password (trust authentication)
./postgresql-mcp "postgres://postgres@localhost:5432/testdb"
# PostgreSQL with SSL (if supported)
./postgresql-mcp "postgresql://user:pass@secure-host:5432/db?sslmode=require"This server communicates via stdin/stdout using the Model Context Protocol. It's designed to be used with AI assistants that support MCP servers.
Execute SQL queries on the connected PostgreSQL database.
Parameters:
sql(string): The SQL query to execute
Example Usage:
SELECT * FROM users WHERE age > 25;Response Format:
{
"success": true,
"rowCount": 3,
"data": [
{
"id": 1,
"name": "John Doe",
"age": 30,
"email": "john@example.com"
},
{
"id": 2,
"name": "Jane Smith",
"age": 28,
"email": "jane@example.com"
}
]
}Error Response:
{
"success": false,
"error": "relation \"nonexistent_table\" does not exist"
}The server automatically converts PostgreSQL data types to appropriate JSON representations:
| PostgreSQL Type | JSON Type | Notes |
|---|---|---|
bool |
boolean | |
int2, int4 |
number | 16-bit and 32-bit integers |
int8 |
number | 64-bit integers |
float4, float8 |
number | Single and double precision floats |
numeric, decimal |
string | High-precision decimals as strings |
text, varchar, char |
string | Text data |
timestamp, timestamptz |
string | ISO 8601 formatted timestamps |
| Other types | string | Fallback to string representation |
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ AI Assistant │◄──►│ PostgreSQL MCP │◄──►│ PostgreSQL │
│ (Client) │ │ Server │ │ Database │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│ │ │
│ MCP Protocol │ tokio-postgres │
│ (stdin/stdout) │ │
└────────────────────────┘ │
│
SQL Queries ────┘
- rmcp: Model Context Protocol implementation
- tokio-postgres: Async PostgreSQL client
- tokio: Async runtime
- serde: Serialization framework
- rust_decimal: Decimal number handling
- chrono: Date and time handling
- Connection Security: Always use secure connection strings in production
- SQL Injection: The server executes raw SQL queries - ensure proper input validation at the client level
- Access Control: The server inherits the permissions of the database user specified in the connection string
- Network Security: Consider using SSL/TLS connections for remote databases
The server provides detailed error reporting for common issues:
- Connection Failures: Invalid connection strings or unreachable databases
- SQL Syntax Errors: Malformed queries with PostgreSQL error messages
- Permission Errors: Insufficient database privileges
- Type Conversion Errors: Automatic fallback to string representation
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions:
- Check the error messages in the console output
- Verify your PostgreSQL connection string is correct
- Ensure your PostgreSQL server is running and accessible
- Open an issue with detailed error information
- Connection pooling for better performance
- SSL/TLS connection support
- Prepared statement support
- Transaction management
- Schema introspection tools
- Query result streaming for large datasets
- Connection health monitoring