#Overview
Learn how to build an agent that can autonomously query and analyze data from PostgreSQL databases. This example shows how plugins extend agent capabilities and enable complex database operations.
#What You'll Learn
- Using the PostgreSQL plugin with agents
- How agents autonomously query databases
- Plugin tool integration
- Database security best practices
#Prerequisites
- PostgreSQL database running locally or remotely
- Database credentials
asyncpginstalled:pip install asyncpg
#Step 1: Set Up the PostgreSQL Plugin
The PostgreSQL plugin provides database tools that agents can use:
python
from daita import Agent
from daita.plugins import postgresql
import asyncio
# Create the database plugin
db = postgresql(
host="localhost",
port=5432,
database="analytics",
username="postgres",
password="your_password"
)
# The plugin automatically exposes these tools to the agent:
# - query_database: Execute SELECT queries
# - list_tables: Get all table names
# - get_table_schema: Inspect table structure
# - execute_sql: Run INSERT/UPDATE/DELETESecurity note:
- Never hardcode credentials
- Use environment variables:
os.getenv("DB_PASSWORD") - Use read-only accounts when possible
#Step 2: Create the Agent with Database Access
Pass the plugin to the agent to enable database operations:
python
async def main():
# Create database plugin
db = postgresql(
host="localhost",
database="analytics",
username="postgres",
password="your_password"
)
# Create agent with database tools
agent = Agent(
name="Data Analyst",
prompt="""You are a data analyst. Help users query and analyze
data from the database. Use the available database tools to
explore tables, understand schemas, and query data.""",
llm_provider="openai",
model="gpt-4",
tools=[db] # Pass plugin as tool
)
await agent.start()What happens:
- Plugin registers its tools with the agent
- Agent can now see
query_database,list_tables, etc. - LLM receives tool descriptions and signatures
#Step 3: Let the Agent Explore the Database
The agent can autonomously discover and query database structure:
python
async def main():
db = postgresql(host="localhost", database="analytics")
agent = Agent(
name="Data Analyst",
prompt="You are a data analyst. Help users understand database data.",
llm_provider="openai",
model="gpt-4",
tools=[db]
)
await agent.start()
try:
# Agent will autonomously:
# 1. Call list_tables() to see available tables
# 2. Call get_table_schema() to understand structure
# 3. Formulate and provide a summary
result = await agent.run("What tables are in this database?")
print(result)
finally:
await agent.stop()Agent's autonomous process:
- Receives task: "What tables are in this database?"
- Recognizes need for database tool
- Calls
list_tables() - Receives table list
- Formats and presents results to user
#Step 4: Complex Data Analysis
Agents can handle multi-step analysis tasks:
python
async def main():
db = postgresql(host="localhost", database="analytics")
agent = Agent(
name="Data Analyst",
prompt="""You are a data analyst. Query databases to answer
questions. Always verify table structure before querying.""",
llm_provider="openai",
model="gpt-4",
tools=[db]
)
await agent.start()
try:
# Agent performs multi-step analysis:
# 1. Checks if 'users' table exists (list_tables)
# 2. Inspects schema (get_table_schema)
# 3. Queries for active users (query_database)
# 4. Analyzes and presents results
result = await agent.run("""
How many active users do we have?
Show me the top 5 users by activity count.
""")
print(result)
finally:
await agent.stop()
if __name__ == "__main__":
asyncio.run(main())#Step 5: Monitor Tool Execution
See exactly how the agent interacts with the database:
python
async def main():
db = postgresql(host="localhost", database="analytics")
agent = Agent(
name="Data Analyst",
prompt="You are a data analyst.",
llm_provider="openai",
model="gpt-4",
tools=[db]
)
await agent.start()
try:
result = await agent.run_detailed("""
Find all users who signed up in the last 30 days.
""")
print(f"Answer: {result['result']}\n")
print(f"Tools used: {len(result['tool_calls'])}\n")
# See each database operation
for i, call in enumerate(result['tool_calls'], 1):
print(f"{i}. {call['tool']}")
print(f" Args: {call['args']}")
if 'sql' in call['args']:
print(f" SQL: {call['args']['sql']}")
finally:
await agent.stop()
if __name__ == "__main__":
asyncio.run(main())#Complete Example with Error Handling
python
from daita import Agent
from daita.plugins import postgresql
import asyncio
import os
async def main():
# Create database plugin with environment variables
db = postgresql(
host=os.getenv("DB_HOST", "localhost"),
database=os.getenv("DB_NAME", "analytics"),
username=os.getenv("DB_USER", "postgres"),
password=os.getenv("DB_PASSWORD")
)
# Create agent
agent = Agent(
name="Data Analyst",
prompt="""You are a data analyst. Help users query and analyze
database data. Always verify table schemas before querying.""",
llm_provider="openai",
model="gpt-4",
tools=[db]
)
await agent.start()
try:
# Complex analysis task
result = await agent.run("""
Analyze our user base:
1. Total number of users
2. Users who signed up in the last 30 days
3. Most active users this month
""")
print("Analysis Results:")
print("=" * 50)
print(result)
except Exception as e:
print(f"Error: {e}")
finally:
await agent.stop()
if __name__ == "__main__":
asyncio.run(main())#Framework Internals
How plugins work with agents:
- Plugin Initialization: Plugin connects to database, registers tools
- Tool Registration: Each plugin method becomes an available tool
- LLM Integration: Tool definitions sent to LLM with parameters
- Autonomous Execution: Agent decides which tools to call based on task
- Connection Management: Plugin handles database connections automatically
Available PostgreSQL tools:
query_database(sql, params): Execute SELECT queriesexecute_sql(sql, params): Run INSERT/UPDATE/DELETElist_tables(): Get all table namesget_table_schema(table_name): Inspect columns and types
#Security Best Practices
- Use read-only accounts for query-only agents
- Environment variables for credentials
- Limit agent permissions - don't give DROP/ALTER access
- Validate queries - monitor what the agent executes
- Rate limiting - prevent excessive queries
#Key Takeaways
- Plugins = Pre-built tools: PostgreSQL plugin adds database operations
- Autonomous exploration: Agents can discover and understand database structure
- Multi-step workflows: Agents handle complex analysis automatically
- Connection management: Plugin handles pooling and cleanup
- Security matters: Always use appropriate permissions and credentials
#Next Steps
- Multi-agent workflows for coordinated data analysis
- Lineage tracking to track data transformations
- Streaming responses for long-running queries