DataGrip 2024.3 Help

Use Files tool window

In this tutorial, you will learn how to work in the Files tool window . The tool window acts as a file browser window, providing access to the files and directories that are attached to the IDE project, access to the project root directory, and also to the IDE special file types: query consoles, data processing scripts, and scratch files.

For example, you can attach new directories, create new items (directories and files), open files in the editor, run SQL files, view the local history of a file, put directories under VCS, and so on. You can rearrange files and directories by dragging them, and also import data from files to database.

For more information about the tool window, refer to Files tool window.

Quick overview

You can open the Files tool window by doing one of the following:

  • In the main menu, go to View | Tool Windows | Files.

  • On the right tool window bar, click Files tool window icon Files.

  • Press Alt+2.

By default, the tool window is located on the right side of the main window:

User files in the Files tool window
  1. the Files tool window icon Files tool window Alt+2

  2. Project root directory and the user files that it contains

  3. Attached directories and the user files that they contain

    To learn how to attach directories to your project, refer to Attach a directory with SQL files.

  4. Query consoles

    To learn how to create consoles, refer to Create a query console.

  5. Data processing scripts: extractors, aggregators, and loaders

    To learn how to use data extractors and loaders, refer to Data extractors and Data loaders topics.

  6. Scratch files

    To learn how to use scratch files, refer to Scratch files.

  7. the Attach Directory to Project icon Attach Directory to Project header button

  8. the Jump to Query Console icon Jump to Query Console toolbar button

User files

In the Files tool window , you can access the files stored both in a project root directory and in attached directories.

Project root directory

The DataGrip project root directory is always shown at the top of the Files tool window .

If you want to be able to quickly access certain files for your current project in the IDE, and also to share them along with the project, you can copy the files into the project directory and have access to them in the Files tool window .

Attached directory

You can attach any directory to your DataGrip project and have access to it and its files in the Files tool window . In the attached directory, you can open existing SQL files and also create new ones. DataGrip will index the files, providing the option to search for both those files and their content using the IDE's search functionality.

For the attached SQL files, you can run the file itself by using run configurations, and also run separate queries that are stored in the file. Running separate queries is possible because any SQL file can be attached to a data source using the <data source> selector on the editor toolbar. For more information about running queries from a file, refer to the Run queries topic.

Here are the options of accessing SQL files once their directory is attached to the DataGrip project:

  • Open the existing files already located in the attached directory.

  • Create new files in the attached directory in Files tool window .

To learn how to open, create and copy files in Files tool window , we will consider the following example in this tutorial: there is a my_scripts directory where you already store the SQL files, and you want to have access to that directory and its files in the IDE. We will use the following set of queries as the query_book.sql file contents:

-- Retrieve all actors SELECT actor_id, first_name, last_name, last_update FROM actor; -- Find the number of films each actor has appeared in SELECT a.actor_id, a.first_name, a.last_name, COUNT(fa.film_id) AS film_count FROM actor a JOIN film_actor fa ON a.actor_id = fa.actor_id GROUP BY a.actor_id, a.first_name, a.last_name ORDER BY film_count DESC; -- List all movies by category SELECT c.name AS category, f.title AS film FROM film_category fc JOIN film f ON fc.film_id = f.film_id JOIN category c ON fc.category_id = c.category_id ORDER BY c.name, f.title; -- PostgreSQL -- Delete actors with ID greater than 200 DELETE FROM guest.public.actor WHERE actor_id > 200; -- MySQL -- Delete actors with ID greater than 200 DELETE FROM sakila.actor WHERE actor_id > 200; -- PostgreSQL -- Insert a new actor with the current timestamp INSERT INTO guest.public.actor (first_name, last_name, last_update) VALUES (?, ?, CURRENT_TIMESTAMP); -- MySQL -- Insert a new actor with the current timestamp INSERT INTO sakila.actor (first_name, last_name, last_update) VALUES (?, ?, CURRENT_TIMESTAMP);

To access the directory with its files in DataGrip, you need to attach the directory to the project first.

