DataGrip 2024.2 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 WIP.

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 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:

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

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

  3. Attached directories and the user files that they contain

    For more information about attaching directories, refer to Attach a directory with SQL files.

  4. Query consoles

    For more information about creating a console, refer to Create a query console.

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

    For more information about data extractors and loaders, refer to Data extractors and Data loaders topics.

  6. Scratch files

    For more information about using scratch files, refer to Scratch files.

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

    For more information about attaching directories, refer to Attach a directory with SQL files.

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

    For more information about opening and creating query consoles, refer to Query consoles.

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 . Here are the options of working with user files once the directory is attached to the DataGrip project:

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

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

  • Copy the existing files located elsewhere to the attached directory in Files tool window .

Also, you can either store all your queries in a single SQL file or in several separate ones.

In this tutorial, we will consider the following example. There is a query_book 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 sets of queries as file contents:

  • Single SQL file: my_queries.sql

    -- 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; -- Insert a new actor with the current timestamp INSERT INTO guest.public.actor (first_name, last_name, last_update) VALUES (?, ?, CURRENT_TIMESTAMP); -- Delete actors with ID greater than 200 DELETE FROM guest.public.actor WHERE actor_id > 200;
  • Several SQL files: :

    SELECT.sql

    -- 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;

    other_queries.sql

    -- Insert a new actor with the current timestamp INSERT INTO guest.public.actor (first_name, last_name, last_update) VALUES (?, ?, CURRENT_TIMESTAMP); -- Delete actors with ID greater than 200 DELETE FROM guest.public.actor WHERE actor_id > 200;

To access the directory with its SQL 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 query_book 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 filename.

    Open en existing SQL files in the attached folder
  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 query_book 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 filename.

    Open en existing SQL files in the attached folder

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

    Create new 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 query_book 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, my_queries.sql.

      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.

      Storing all queries in a single console
    6. In the editor, paste your queries set into the file.

    Storing all queries in a single console
    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 query_book 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, SELECT.sql.

      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.

      Storing all queries in a single console
    6. In the editor, paste your SELECT queries set into the file.

    7. Create a new other_queries.sql file and paste your other queries into it.

    Storing all queries in a single console

      Copy existing files

      1. In your OS file browser, copy the SQL file with your query set that you want to use in DataGrip.

      2. In DataGrip, open the Files tool window ( View | Tool Windows | Files) .

      3. In the Files tool window ( View | Tool Windows | Files) , select your attached directory and press Ctrl+V.

      4. In the Copy dialog that opens, make sure the file name and target directory are correct and click OK to copy the file.

      Copying an existing SQL file to the project root

      DataGrip copies your SQL file into the attached directory. To open your file, double-click it in the Files tool window .

      Open en existing SQL files in the attached folder
      1. In your OS file browser, copy the SQL files with your query sets that you want to use in DataGrip.

      2. In DataGrip, open the Files tool window ( View | Tool Windows | Files) .

      3. In the Files tool window ( View | Tool Windows | Files) , select your attached directory and press Ctrl+V.

      4. In the Copy dialog that opens, make sure the target directory is correct and click OK to copy the files.

      Copying existing SQL files to the project root

      DataGrip copies your SQL files into the attached directory. To open your file, double-click it in the Files tool window .

      Open en existing SQL files in the attached folder

        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. 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.

        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: 23 August 2024