An intelligent database agent demo that can interact with PostgreSQL databases using natural language queries powered by LLM models.
- Natural Language Interface: Ask questions about your database in nature language
- SQL Query Execution: Automatically generates and executes SQL queries based on your requests
- Conversation Memory: Maintains context across multiple interactions
- Error Handling: Robust error handling for database operations and API calls
- Data Serialization: Properly handles various PostgreSQL data types including arrays, timestamps, and custom types
- Go 1.23+
- PostgreSQL database
- LLM API key
-
Clone or download the source code
-
Install dependencies:
go mod tidy
-
Copy the environment configuration:
cp .env.example .env
-
Edit
.envfile with your actual configuration
-
Run the application:
go run . -
Start asking questions about your database:
You: Show me all tables in the database You: How many users do we have? You: What are the top 5 products by sales? -
Use special commands:
clear: Clear conversation historyexitorquit: Exit the application
main.go: Main application entry point and CLI interfacedb_agent.go: Core database agent implementationconfig.go: Configuration managementgo.mod: Go module dependencies.env.example: Environment variables template
github.com/joho/godotenv: Environment variable loadinggithub.com/lib/pq: PostgreSQL drivergithub.com/sashabaranov/go-openai: OpenAI API client
The agent properly serializes various PostgreSQL data types:
- Timestamps (converted to RFC3339 format)
- Arrays (both integer and string arrays)
- Binary data (converted to strings)
- Custom types (handled via reflection)
The agent maintains conversation history, allowing for:
- Follow-up questions
- Contextual queries
- Reference to previous results
Comprehensive error handling for:
- Database connection issues
- SQL execution errors
- API communication problems
- Data serialization issues
You: What tables do we have?
Agent: I'll check what tables are available in your database.
You: Show me the structure of the users table
Agent: Here's the structure of the users table with all columns and their data types.
You: How many active users do we have?
Agent: Based on the users table, there are 1,247 active users.
- Never commit your
.envfile with actual credentials - Use environment variables in production
- Consider implementing connection pooling for production use
- Review and sanitize any user inputs if extending this tool
Feel free to submit issues and pull requests to improve the functionality and add new features.
This project is provided as-is for educational and development purposes.