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
.On the tool window bar, click Files.
Press Alt+2.
By default, the tool window is located on the right side of the main window:
Files Alt+2
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.
To learn how to create consoles, refer to Create a query console.
Data processing scripts: extractors, aggregators, and loaders
To learn how to use data extractors and loaders, refer to Data extractors and Data loaders topics.
To learn how to use scratch files, refer to Scratch files.
Attach Directory to Project header button
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:
To access the directory with its files in DataGrip, you need to attach the directory to the project first.
Open existing files
In the Files tool window , click the Attach Directory to Project button () in the toolbar.
Alternatively, right-click in the area of the Files tool window and select Attach Directory to Project.
In the file browser, navigate to the directory that you want to attach. In our case, it is the my_scripts directory.
Click Open.
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.
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.
In the Files tool window , click the Attach Directory to Project button () in the toolbar.
Alternatively, right-click in the area of the Files tool window and select Attach Directory to Project.
In the file browser, navigate to the directory that you want to attach. In our case, it is the my_scripts directory.
Click Open.
Right-click your attached directory and select
.In the New File popup window that appears, enter the file name. For example, query_book.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.
In the editor, type your queries or paste your queries set into the file.
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 Use Run configurations.
For example, you need to run the sqlite-sakila-schema.sql file against your identifier.sqlite
database.
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.
Select the settings for your run configuration.
In Target data source / schema, click the Add button () and select the
identifier.sqlite
database.In Script files, your SQL file is added automatically.
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.
Attach the directory to your IDE project. In our case, the myFiles directory.
In the myFiles tree node, navigate to the countries.xlsx file.
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.
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.
Attach the directory to your IDE project. In our case, the MyScripts directory.
Create a local Git repository in the attached directory by navigating to
in the main menu and selecting the directory you want to share.Link your local and remote repositories by navigating to
and adding the link to your GitHub repository as a new remote.
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.
Open existing console
In the Files tool window , navigate to Scratches and Consoles | Database Consoles .
Expand the PostgreSQL data source node.
Double-click the console [PostgreSQL] console.
DataGrip will open the console [PostgreSQL] query console in the editor.
Create a new console
In the Files tool window , navigate to Scratches and Consoles | Database Consoles .
Right-click the PostgreSQL data source and select .
Alternatively, press Ctrl+Shift+Q.
DataGrip will create a new console_1 [PostgreSQL] query console and open it in the editor.
Rename your console
In the Files tool window , navigate to Scratches and Consoles | Database Consoles .
Expand the PostgreSQL data source node.
Right-click the console that you want to rename and select
.Alternatively, press Shift+F6.
In the Rename dialog, type the new name for the console. For example,
shiny_new_console.sql
.
DataGrip will rename the console_1 [PostgreSQL] query console to shiny_new_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:
Add your custom data aggregator
In the Files tool window , navigate to Scratches and Consoles | Extensions | Database Tools and SQL | data | aggregators.
Right-click the aggregators node and select .
In the New File popup, type the new file name. For example, longest_common_substring.groovy.
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.
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:
Create a new SQL scratch file
Right-click any element in the tool window and select
.Alternatively, press Ctrl+Alt+Shift+Insert.
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.
In the editor, paste your statements into the scratch file.