> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgreSQL MCP Integration

> Guide for integrating PostgreSQL database operations with PraisonAI agents using MCP

## Add PostgreSQL Tool to AI Agent

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
flowchart LR
    In[Query] --> Agent[AI Agent]
    Agent --> Tool[PostgreSQL MCP]
    Tool --> Agent
    Agent --> Out[Answer]
    
    style In fill:#8B0000,color:#fff
    style Agent fill:#2E8B57,color:#fff
    style Tool fill:#336791,color:#fff
    style Out fill:#8B0000,color:#fff
```

## Quick Start

<Steps>
  <Step title="Install Dependencies">
    Make sure you have Node.js installed, as the MCP server requires it:

    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    pip install praisonaiagents mcp
    ```
  </Step>

  <Step title="Set up PostgreSQL">
    Ensure you have PostgreSQL running locally or specify your PostgreSQL connection URL.
  </Step>

  <Step title="Create a file">
    Create a new file `postgres_agent.py` with the following code:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    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")
    ```
  </Step>

  <Step title="Run the Agent">
    Execute your script:

    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    python postgres_agent.py
    ```
  </Step>
</Steps>

<Note>
  **Requirements**

  * Python 3.10 or higher
  * Node.js installed on your system
  * PostgreSQL server running locally or remotely
  * OpenAI API key (for the agent's LLM)
</Note>
