DataGrip 2023.1 Help

Connect to Google Cloud SQL instances

Within the framework of this topic, we are going to use a PostgreSQL instance on the Google Cloud Platform. But you can use the same instructions to connect to MySQL and Microsoft SQL Server instances of Google Cloud SQL.

If you have not created the instance yet, see Creating instances at cloud.google.com.

From DataGrip, you can connect to your instances by using the following options.

Configuring Google Cloud SDK

This configuration procedure is common for the Cloud SQL Proxy client and the Cloud SQL Connector for Java. If you plan to add your network to the list of authorized networks, you can skip this part.

To complete the connection's configuration, you need Google Cloud SDK.

Google Cloud SDK includes the gcloud utility that manages authentication and interactions with the Google Cloud Platform API. For more information about gcloud, see the gcloud reference at cloud.google.com.

Step 1. Installing Google Cloud SDK

For more information about the installation process, see the Installing Google Cloud SDK guide at cloud.google.com. This procedure provides a summary of steps from the guide.

  1. Download and extract the Google Cloud SDK archive.

  2. Run the gcloud init command to initialize the SDK. Follow the installation wizard in the command prompt.

    ./google-cloud-sdk/bin/gcloud init
    Run gcloud init to initialize the SDK:

Step 2. Register your application for the Cloud SQL Admin API

  1. Click the Enable the API button in the Enable the Cloud SQL Admin API section.

  2. Select your project in the drop-down list and click Continue.

    You must enable the Cloud SQL Admin API for each database project if you want to connect to the database in this project. For example, one project uses PostgreSQL, another project uses MySQL. You need to enable the API for both projects.

    Cloud SQL Admin API

Step 3. Configure authentication

  1. Run the following command to authenticate the gcloud tool.

    gcloud auth login

    This command obtains your credentials and stores them in ~/.config/gcloud/. Now you can run gcloud commands from your terminal, and it will find your credentials automatically.

    Authenticate the gcloud tool
  2. Run the following command to obtain credentials for the Application Default Credentials (ADC) library.

    gcloud auth application-default login

    This command obtains your credentials for the ADC library. Now, any code or SDK that is running on your computer will be able to find the credentials automatically.

    Authenticate the connection to the Cloud SQL server

Configuring connections with the Cloud SQL Proxy client

The Cloud SQL Proxy client communicates with your database through a secure tunnel. Read more about the client in About the Cloud SQL Proxy at cloud.google.com.

Step 1. Run the Cloud SQL Proxy client

  1. Download the Cloud SQL Proxy client. See installation instructions in Install the Cloud SQL Proxy client on your local machine at cloud.google.com.

  2. Start the proxy in the command prompt.

    ./cloud_sql_proxy -instances=connection_name=tcp:3306

    Replace the connection_name with the connection name of your instance. You can find this information on the Overview page of your instance. If the 3306 port is already in use, change it to another port (for example, 5435).

    The connection name of your instance on the Overview page

Step 2. Create a data source

  1. Ensure that the Cloud SQL Proxy client is running (see Step 1).

  2. Open data source properties. You can open data source properties by using one of the following options:

    • Navigate to File | Data Sources.

    • Press Control+Alt+Shift+S.

    • In the Database Explorer ( View | Tool Windows | Database Explorer), click the Data Source Properties icon The Data Source Properties icon.

  3. In the Data Sources and Drivers dialog, click the Add icon (The Add icon) and select your database vendor (in our case, PostgreSQL).

  4. Check if there is a Download missing driver files link at the bottom of the data source settings area. As you click this link, DataGrip downloads drivers that are required to interact with a database. The IDE does not include bundled drivers in order to have a smaller size of the installation package and to keep driver versions up-to-date for each IDE version.

    You can specify your drivers for the data source if you do not want to download the provided drivers. For more information about creating a database connection with your driver, see Add a user driver to an existing connection.

  5. From the Authentication list, select User & Password.

  6. Specify connection details.

    • Host: 127.0.0.1. You are connecting to a local machine because you use the Cloud SQL Proxy client.

    • Port: 3306, or any other port that you selected for the proxy (for example, 5435).

    • User: a user who has permission to access the database.

    • Password: password of a user.

    • Database: a database to which you want to connect.

  7. To ensure that the connection to the data source is successful, click the Test Connection link.

  8. (Optional) If you are connecting to a data source that contains a lot of databases and schemas, in the Schemas tab, select the schemas that you need to work with.

  9. Click OK to create the data source.

  10. Find your new data source in Database Explorer.

    • To write and run queries, open the default query console by clicking the data source and pressing F4.

    • To view and edit data, use Data editor.

    • To learn how to work with database objects in DataGrip, see Database objects.

