Back to JetBrains

AI Agents for SQL and Databases in DataGrip

JetBrainsAugust 26, 202613m
In a Nutshell

DataGrip's AI agents connect directly to databases via MCP tools, letting users create data sources, query schemas, and run SQL through natural language conversation. The agent can discover connections from other tools, analyze database structure, execute read queries, and even drop objects after safety checks, with all permissions managed in IDE settings. Users can reference specific database objects or files using @mentions, and the AI provides detailed explanations while maintaining security by never receiving actual credentials.

AI-Generated Notes

These notes were generated by AI and may contain inaccuracies.

Once the AI assistant plugin is installed, the AI assistant button appears in the top toolbar. Clicking it opens the tool window, where users can choose between chat mode or any agent installed on the machine. Available agents include Claude, Codex, Gemini, or any other agent from the ACP registry. Claude is used for this demonstration.

There might be a need to configure the MCP server for the specific agent. Refer to DataGrip's documentation to accomplish this.

Method 1: Using JDBC URL

The simplest method to create a data source is to use a JDBC URL. Type "create data source from URL" and paste the URL into the agent chat. Claude first asks for permission to run a skill called database connection management. Once allowed, Claude requests permission to run the MCP tool called create database connection. After allowing this, it asks for credentials since they aren't transferred via the URL. After entering username and password, the data source is created and visible in the database explorer tool window, complete with all its objects.

A connection is created within DataGrip, and credentials are never passed to the AI agent.

Method 2: Providing Connection Details Directly

Another straightforward way to create a data source is to provide the agent with all known connection details. Type "create another data source" and specify the database type (MySQL), host (locally), port (33081), and database name (guest). Claude asks to use the database connection management skill, then prompts for credentials. After entering credentials and clicking okay, the second data source is created. All details are visible in the chat, the test connection is successful, and objects are visible in the database explorer.

Method 3: Exporting Connections from Other Tools

The agent can locate relevant files for any database tools installed and import the connections. Type "find my connections in pgAdmin and import them here." Claude searches for where pgAdmin connections are stored on the computer. After granting permissions, two servers are found in pgAdmin and appear on the left side of the screen. Username and password must be entered for both. Once completed, the data sources are ready to use. The agent provides details about these connections in the chat and they appear in database explorer. They aren't automatically introspected, but can be manually refreshed to see all underlying objects.

Because the AI agent knows about data sources, users can essentially talk to their connections. For example, asking "In which schema do I have data about employees?" triggers Claude to use MCP DataGrip tools. Claude asks permission to read the database schema. After allowing this, Claude provides the answer and displays the relevant table. The locally hosted MySQL data source contains a database with employee data.

When asked "Tell me about this database. What is it about? What tables are there and how are they connected?" regarding the Sakila database and PG AI data source, Claude requests permission to read the database schema. The actual SQL query that Claude intends to run is displayed, with a warning that the statement will read database data. After reviewing and allowing, multiple queries are prompted. Checking the box for "always allow Claude code to read database data" prevents future confirmation prompts. Claude provides information about where the database is hosted, how tables are connected, and other important details, giving a complete schema overview.

When prompted "Give me a query to show actors who played in more than five dramas and never played in comedies," Claude uses MCP tools to generate a result. The output is a fairly long SQL query. Since read queries were previously allowed automatically, the query executes and returns exactly one row: Max Mastel, who starred in seven dramas. Helpful comments explaining the logic are provided. The query can be copied from the AI chat window and pasted into the standard query console to modify or run manually.

Permissions can be managed by going to IDE settings, database, AI tools. Here, permissions granted to the agent can be reviewed and toggled. The permission to read database data can be granted, as well as permission to read database schemas. The schema permission allows the agent to retrieve metadata such as schema names, object names, and overall structure, but does not allow access to actual table data.

When using a schema as a sandbox containing the standard Sakila database plus random tables, the AI can identify which tables don't belong to the Sakila schema. When prompted "Which tables don't really belong to this database?" the agent recognizes that the core schema is the Sakila DVD rental database, then analyzes the rest to find outliers. The answer lists tables belonging to Sakila and points out those out of place, along with reasoning.

When asked to clean up the identified tables by dropping them, the agent performs safety checks first, checking whether anything else like views or foreign keys depends on these tables. After approval, Claude generates the necessary drop table statements and asks for confirmation to run them. After approval, the unnecessary tables are removed from the public schema. The AI chat window provides a summary of actions performed and the final outcome.

Users can ask the agent to recap past activities by typing "Tell me all the queries you ran over the last 2 days." After a moment of thought, the agent provides a comprehensive list of those queries and asks for feedback. The majority of listed queries are those executed during the demonstration.

Database objects can be mentioned directly within the agent chat using the at symbol (@) to trigger the mention feature. Selecting "@ DB object identifier" activates code completion. Typing "get C" and selecting "get customer balance" from suggestions inserts it. This explicitly tells the agent which database object is being asked about.

When asked about the get customer balance function, the agent requests permission to execute the routine to analyze it. This falls under modify data permission because DataGrip cannot guarantee what a stored procedure might do internally. Running any routine requires modification privileges. After approval, a comprehensive explanation of the routine is provided, including the signature, intent, and what the body of the function actually does. The agent detected MySQL specific syntax within this PostgreSQL procedure, explaining that this logic was likely inherited from the original Sakila database and was never properly converted to a case when statement during the migration to Postgres. The agent also breaks down what the function returns and highlights other relevant details.

In DataGrip, the files tool window can attach local directories and navigate the file system directly from the IDE. For example, a folder containing scripts and a CSV file named housing2.csv can be attached. When asked to identify data quality problems within the file, the at symbol (@) can be used to mention files, linking them directly in the chat prompt.

The analysis confirms data problems in housing2.csv. Issues identified include physical lines and line endings, header names containing spaces and periods (hostile to SQL, requiring double quoting), no primary key or row ID, and semantic issues such as four rows having more bedrooms than rooms (logically impossible) and mixed granularity across rows.

AI agents can process and analyze a tremendous amount of information, and DataGrip seamlessly integrates these capabilities directly into database workflow. Whether untangling complex joins, optimizing slow queries, or exploring new data sets, having an AI agent inside the IDE changes the workflow. Ensure the IDE is up to date and enable the AI Assistant plugin to start experimenting. All links needed are in the description.

Keep JetBrains in your library

Save the videos and channels worth coming back to, and find them again in one place.