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

# SQL

> The SQLTools toolkit enables an Agent to run SQL queries and interact with databases.

**SQLTools** enable an Agent to run SQL queries and interact with databases.

## Prerequisites

The following example requires the `sqlalchemy` and `openai` libraries and a database URL.

```shell theme={null}
uv pip install -U sqlalchemy openai
```

You will also need to install the appropriate Python adapter for the specific database you intend to use.

### PostgreSQL

For PostgreSQL, you can install the `psycopg` adapter:

```shell theme={null}
uv pip install -U psycopg
```

### MySQL

For MySQL, you can install the `mysqlclient` adapter:

```shell theme={null}
uv pip install -U mysqlclient
```

The `mysqlclient` adapter may have additional system-level dependencies. Please consult the [official installation guide](https://github.com/PyMySQL/mysqlclient/blob/main/README.md#install) for more details.

You will also need a database. The following example uses a Postgres database running in a Docker container.

```shell theme={null}
 docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql \
  -v pgvolume:/var/lib/postgresql \
  -p 5532:5432 \
  --name pgvector \
  agnohq/pgvector:18
```

## Example

The following agent will run a SQL query to list all tables in the database and describe the contents of one of the tables.

```python cookbook/91_tools/sql_tools.py theme={null}
from agno.agent import Agent
from agno.tools.sql import SQLTools

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

agent = Agent(tools=[SQLTools(db_url=db_url)])
agent.print_response("List the tables in the database. Tell me about contents of one of the tables", markdown=True)
```

## Toolkit Params

| Parameter | Type | Default | Description |
| - | - | - | - |
| `db_url` | `Optional[str]` | `None` | The URL for connecting to the database. |
| `db_engine` | `Optional[Engine]` | `None` | The database engine used for connections and operations. |
| `user` | `Optional[str]` | `None` | The username for database authentication. |
| `password` | `Optional[str]` | `None` | The password for database authentication. |
| `host` | `Optional[str]` | `None` | The hostname or IP address of the database server. |
| `port` | `Optional[int]` | `None` | The port number on which the database server is listening. |
| `schema` | `Optional[str]` | `None` | The specific schema within the database to use. |
| `dialect` | `Optional[str]` | `None` | The SQL dialect used by the database. |
| `tables` | `Optional[Dict[str, Any]]` | `None` | A dictionary mapping table names to their respective metadata or structure. |
| `enable_list_tables` | `bool` | `True` | Enables the functionality to list all tables in the database. |
| `enable_describe_table` | `bool` | `True` | Enables the functionality to describe the schema of a specific table. |
| `enable_run_sql_query` | `bool` | `True` | Enables the functionality to execute SQL queries directly. |
| `all` | `bool` | `False` | Enables all functionality when set to True. |

## Toolkit Functions

| Function | Description |
| - | - |
| `list_tables` | Lists all tables in the database. |
| `describe_table` | Describes the schema of a specific table. |
| `run_sql_query` | Executes SQL queries directly. |

## Developer Resources

* [Tools](https://github.com/agno-agi/agno/blob/main/libs/agno/agno/tools/sql.py)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.