> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2-service-account.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# SQL Tools

> List tables and query a Postgres database from a connection URL with SQLTools.

```python sql_tools.py theme={null}
"""
Sql Tools
=============================

Demonstrates sql tools.
"""

from agno.agent import Agent
from agno.tools.sql import SQLTools

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------


db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

agent = Agent(tools=[SQLTools(db_url=db_url)])

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    agent.print_response(
        "List the tables in the database. Tell me about contents of one of the tables",
        markdown=True,
    )
```

## Run the Example

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U agno openai sqlalchemy
    ```
  </Step>

  <Step title="Export your OpenAI API key">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENAI_API_KEY="your_openai_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the example">
    Save the code above as `sql_tools.py`, then run:

    ```bash theme={null}
    python sql_tools.py
    ```
  </Step>
</Steps>

Full source: [cookbook/91\_tools/sql\_tools.py](https://github.com/agno-agi/agno/blob/main/cookbook/91_tools/sql_tools.py)
