Run SQL files
This functionality relies on the Database Tools and SQL plugin, which is bundled and enabled in IntelliJ IDEA by default. If the relevant features are not available, make sure that you did not disable the plugin.
note
The Database Tools and SQL plugin is not available in IntelliJ IDEA Community Edition.
Press CtrlAlt0S to open settings and then select Plugins.
Open the Installed tab, find the Database Tools and SQL plugin, and select the checkbox next to the plugin name.
note
This page describes how to run SQL files using the Database Tools and SQL plugin. For more information about running applications and other files, refer to Run applications.
SQL files are run against the selected databases or schemas using run configurations. Depending on your purposes, you can run SQL files using one of the following ways:
Create new run configurations or edit the existing ones via a run configuration widget.
Create new run configurations via Project tool window by running the selected file.
Create new run configurations via Database tool window by running a file against the selected database or schema.
In IntelliJ IDEA, the maximum number of DML statements that you can run from an SQL file is 1000.
Open the Run/Debug Configuration dialog in one of the following ways:
Select Run | Edit Configurations from the main menu.
With the Navigation bar visible (View | Appearance | Navigation Bar), choose Edit Configurations from the run/debug configuration selector.
Press AltShiftF10 and then press 00.
In the Run/Debug Configurations dialog, you can either create a new run configuration or edit an existing one.
To create a new run configuration, click the Add New Configuration icon () and select Database Script.
To edit an existing run configuration, expand the Database Script node and select the run configuration that you want to edit from the list.
The fields that appear in the right-hand pane display the settings for the selected configuration type.
Target data source / schema: databases or schemas against which you want to run your database scripts. This setting is dialect-dependent.
If you select a data source as a target, IntelliJ IDEA displays a schema in which the script will be run. It is the default schema.
Script files: SQL files that you want to run. To add files, click the Add button () and navigate to files that you want to run. If a script contains schema switching, you will see a warning ().
You can either run the configuration right away or save the configuration to run it later.
To save the run configuration for later, click OK.
To run the configuration right away, click Run.
In the Project tool window (View | Tool Windows | Project) , right-click the SQL file that you want to run, and select Run '<file_name>'. Alternatively, press AltShift0R.
Select the settings for your run configuration.
Target data source / schema: databases or schemas against which you want to run your database scripts. This setting is dialect-dependent.
If you select a data source as a target, IntelliJ IDEA displays a schema in which the script will be run. It is the default schema.
Script files: SQL files that you want to run. To add files, click the Add button () and navigate to files that you want to run. If a script contains schema switching, you will see a warning ().
Click Run.
In the Database tool window (View | Tool Windows | Database) , right-click a data source, or a schema and select SQL Scripts | Run SQL Script.
In the file browser window that opens, navigate to the SQL file that you want to run.
Click Open.
You can view the output in Run tool window. For more information about tool window controls, refer to Run tool window.
Open the Project tool window Alt01, navigate to the SQL file, and select it.
Drag the SQL file to Database tool window to the data source, database, or schema that you want to run it against.
Select the settings for your run configuration.
Target data source / schema: databases or schemas against which you want to run your database scripts. This setting is dialect-dependent.
If you select a data source as a target, IntelliJ IDEA displays a schema in which the script will be run. It is the default schema.
Script files: SQL files that you want to run. To add files, click the Add button () and navigate to files that you want to run. If a script contains schema switching, you will see a warning ().
Click Run.
You can run single files using a dedicated option on the toolbar. The run and debug buttons are active and allow you to instantly run the currently opened file.
In the editor, open the file that you want to run.
Click next to the Current File option on the toolbar.
Select the settings for your run configuration.
Target data source / schema: databases or schemas against which you want to run your database scripts. This setting is dialect-dependent.
If you select a data source as a target, IntelliJ IDEA displays a schema in which the script will be run. It is the default schema.
Script files: SQL files that you want to run. To add files, click the Add button () and navigate to files that you want to run. If a script contains schema switching, you will see a warning ().
Click Run.
You can access the other run modes by expanding the list. From the widget that opens, you can debug the code, run it with coverage, profile it, or open the run configuration to specify more options.
In the Project tool window (View | Tool Windows | Project) , select file or files that you want to run.
Right-click the selection and invoke Run <file_names> (or press CtrlShiftF10). This action creates a temporary run configuration.
In the Edit Configuration window that opens, click the Add button () in the Target data source / schema pane and select data sources or schemes against which you want to run the selected files.
You can click the Recent button () to select data sources that you used recently.
Click Run.
note
Before IntelliJ IDEA 2024.1, you had to attach your SQL files to a session manually using the <session> list in the top right corner of the editor.
Starting with IntelliJ IDEA 2024.1, you have to attach your SQL files to a data source using the corresponding list in the top right corner of the editor. The IDE manages sessions automatically.
From the <data sources> list in the top right corner, select the data source that you want to attach the file to.
To attach the file to another data source, click the data sources list and select the other one. Alternatively, in the Sessions tool window, right-click the file name, select Switch Data Source, then select the data source.
To detach a file from data source, click the data sources list and select Detach Data Source.
In the Run tool window, click on the toolbar. Alternatively, press CtrlF2 and select the process to stop.
This performs a forceful termination of the program, meaning that the usual cleanup mechanisms at the operating system, virtual machine, and application levels may be bypassed, possibly preventing the execution of shutdown hooks, proper resource releasing, logging, and so on.
When you run a file or use a run configuration, the output is shown in the tab of a Run tool window. For more information about the tool window controls, refer to Run tool window.
Right-click in the Run tool window and select Pause Output from the context menu. Use the same toggle to resume the program.
note
Only the output is suspended. Pausing the output does not affect the execution of the program.
Press Shift twice.
In the Search Everywhere dialog, type the filename.
Select a file by using Up and Down arrows.
Press CtrlShiftF10.
In the Edit Configuration window, click the Add button () and select data sources or schemes against which you want to run selected files.
Thanks for your feedback!