Run a query
A query is a set of instructions that you use to work with database data. In PyCharm, you can run a query from the SQL file or a database console.
A database console is an SQL file that is already associated with a data source. It means that you can start running your queries right away.
For SQL files, you need to attach a console first. Attaching a console to the SQL file means that objects will be associated with the data source of this console and queries will be run in the context of this console.
If a file or a console has several statements, you can select whether you want to run all the statements or only an individual statement.
Run an individual statement
Open an SQL file in PyCharm.
Click the statement that you want to execute.
Press Ctrl+Enter or select Execute from the context menu.
(Optional) If the SQL file is not associated with a console, select the database console in which you want to execute the statement.
In the Statements window, press Enter to run the statement. Otherwise, navigate to another entry to run all statements.
Run a query from the editor
You can open and run an SQL file from the editor. Limitation for the SQL file size is 20 MB. When you open a file that is larger than 20 MB, you see only first 2.5 MB of the file.
Run an SQL file from the editor
Double-click an SQL file to open it in PyCharm.
Right-click the code in the editor and select Attach Console.
Select a console file that you want to use.
Click the Run icon (Shift+F10).
Select statements that you want to execute and press Enter.
Run a query from a tool window
For example, the Database tool window ( ) displays a list of all available data sources. The Project tool window ( ) includes a list of attached directories, files for extensions (GROOVY scripts), scratch files, and database consoles.
Run an SQL file for one data source
In the Database tool window, right-click a data source.
Select Run SQL Script.
In the Select Path window, navigate to the SQL file that you want to apply.
Run an SQL file for several data sources
In the Project tool window, select the necessary SQL file.
Right-click the SQL file, select Run <file_name> (or press Ctrl+Shift+F10).
In the Execution target window, select the necessary data sources.
Run a query from a database console
When you create a data source, a database console is created automatically. For more information about database consoles, see Database consoles.
Create a database console
To create a console, use one of the following actions in the Database tool window ( ).
Click a data source and select.
Right-click a data source and select.
Click a data source, press Alt+Insert, and select Console.
Click a data source, press Ctrl+Shift+F10, and select New console.
Open a console
In the tool window (), navigate to , double-click a console file.
In the Database tool window, click the Jump to console icon () and select a console.
Click a data source, press F4, and select the console that you need.
Execute an SQL statement
Place the caret within the statement.
Press Ctrl+Enter. Alternatively, click the Execute icon on the toolbar.
If you have several statements, select whether you want to execute all statements or a single statement. The suggestion list always contains an item for running all the statements.
Save a console as a file
In the console file, press N/A. Navigate to the directory where you want to store the file.