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.
When you create a data source, the data source's default query console is created automatically. If necessary, you can create additional query consoles.
Each console creates a new connection session. If you do not want to create new connections, enable single session mode.
All the created query consoles are located in the internal Database Consoles directory. To open this folder, open the Files tool window Alt02 and navigate to Scratches and Consoles | Database Consoles. On your machine, the query console files are stored in the consoles subdirectory of the IDE configuration directory.
The following video gives a short overview of query consoles in DataGrip.
note
In the video, the DataGrip user interface is Classic UI. Starting from version 2023.1, the New UI is available. For more information about the New UI, refer to the corresponding topic.
For more information about working with query results in query consoles, refer to Query results.
For each console, a database connection session is created automatically. You can create new connection sessions and attach your console to existing ones in the console toolbar.
In the following example, the PostgreSQL console is associated with the guest.public
schema of a data source and connected to it via the console
connection session. You can change the console
session to actor
or create a new session and attach the console to it.
The code editor is where you compose your SQL statements using the resolve modes and coding assistance features, and execute them against the associated data source.
Find the code editor toolbar controls in Code editor controls. Read more about the editor in Editor basics.
When you execute a statement, the Services tool window opens. The Services tool window displays available connection sessions, Output and Result tabs. For more information about the Services tool windows, refer to Services tool window.
If the executed statement retrieves data (for example,
SELECT
), results are displayed in the Result tab that has a title of a qualified table name. For more information about creating custom titles for result tabs, refer to Use custom titles for tabs with results.If the executed statement does not retrieve data, results are displayed in the Output tab.
Active Result tabActive Output tabCode editor.
Services tool window.
Code editor.
Services tool window.
Output and Result tabs. Output tab is active.
Right toolbar of the Output tab.
To create a console, use one of the following actions in the Database Explorer (View | Tool Windows | Database Explorer).
Click a data source and select File | New | Query Console.
Right-click a data source and select New | Query Console.
Click a data source, press AltInsert, and select Query Console.
Click a data source, press CtrlShiftF10, and select New Query Console.
Click a data source and press CtrlShift0Q.
note
New console means a new connection to a data source. You can work with just one connection for the data source and have all consoles use that same one connection. Read more about the single session mode in Single session mode.
When a console is open, you can write SQL here, run it and get the result.
In the Files tool window (View | Tool Windows | Files) , navigate to Scratches and Consoles
| Database Consoles , double-click a console file.| <data_source_name> In the Database Explorer (View | Tool Windows | Database Explorer) , click the Jump to Query Console button () and select a console that you want to open or create a new one.
Click a data source, press F4, and select the console that you need.
To rename a query console file, do one of the following:
Right-click a console tab, and select Rename File.
Open the Files tool window (View | Tool Windows | Files) and navigate to Scratches and Consoles | Database Consoles. Expand a data source group that includes your console, right-click the console file, and select Rename (ShiftF6).
In the Files tool window (View | Tool Windows | Files) , navigate to Scratches and Consoles | Database Consoles.
Right-click a data source group where you want to create a new directory, select New | Directory.
Specify a name of the directory.
Select and drag console files to the directory.
In the Move dialog, click Refactor.
In the Files tool window (View | Tool Windows | Files) , navigate to Scratches and Consoles | Database Consoles.
Expand a data source group that includes your console.
Right-click a console file and select Delete.
In the Files tool window (View | Tool Windows | Files) , navigate to Scratches and Consoles | Database Consoles.
Expand a data source group that includes your console.
Right-click a console file and select Refactor | Copy File.
In the To directory field, specify a new location for the file.
In the query console file tab, press CtrlShift0S.
In the To directory field, specify the location where you want to store the file.
To open a console file in your file browser, do one of the following:
Right-click a console tab and navigate to Open In | <file_browser>.
Open the Files tool window (View | Tool Windows | Files) and navigate to Scratches and Consoles | Database Consoles. Expand a data source group that includes your console, right-click the console file, and select Open In | <file_browser>.
In the query console toolbar, open the list of databases and schemas by clicking the <schema> button and select the one that you want to run queries against.
For more information about schemas, refer to Schemas.
Open a console and click the Open Query Execution Settings... button ().
Open settings by pressing CtrlAlt0S, navigate to Database | Query Execution. For more information about the settings, refer to Query execution.
note
For query consoles, SQL dialect is defined by a data source that is attached to the console. It can only be changed for the Oracle consoles.
Right-click the editing area and select Change Dialect (<current_dialect>) to. In the Change SQL Dialect window, select a dialect.
In a console, right-click any area and select Local History | Show History.
Right-click any area or a selection of code in the editor and select Reformat Code. Alternatively, press CtrlAlt0L.
To see the details of a file, hover over the console tab.
Press AltF1, select Files View and press Enter.
Select
INSERT
statements that you want to edit.Right-click the selection and click Edit as Table.
In the table editor that opens, make necessary changes in the table cell. Press Enter to confirm.
In the code editor context menu, use the following actions to increase your productivity:
Action | Shortcut | Description |
---|---|---|
Edit as Table | In | |
Change Dialect (<CurrentDialect>) | Change the SQL dialect. Select a dialect from the list. | |
Explain Plan |
| |
Execute | CtrlEnter | Execute the current statement or the sequence of selected statements. |
Execute to File | Execute the current statement and save results in a text file. Select the output format and specify the file location and name. | |
Run 'console [data_source]' | CtrlShiftF10 | Execute all the statements in the query console. |
Use the code editor to compose and execute your SQL statements as well as to perform other associated tasks.
Read more about the editor in Editor basics.
Icon | Action and shortcut | Description |
---|---|---|
Execute CtrlEnter | Execute selected SQL statement or statements. If nothing is selected, the current statement is executed. | |
Browse Query History CtrlAlt0E | Open a dialog that shows all the statements that you have run for the corresponding data source. See also, Use the Query History dialog. | |
View Parameters | Open or close the Parameters dialog. For more information about user parameters, refer to the Run queries topic. | |
Open Query Execution Settings | Open the Database | Query Execution section of the Settings dialog to view or edit the query execution settings. For more information about these settings, refer to Query Execution. | |
Transaction Mode and Transaction Isolation | Select the isolation level for database transactions and the way the transactions are committed.
For more information about database transaction modes and isolation, refer to Submit changes to a database. | |
Commit | (For the Manual transaction mode.) Commit the current transaction. This button is available only for the manual transaction mode. See also, transaction mode and isolation. | |
| Roll back | (For the Manual transaction mode.) Roll back changes. This button is available only for the manual transaction mode. See also, transaction modes and isolation. |
Cancel Running Statements CtrlF2 | Terminate execution of the current statement or statements. | |
File Resolve Mode | Select the resolve mode to manage the context that database objects in your code are resolved to.
For more information about the resolve modes, refer to Resolve modes. In the following video, the Once the resolve mode is changed to Script, the second | |
In-Editor Results | Toggle the display of query result within the code editor of query console. For more information about the in-editor results, refer to In-Editor Results. | |
<schema> | Switch current schema | Select the default schema or database. For PostgreSQL, Amazon Redshift, and Greenplum use the <schema> list to form the schema search path. For more information about schemas, refer to Schemas. |
<session> | Switch attached session | Select the database connection session. For more information about the database connection sessions, refer to Managing connection sessions. |
Thanks for your feedback!