Use database CLI tools
Almost every database vendor has its own Command-Line Interface (CLI) tool. The following list shows you a couple of examples.
PostgreSQL:
psqlOracle:
sqlplus(SQL Command Line for SQL*Plus)MySQL:
mysqlMicrosoft SQL Server:
sqlcmdSQLite:
sqlite3
To use these tools in DataGrip, you need to add them as external tools. For more information about how to work with external tools, refer to the External tools topic.
In the majority of cases, these tools support two types of commands: standard and meta. Standard commands are SELECT, CREATE, UPDATE, and other ordinary SQL commands. Meta-commands use special syntax that is specific for every tool. For example, in SQLite, except for ordinary SQL statements, you can use dot-commands. These dot-commands are used to change the output format of queries or to execute certain prepackaged query statements. When you issue the dot-command, sqlite3 uses its own interpretation of the command and runs it on a database.
Other tools have different meta-commands. For example, sqlplus has DESCRIBE; psql has backslash directives like \dD; in sqlcmd, you can use :r Script.sql to load a script file. These commands are not standard SQL commands that your database would easily understand. They need to be translated. CLI tools do this translation.
DataGrip supports syntax highlighting for meta-commands but not the translation logic. It means that you can open an SQL script with meta-commands in the editor, but you need an external tool to run the script. By default, meta-commands are highlighted in green.


Running CLI tools
In this topic, we use sqlite3 and psql as examples. You can create similar configurations for other tools.
For this tutorial, we will use the sqlite.db database file for sqlite3 and the PostgreSQL database running locally for psql. And we will use the following scripts in the SQL files:
script_sqlite3.sql
script_psql.sql
To run the script, you need to create a configuration for a third-party tool. In our case, sqlite3 or psql. This configuration will pass contextual information from your project to the tool as command-line arguments and display the output in the Run tool window.
Step 1. Create an external tool configuration
Open settings by pressing Ctrl+Alt+S and navigate to .
Click
Add and specify the following settings:
Name: the name of the tool that will be displayed in the DataGrip interface. For example,
sqlite3.Group: the name of the group to which the tool belongs. You can select an existing group or type the name of a new group.
Description: a meaningful description of the tool. For example,
Command-line tool for SQLite.Program: the path to the application executable file. For example, ~/files/sqlite/sqlite3.
Arguments: the arguments passed to the executable file, as you would specify them on the command line.
Working directory: the path to the current working directory from which the tool is executed. For example, you can point this field to a folder with the database file (~/files/sqlite).

In this case,
sqlite3will be run withsqlite.db ".read '$FilePath$'"arguments.Name: the name of the tool that will be displayed in the DataGrip interface. For example,
psql.Group: the name of the group to which the tool belongs. You can select an existing group or type the name of a new group.
Description: a meaningful description of the tool. For example,
Command-line tool for PostgreSQL.Program: the path to the application executable file. For example, /Library/PostgreSQL/bin/psql.
Arguments: the arguments passed to the executable file, as you would specify them on the command line.
Working directory: the path to the current working directory from which the tool is executed. For example, /Library/PostgreSQL/bin.

In this case,
psqlwill be run with-h localhost -p 54333 -U guest -d guest -f "$FilePath$"arguments.You can use macros that can refer to the project name, the current file path, and so on. Clicking
Insert Macros… opens the Macros dialog that lists all available macros and their values.
Click OK to add the tool and then apply the changes.
Step 2. Run the created configuration
Double-click the script file to open it in the editor.
Press Ctrl+Shift+A, start typing
External Tools, then select the action from the list. Press Ctrl+Shift+A to open the Search Everywhere popup, start typingExternal Toolsand select it from the list.Open the Files tool window by clicking
Files on the main window toolbar. Alternatively, press Alt+2.
In the Settings dialog (Ctrl+Alt+S) , select Keymap.
DataGrip displays the output in the Run tool window.