Open existing files

  1. In the Files tool window , click the Attach Directory to Project button (Attach Directory to Project) in the toolbar.

    Alternatively, right-click in the area of the Files tool window and select Attach Directory to Project.

  2. In the file browser, navigate to the directory that you want to attach. In our case, it is the my_scripts directory.

  3. Click Open.

    Attaching the directory with the SQL file to the DataGrip project
  4. The directory will become available in the Files tool window . To open your SQL file in the editor, expand the attached directory node and double-click the file name.

    Open en existing SQL files in the attached folder

Create new files

You can also create new files in the attached directory. For example, if your queries are stored in a certain query_book.sql file, or various places, and you want to store them in a dedicated my_scripts directory on your local machine. You can attach that directory to the DataGrip project, create a new SQL file in it, and copy your SQL query sets into that file.

  1. In the Files tool window , click the Attach Directory to Project button (Attach Directory to Project) in the toolbar.

    Alternatively, right-click in the area of the Files tool window and select Attach Directory to Project.

  2. In the file browser, navigate to the directory that you want to attach. In our case, it is the my_scripts directory.

  3. Click Open.

    Attaching the directory with the SQL file to the DataGrip project
  4. Right-click your attached directory and select New | SQL File.

  5. In the New File popup window that appears, enter the file name. For example, query_book.sql.

    Storing all queries in a single console

    Your new SQL file will appear under the attached directory node in the Files tool window file tree. DataGrip will also open it in a code editor tab.

  6. In the editor, type your queries or paste your queries set into the file.

Storing all queries in a single console

Run SQL files

You can also run your SQL files from the Files tool window . To do that, use the context menu of a file and select the database or schema that you want to run it against. DataGrip will create a run/debug configuration to run your file.

Each configuration is a named set of startup properties that define what to execute and what parameters and environment should be used. For more information about run configurations, refer to Run/debug configurations.

For example, you need to run the sqlite-sakila-schema.sql file against your identifier.sqlite database.

  1. In the Files tool window Alt+2, navigate to the sqlite-sakila-schema.sql file, right-click it, and select Run 'sqlite-sakila-schema.sql'. Alternatively, select it and press Alt+Shift+R.

    Run SQL files from the Files tool window
  2. Select the settings for your run configuration.

    • In Target data source / schema, click the Add button (the Add button) and select the identifier.sqlite database.

    • In Script files, your SQL file is added automatically.

    Edit Configuration dialog
  3. Click Run.

For more information about running SQL files, refer to the Run files topic.

Import data from files to database tables

You can import data from your files to database tables by dragging the files to the Database Explorer and configuring the import settings. For example, there is the myFiles directory with countries.xlsx file. You need to import the file's data to the guest.public schema of PostgreSQL data source.

  1. Attach the directory to your IDE project. In our case, the myFiles directory.

    Directory with the Excel file is attached to the DataGrip project
  2. In the myFiles tree node, navigate to the countries.xlsx file.

  3. Drag the countries.xlsx file to the database in Database Explorer that you want to import the data to. In our case, it is the guest.public schema of PostgreSQL data source.

    Drag the Excel file from Files tool window to Database Explorer

Share directories on GitHub

You can share your attached directories on GitHub by creating a local repository and linking it to a remote one. For example, there is the MyScripts directory that you need to link with the https://github.com/JetBrainsUser/MyScripts.git GitHub repository.

  1. Attach the directory to your IDE project. In our case, the MyScripts directory.

    Attach a directory
  2. Create a local Git repository in the attached directory by navigating to Git | Create Git Repository in the main menu and selecting the directory you want to share.

    Create a local repository
  3. Link your local and remote repositories by navigating to Git | Manage Remotes and adding the link to your GitHub repository as a new remote.

    Link local and remote repositories

Query consoles

Query or database consoles are SQL files that are attached to a data source. You can write and execute SQL statements in query consoles the same way as you do it in terminal. The consoles are not included in the project context. For more information about the consoles, refer to Query consoles.

When you create a data source in DataGrip, a default console is created automatically. In the Files tool window , consoles are located under Scratches and Consoles | Database Consoles .

For example, you have a PostgreSQL data source, and you have already created and used a console query console for it. You can open the console in the Files tool window . For more information about creating query consoles, refer to Create a query console.

