Connecting and querying multiple databases within JetBrains DataGrip optimizes your workflow by consolidating your database management into a single interface. Whether you need to compare schemas, migrate data, or run cross-database queries, DataGrip provides the tools to handle multiple connections simultaneously.
Here is a step-by-step guide to setting up and querying multiple databases in DataGrip. Step 1: Connect to Multiple Database Instances
To work with multiple databases, you must first add them as separate data sources in your DataGrip workspace.
Open the Database Tool Window: Go to View > Tool Windows > Database (or use the shortcut Alt + 1 / Cmd + 1).
Add a New Data Source: Click the + (New) icon in the top-left corner of the Database tool window.
Select Your DBMS: Choose Data Source and select your specific database management system (e.g., PostgreSQL, MySQL, SQL Server, Oracle). Configure Connection Settings:
Enter your host, port, user credentials, and the default database name. Click Download Word Drivers if prompted by DataGrip.
Test and Save: Click Test Connection to verify the credentials, then click OK.
Repeat: Follow these steps for every additional database instance you need to connect to, regardless of whether they are on the same server or different cloud providers. Step 2: Manage Visible Schemas and Databases
By default, DataGrip might only display the main database you specified during setup. To see all available databases and schemas within a connection:
Right-click on your database connection in the Database tool window.
Navigate to Tools > Manage Shown Schemas… (or click the database/schema counter text next to the connection name).
Check the boxes for all the specific databases and schemas you want to visible in your sidebar. Click OK to refresh the tree view. Step 3: Querying Multiple Databases (Same Server Instance)
If your databases live on the same server instance (for example, two different databases inside a single PostgreSQL or SQL Server instance), you can query them inside a single console using fully qualified names.
Open a new SQL Console (Ctrl + Shift + F10 / Cmd + Shift + F10) on your target data source.
Write a query that references the database, schema, and table explicitly. Syntax Example (SQL Server / PostgreSQL):
SELECT e.employee_id, e.employee_name, p.payroll_amount FROM ProductionDB.dbo.Employees e JOIN FinanceDB.dbo.Payroll p ON e.employee_id = p.employee_id; Use code with caution. Step 4: Querying Across Different Server Instances
If you need to query databases that reside on entirely different servers or use different engines (e.g., joining a MySQL table with a PostgreSQL table), native SQL cannot do this directly because standard consoles bind to one driver session.
To bridge completely separate servers, use one of the following methods:
Database Link / Foreign Data Wrappers (FDW): If you use PostgreSQL, setup postgres_fdw on one server to link the remote server. DataGrip will read the foreign tables natively, allowing you to run standard JOIN queries through a single console.
Target Switching: If you do not need to join the data in a single query but want to run the same script across different databases, use the Target Switcher dropdown menu at the top-right corner of your SQL console window. This allows you to quickly change the execution context of your script from Database A to Database B with one click. Step 5: Compare Structures and Data
DataGrip includes built-in tools to compare your newly connected databases:
Select two databases or schemas in the Database tool window using Ctrl (or Cmd).
Leave a Reply