postgres-mcp

MCP.Pizza Chef: tyrchen

Postgres MCP is a server implementation of the Model Context Protocol designed to provide AI agents with a standardized, secure interface to interact with PostgreSQL databases. It supports connection management, SQL validation, and a wide range of database operations including querying, inserting, updating, and schema management, enabling efficient and safe real-time database manipulation.

Use This MCP server To

Manage multiple PostgreSQL connections with pooling Execute and validate complex SELECT queries securely Insert, update, and delete database records via AI commands Create and drop tables and indexes programmatically Describe table structures and list schema tables for context Enable AI agents to perform real-time database operations Validate SQL statements for PostgreSQL-specific syntax Automate database schema changes through AI-driven workflows

README

Postgres MCP

Postgres MCP is a Model Context Protocol (MCP) implementation for PostgreSQL databases. It provides a standardized interface for AI agents to interact with PostgreSQL databases through a set of well-defined commands.

Features

  • Connection Management

    • Register and unregister database connections
    • Support for multiple concurrent database connections
    • Connection pooling for efficient resource management
  • Database Operations

    • Execute SELECT queries
    • Insert new records
    • Update existing records
    • Delete records
    • Create and drop tables
    • Create and drop indexes
    • Describe table structures
    • List tables in a schema
  • SQL Validation

    • Built-in SQL parser for validating statements
    • Support for PostgreSQL-specific syntax
    • Safety checks to ensure only allowed operations are performed

Installation

cargo install postgres-mcp

Usage

Configuration

Add the following to your MCP configuration file:

{
  "mcpServers": {
    "postgres": {
      "command": "postgres-mcp",
      "args": ["stdio"]
    }
  }
}

or run it in SSE mode:

First, start the postgres-mcp server in SSE mode:

postgres-mcp sse

Then, configure the MCP config file to use the SSE mode:

{
  "mcpServers": {
    "postgres": {
      "url": "http://localhost:3000/sse"
    }
  }
}

Once you started the postgres-mcp server, you should see the status of the MCP config is green, like this (cursor):

mcp-status

And then you could interact with it via the agent, like this (cursor):

mcp

Commands

Register a Database Connection

pg_mcp register "postgres://postgres:postgres@localhost:5432/postgres"
# Returns a connection ID (UUID)

Unregister a Connection

pg_mcp unregister <connection_id>

Execute a SELECT Query

pg_mcp query <connection_id> "SELECT * FROM users"

Insert Data

pg_mcp insert <connection_id> "INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com')"

Update Data

pg_mcp update <connection_id> "UPDATE users SET name = 'Jane Doe' WHERE id = 1"

Delete Data

pg_mcp delete <connection_id> "users" "1"

Create a Table

pg_mcp create <connection_id> "CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(255), email VARCHAR(255))"

Drop a Table

pg_mcp drop <connection_id> "users"

Create an Index

pg_mcp create_index <connection_id> "CREATE INDEX idx_users_name ON users (name)"

Drop an Index

pg_mcp drop_index <connection_id> "idx_users_name"

Describe a Table

pg_mcp describe <connection_id> "users"

Dependencies

  • Rust 1.70 or later
  • PostgreSQL 12 or later
  • Required Rust crates:
    • anyhow: 1.0
    • arc-swap: 1.7
    • sqlx: 0.8 (with "runtime-tokio", "tls-rustls-aws-lc-rs", "postgres" features)
    • rmcp: 0.1 (with "server", "transport-sse-server", "transport-io" features)
    • schemars: 0.8
    • sqlparser: 0.55
    • tokio: 1.44

Development

To build from source:

git clone https://github.com/yourusername/postgres-mcp.git
cd postgres-mcp
cargo build --release

License

MIT license. See LICENSE.md for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

postgres-mcp FAQ

How do I install postgres-mcp?
Install postgres-mcp easily using 'cargo install postgres-mcp' from the command line.
Can postgres-mcp handle multiple database connections?
Yes, it supports multiple concurrent PostgreSQL connections with connection pooling for efficiency.
Does postgres-mcp validate SQL queries?
Yes, it includes a built-in SQL parser that validates PostgreSQL-specific syntax and ensures safe operations.
What database operations can postgres-mcp perform?
It supports SELECT, INSERT, UPDATE, DELETE, table and index creation and deletion, and schema inspection.
Is postgres-mcp compatible with all PostgreSQL versions?
It supports standard PostgreSQL syntax and features, but compatibility depends on the specific PostgreSQL version used.
How does postgres-mcp ensure security during database operations?
It performs safety checks on SQL statements to restrict operations to allowed commands only.
Can I use postgres-mcp with different LLM providers?
Yes, postgres-mcp is provider-agnostic and works with OpenAI, Claude, Gemini, and others.
How do I configure postgres-mcp in my MCP setup?
Add postgres-mcp to your MCP configuration file with connection details and desired settings.