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

# SQL

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

## Prerequisites

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

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

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/data/pgdata \
  -v pgvolume:/var/lib/postgresql/data \
  -p 5532:5432 \
  --name pgvector \
  phidata/pgvector:16
```

## 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/tools/sql_tools.py theme={null}
from phi.agent import Agent
from phi.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`         | `str`            | -       | The URL for connecting to the database.                                     |
| `db_engine`      | `Engine`         | -       | The database engine used for connections and operations.                    |
| `user`           | `str`            | -       | The username for database authentication.                                   |
| `password`       | `str`            | -       | The password for database authentication.                                   |
| `host`           | `str`            | -       | The hostname or IP address of the database server.                          |
| `port`           | `int`            | -       | The port number on which the database server is listening.                  |
| `schema`         | `str`            | -       | The specific schema within the database to use.                             |
| `dialect`        | `str`            | -       | The SQL dialect used by the database.                                       |
| `tables`         | `Dict[str, Any]` | -       | A dictionary mapping table names to their respective metadata or structure. |
| `list_tables`    | `bool`           | `True`  | Enables the functionality to list all tables in the database.               |
| `describe_table` | `bool`           | `True`  | Enables the functionality to describe the schema of a specific table.       |
| `run_sql_query`  | `bool`           | `True`  | Enables the functionality to execute SQL queries directly.                  |

## 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.            |

## Information

* View on [Github](https://github.com/agno-agi/phidata/blob/main/phi/tools/sql.py)
