PostgreSQL MCP Integration

Guide for integrating PostgreSQL database operations with PraisonAI agents using MCP

MCP(...) accepts three equivalent construction forms — see Three equivalent forms.

Add PostgreSQL Tool to AI Agent

Diagram

Query

AI Agent

PostgreSQL MCP

Answer

Quick Start

  1. Install Dependencies

    Make sure you have Node.js installed, as the MCP server requires it:

    bash
    pip install praisonaiagents mcp
  2. Set up PostgreSQL

    Ensure you have PostgreSQL running locally or specify your PostgreSQL connection URL.

  3. Create a file

    Create a new file postgres_agent.py with the following code:

    python
    from praisonaiagents import Agent, MCP
    import os
    
    # PostgreSQL connection string
    postgres_url = "postgresql://localhost/mydb"
    
    # Use a single string command with PostgreSQL configuration
    postgres_agent = Agent(
        instructions="""You are a helpful assistant that can interact with PostgreSQL databases.
        Use the available tools when relevant to manage database operations.""",
        llm="gpt-4o-mini",
        tools=MCP("npx -y @modelcontextprotocol/server-postgres", args=[postgres_url])
    )
    
    postgres_agent.start("List all tables in the database")
  4. Run the Agent

    Execute your script:

    bash
    python postgres_agent.py