step-4-run-the-cloud-sql-proxy-client

Configuring connections with the Cloud SQL Connector for Java

The Cloud SQL Connector for Java is a set of libraries for MySQL, Microsoft SQL Server, and PostgreSQL JDBC drivers by using which you can connect to a Cloud SQL database without adding your IP to the list of authorized networks or SSL certificates. Libraries of the Cloud SQL Connector for Java are distributed as a source code on github.com. You can build this source code by using Maven or download the built JAR file from the Release page.

To build all libraries with Maven, clone the cloud-sql-jdbc-socket-factory repository, and run the following command from the cloned repository directory.

mvn -P jar-with-dependencies clean package -DskipTests

This connection type uses application default credentials and the Cloud SQL Connector for Java. Ensure that you obtained credentials for the Application Default Credentials (ADC) library. For more information about the ADC library authentication, see Step 3. Configure authentication.

Step 1. Add the Cloud SQL Connector to the driver

  1. Download the necessary JAR file from the Release page of the cloud-sql-jdbc-socket-factory repository at github.com.

    • PostgreSQL: postgres-socket-factory-<version>-jar-with-dependencies.jar

    • MySQL: mysql-socket-factory-<version>-jar-with-dependencies.jar

    • Microsoft SQL Server: cloud-sql-connector-jdbc-sqlserver-<version>-jar-with-dependencies.jar

  2. Open data source properties. You can open data source properties by using one of the following options:

    • Navigate to File | Data Sources.

    • Press Control+Alt+Shift+S.

    • In the Database Explorer ( View | Tool Windows | Database Explorer), click the Data Source Properties icon The Data Source Properties icon.

  3. In the Data Sources and Drivers dialog, click the Drivers tab.

  4. Click the driver of your database vendor and select Duplicate. Alternatively, press Control+D.

  5. In the Name field, type a new driver's name.

  6. Click the Advanced tab and set the following properties:

    • PostgreSQL:

      socketFactory = com.google.cloud.sql.postgres.SocketFactory
    • MySQL:

      socketFactory = com.google.cloud.sql.mysql.SocketFactory
    • Microsoft SQL Server:

      socketFactoryClass = com.google.cloud.sql.sqlserver.SocketFactor socketFactoryConstructorArg = <connection_name>

      , where <connection_name> is a connection name of your instance in Cloud SQL (for example, sql-server-307:europe-west3:db). You can find this information on the Overview page of your instance.

  7. On the General tab, click the Add icon (The Add icon) and select Custom JARs….

  8. Navigate to the built or downloaded JAR library file (in our case, postgres-socket-factory-1.2.0-jar-with-dependencies.jar).

  9. From the Class list, select the appropriate driver class (for example, org.postgresql.Driver).

  10. Click Apply.

    db_add_the_cloud_sql_connector_to_the_postgresql_driver

