Skip to content

strands-sql

A general-purpose SQL tool for Strands Agents — supports PostgreSQL, MySQL, and SQLite via SQLAlchemy.

Terminal window
# SQLite (no extra driver needed)
pip install strands-sql
# PostgreSQL
pip install "strands-sql[postgres]"
# MySQL
pip install "strands-sql[mysql]"
from strands import Agent
from strands_sql import sql_database
agent = Agent(tools=[sql_database])
# Discover the schema
agent.tool.sql_database(action="schema_summary")
# Run a query
agent.tool.sql_database(
action="query",
sql="SELECT * FROM orders WHERE amount > 100 LIMIT 20",
)

Set your database connection via environment variable:

Terminal window
export DATABASE_URL="postgresql://user:password@localhost:5432/mydb"

Or pass it directly per call:

agent.tool.sql_database(
action="list_tables",
connection_string="sqlite:///./local.db",
)
  • No connection string found — make sure DATABASE_URL is set or pass connection_string explicitly.
  • Write query blocked — write operations require read_only=False explicitly.
  • Timeout errors — increase the timeout parameter (default: 30s, max: 300s).