Structered way to generate projects/data source connections semi-automatically

Answered

We have a custom system to manage all our deployments. Part of a deployment is a bunch of databases (A sqlserver and a postgres database). Currently we surface the credentails and hostnames such that they are easy to pull, but you do have to create the datasource(s) manually in datagrip. 

Is there a way to (semi-)automatically register new datasources in datagrip? This could be either via some url-scheme that datagrip is registered to recognize or via generating project files dynamically, or maybe copy-pasting some specific json/xml string block somewhere in the UI? Kinda like how Postman can “import” a whole curl command.

I would even take generating the project files via code, if those are stable enough in the format that we can do that. 

0
2 comments

Hi Rasmus,

The main data source configurations are stored in a file named dataSources.xml, located in the hidden .idea directory of your project. Its structure looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="DataSourceManagerImpl" format="xml" multifile-model="true">
    <data-source source="LOCAL" name="MySQL" group="Connections" uuid="17082901-8d9a-4da8-80cb-dec551585063">
      <driver-ref>mysql.8</driver-ref>
      <synchronize>true</synchronize>
      <jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
      <jdbc-url>jdbc:mysql://127.0.0.1:3306</jdbc-url>
      <working-dir>$ProjectFileDir$</working-dir>
    </data-source>
    ...
    <data-source source="LOCAL" name="..." uuid="...">
      ...
    </data-source>
  </component>
</project>

There's also a dataSources.local.xml file, which stores user names, SSH, and SSL configurations.

You can try generating these files and adding them to the corresponding .idea folder in your project.

 

maybe copy-pasting some specific json/xml string block somewhere in the UI?

There's a possibility to copy/paste data sources in the Database Explorer using Ctrl+C / Ctrl+V. When you copy data sources, the clipboard content looks like this:

#DataSourceSettings#
#LocalDataSource: MySQL
#BEGIN#
<data-source source="LOCAL" name="MySQL" group="Connections" uuid="17082901-8d9a-4da8-80cb-dec551585063"><database-info product="MySQL" version="11.7.2-MariaDB-ubu2404" jdbc-version="4.2" driver-name="MySQL Connector/J" driver-version="mysql-connector-j-8.2.0 (Revision: 06a1f724497fd81c6a659131fda822c9e5085b6c)" dbms="MARIADB" exact-version="11.7.2" exact-driver-version="8.2"><extra-name-characters>#@</extra-name-characters><identifier-quote-string>`</identifier-quote-string></database-info><case-sensitivity plain-identifiers="exact" quoted-identifiers="exact"/><driver-ref>mysql.8</driver-ref><synchronize>true</synchronize><jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver><jdbc-url>jdbc:mysql://127.0.0.1:3306</jdbc-url><secret-storage>master_key</secret-storage><user-name>root</user-name><schema-mapping><introspection-scope><node kind="schema" negative="1"/></introspection-scope></schema-mapping><working-dir>$ProjectFileDir$</working-dir></data-source>
#END#

#LocalDataSource: MySQL2
#BEGIN#
<data-source ...>...</data-source>
#END#

It includes all relevant connection details, including SSH, SSL, and any other configured properties, except for the password.

0

Generate .idea/dataSources.xml and .idea/dataSources.local.xml (for shared projects)

If you use project-based setup (like with IntelliJ-style .idea projects), DataGrip stores datasource information in XML files:

  • dataSources.xml – contains shared datasource definitions (credentials excluded)
  • dataSources.local.xml – contains local (private) data like passwords

You can generate these files dynamically for each deployment. Here’s a sample minimal entry for a PostgreSQL datasource in fnf dataSources.xml:

<project version="4">  <component name="DataSourceManagerImpl">    <data-source name="My Postgres DB" uuid="1234-5678-9012-ABCD" dialect="PostgreSQL" jdbc-url="jdbc:postgresql://localhost:5432/mydb">      <driver-ref ref="postgresql"/>      <synchronize>true</synchronize>    </data-source>  </component> </project>

Then in dataSources.local.xml, store the credentials securely:

<project version="4">  <component name="DataSourceManagerImpl">    <data-source uuid="1234-5678-9012-ABCD">      <user-name>myuser</user-name>      <password>mypassword</password>    </data-source>  </component> </project>

  • Use a script or template engine to generate UUIDs per datasource.
  • This works for both SQL Server and PostgreSQL; just change the jdbc-url and dialect.
  • You can pre-bundle the proper JDBC driver in the drivers folder or let DataGrip fetch it.
0

Please sign in to leave a comment.