/ examples / tools / external / postgres / tool.py
tool.py
 1  """PostgreSQL Tool Example.
 2  
 3  This example demonstrates how to use the PostgreSQL tool with PraisonAI agents.
 4  
 5  Requirements:
 6      pip install "praisonai[tools]"
 7      Docker: docker run -d --name postgres -e POSTGRES_PASSWORD=praison123 -e POSTGRES_DB=praisonai -p 5432:5432 postgres:16
 8  
 9  Usage:
10      python tool.py
11  """
12  
13  from praisonai_tools import PostgresTool
14  
15  
16  def main():
17      # Initialize PostgreSQL tool
18      pg = PostgresTool(
19          host="localhost",
20          port=5432,
21          database="praisonai",
22          user="postgres",
23          password="praison123"
24      )
25      
26      # Example 1: List tables
27      print("=" * 60)
28      print("Example 1: List Tables")
29      print("=" * 60)
30      
31      tables = pg.list_tables()
32      
33      if isinstance(tables, dict) and "error" in tables:
34          print(f"Error: {tables['error']}")
35      else:
36          print(f"Found {len(tables)} tables:")
37          for t in tables[:10]:
38              print(f"  - {t.get('table_name', t)}")
39      
40      # Example 2: Query
41      print("\n" + "=" * 60)
42      print("Example 2: Execute Query")
43      print("=" * 60)
44      
45      result = pg.query("SELECT version()")
46      
47      if isinstance(result, dict) and "error" in result:
48          print(f"Error: {result['error']}")
49      else:
50          print(f"PostgreSQL version: {result}")
51      
52      print("\n✅ PostgreSQL tool working correctly!")
53  
54  
55  if __name__ == "__main__":
56      main()