Step 2. Create a data source

  1. Open data source properties. You can open data source properties by using one of the following options:

    • Navigate to File | Data Sources.

    • Press Control+Alt+Shift+S.

    • In the Database Explorer ( View | Tool Windows | Database Explorer), click the Data Source Properties icon The Data Source Properties icon.

  2. In the Data Sources and Drivers dialog, click the Drivers tab, select the driver that you prepared on Step 1, and click the Create Data Source link.

    Alternatively, click the Add icon (The Add icon) and select the driver that you prepared on Step 1.

  3. In the Connection type list, select URL only.

  4. In the URL field, paste the URL in the following format:

    • PostgreSQL:

      jdbc:postgresql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&user=<USER_NAME>&password=<USER_PASSWORD>
    • MySQL:

      jdbc:mysql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&user=<USER_NAME>&password=<USER_PASSWORD>
    • Microsoft SQL Server:

      jdbc:sqlserver://;databaseName=<DATABASE_NAME>;user=<USER_NAME>;password=<USER_PASSWORD>

    , where

    • DATABASE_NAME: the database name to which you want to connect.

    • INSTANCE_CONNECTION_NAME: the instance connection name. You can find this information on the Overview page of your instance.

      The connection name of your instance on the Overview page
    • USER_NAME: a username that has access to the database.

    • USER_PASSWORD: a user's password.

    Note that you do not need to specify the host. Due to the properties that you set for the driver, the IP address of your instance will be detected automatically.

    Your URL might look something like this:

    • PostgreSQL:

      jdbc:postgresql:///guest?cloudSqlInstance=crucial-study-301709:us-central1:postgres&user=guest&password=guest
    • MySQL:

      jdbc:mysql:///guest?cloudSqlInstance=mysql-304410:europe-west3:mysql&user=guest&password=guest
    • Microsoft SQL Server:

      jdbc:sqlserver://;databaseName=guest;user=sqlserver;password=guest

    Alternatively, in the Connection type list, select Default and enter your connection details in the available fields.

    cloudSqlInstance=crucial-study-301709

Adding your network to authorized networks

To complete this part of the guide, you need to know your external IP address or a range of IP addresses. Use the CIDR notation (for example, 192.168.100.14/24).

Step 1. Add your network to authorized networks

  1. Open a page of your Cloud SQL Instance.

  2. Select the Connections tab and click Add network.

  3. In the Network field, type your external IP address or a range of IP addresses in the CIDR notation.

  4. Click Done and then Save.

    Wait until your instance is updated.

    Add your network to authorized networks

Step 2. Create a data source

  1. Open data source properties. You can open data source properties by using one of the following options:

    • Navigate to File | Data Sources.

    • Press Control+Alt+Shift+S.

    • In the Database Explorer ( View | Tool Windows | Database Explorer), click the Data Source Properties icon The Data Source Properties icon.

  2. In the Data Sources and Drivers dialog, click the Add icon (The Add icon) and select your database vendor (for example, PostgreSQL).

  3. Check if there is a Download missing driver files link at the bottom of the data source settings area. As you click this link, DataGrip downloads drivers that are required to interact with a database. The IDE does not include bundled drivers in order to have a smaller size of the installation package and to keep driver versions up-to-date for each IDE version.

    You can specify your drivers for the data source if you do not want to download the provided drivers. For more information about creating a database connection with your driver, see Add a user driver to an existing connection.

  4. In the Host field, type the IP address of your PostgreSQL instance. You can find this IP address on the overview page of your instance.

    IP address of your PostgreSQL instance
  5. From the Authentication list, select User & Password.

  6. In the User field, type the username that has access to the database.

  7. In the Password field, type the user's password.

  8. In the Database field, type the database name to which you want to connect.

  9. To ensure that the connection to the data source is successful, click the Test Connection link.

  10. (Optional) If you are connecting to a data source that contains a lot of databases and schemas, in the Schemas tab, select the schemas that you need to work with.

  11. Click OK to create the data source.

  12. Find your new data source in Database Explorer.

    • To write and run queries, open the default query console by clicking the data source and pressing F4.

    • To view and edit data, use Data editor.

    • To learn how to work with database objects in DataGrip, see Database objects.

Connect to Cloud SQL with authorized network
Last modified: 21 February 2023