Query console of a data source

Open existing console

  1. In the Files tool window , navigate to Scratches and Consoles | Database Consoles .

  2. Expand the PostgreSQL data source node.

  3. Double-click the console [PostgreSQL] console.

DataGrip will open the console [PostgreSQL] query console in the editor.

Query console of a data source

Create a new console

  1. In the Files tool window , navigate to Scratches and Consoles | Database Consoles .

  2. Right-click the PostgreSQL data source and select New | Query Console.

    Alternatively, press Ctrl+Shift+Q.

DataGrip will create a new console_1 [PostgreSQL] query console and open it in the editor.

Create a new query console

Rename your console

  1. In the Files tool window , navigate to Scratches and Consoles | Database Consoles .

  2. Expand the PostgreSQL data source node.

  3. Right-click the console that you want to rename and select Refactor | Rename.

    Alternatively, press Shift+F6.

  4. In the Rename dialog, type the new name for the console. For example, shiny_new_console.sql.

    Rename dialog for the console

DataGrip will rename the console_1 [PostgreSQL] query console to shiny_new_console.

Create a new query console

Data processing scripts

You can create custom data extractors, aggregators, and loaders by writing their code and placing the file in a corresponding directory.

For example, you have the following Groovy code of a data aggregator script that finds the longest common substring in the selected data:

/* * Available context bindings: * COLUMNS List<DataColumn> * ROWS Iterable<DataRow> * OUT { append() } * FORMATTER { format(row, col); formatValue(Object, col); getTypeName(Object, col); isStringLiteral(Object, col); } * TRANSPOSED Boolean * plus ALL_COLUMNS, TABLE, DIALECT * * where: * DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object } * DataColumn { columnNumber(), name() } */ def lcs(X, Y) { int m = X.length() int n = Y.length() int result = 0 int end = 0 int[][] length = new int[2][n+1] int currRow = 0 for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) { length[currRow][j] = 0 } else if (X[i - 1] == Y[j - 1]) { length[currRow][j] = length[1 - currRow][j - 1] + 1 if (length[currRow][j] > result) { result = length[currRow][j] end = i - 1 } } else { length[currRow][j] = 0 } } currRow = 1 - currRow } if (result == 0) return "No Common Substring" return X.substring(end - result + 1, end + 1) } ArrayList<String> values = [] ROWS.each { row -> COLUMNS.each { column -> def value = row.value(column) if(value instanceof String) { values.add(value) } } } def result = values[0] values.each { value -> result = lcs(result, value) } OUT.append(result)

Add your custom data aggregator

  1. In the Files tool window , navigate to Scratches and Consoles | Extensions | Database Tools and SQL | data | aggregators.

  2. Right-click the aggregators node and select New | File.

  3. In the New File popup, type the new file name. For example, longest_common_substring.groovy.

  4. Paste your new data aggregator script into the file.

    (Optional) If the Convert Pasted Code dialog appears, press Cancel. In this tutorial, we save the generated script itself.

New data aggregator appears on the aggregator list in status bar for data editor and can be used with your data.

Paste the created script to a new file

Scratch files

In scratch files, you can store drafts of your code, temporary notes, and other content that should not be included in the project context and does not require it.

The main difference between query consoles and scratches is that every console is only attached to one data source, whereas scratches don't have to be attached to a data source to store your code. However, if needed, a scratch file can be attached to any data source.

Scratch files are generally more useful for other types of files besides SQL, but they can still be used for SQL files as well.

For example, you need to quickly store the following DELETE statements somewhere:

DELETE FROM address ; DELETE FROM city ; DELETE FROM country ; DELETE FROM language ;

Create a new SQL scratch file

  1. Right-click any element in the tool window and select New | Scratch File.

    Alternatively, press Ctrl+Alt+Shift+Insert.

  2. In the New Scratch File popup, select Generic SQL.

    The new SQL scratch file will appear in Scratches directory. IDE will automatically open it in the editor.

  3. In the editor, paste your statements into the scratch file.

Paste the created script to a new scratch file
Last modified: 22 November 2024