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.
Cloud SQL Proxy client: a command-line tool that establishes the proxy connection and provides secure access to your instances. When the Cloud SQL Proxy client runs, you do not need to add your IP to authorized networks or configure SSL. You can read more about the Cloud SQL Proxy in this article at cloud.google.com.
The connection is secured. To connect to your instance, you need Google Cloud SDK and the Cloud SQL Proxy client.
Cloud SQL Connector for Java: libraries for JDBC drivers by using which you can connect securely to Cloud SQL databases. These libraries are available on the Release page of the cloud-sql-jdbc-socket-factory repository at github.com.
The connection is secured. To connect to your instance, you need Google Cloud SDK and the JAR library file of the Cloud SQL Connector for Java.
Adding your network to authorized networks: adding your network to the list of authorized sources in the Google Cloud Console. After this procedure, you can connect to the PostgreSQL instance from your network by using the IP address of your instance.
The connection is not secured (no encryption). To connect to your instance, you need to add your IP address or a range of IP addresses to authorized networks.
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.
Download and extract the Google Cloud SDK archive.
Run the
gcloud init
command to initialize the SDK. Follow the installation wizard in the command prompt../google-cloud-sdk/bin/gcloud init
Step 2. Register your application for the Cloud SQL Admin API
Click the Enable the API button in the Enable the Cloud SQL Admin API section.
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.
Step 3. Configure authentication
Run the following command to authenticate the
gcloud
tool.gcloud auth loginThis 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.Run the following command to obtain credentials for the Application Default Credentials (ADC) library.
gcloud auth application-default loginThis 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.
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
Download the Cloud SQL Proxy client. See installation instructions in Install the Cloud SQL Proxy client on your local machine at cloud.google.com.
Start the proxy in the command prompt.
./cloud_sql_proxy -instances=connection_name=tcp:3306Replace the
connection_name
with the connection name of your instance. You can find this information on the Overview page of your instance. If the3306
port is already in use, change it to another port (for example, 5435).
Step 2. Create a data source
Ensure that the Cloud SQL Proxy client is running (see Step 1).
Open data source properties. You can open data source properties by using one of the following options:
Navigate to
.Press Ctrl+Alt+Shift+S.
In the Database Explorer ( ), click the Data Source Properties icon .
In the Data Sources and Drivers dialog, click the Add icon () and select your database vendor (in our case, PostgreSQL).
At the bottom of the data source settings area, click the Download missing driver files link. 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.
From the Authentication list, select User & Password.
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.
To ensure that the connection to the data source is successful, click the Test Connection link.
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.
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
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
Open data source properties. You can open data source properties by using one of the following options:
Navigate to
.Press Ctrl+Alt+Shift+S.
In the Database Explorer ( ), click the Data Source Properties icon .
In the Data Sources and Drivers dialog, click the Drivers tab.
Click the driver of your database vendor and select Duplicate. Alternatively, press Ctrl+D.
In the Name field, type a new driver's name.
Click the Advanced tab and set the following properties:
PostgreSQL:
socketFactory = com.google.cloud.sql.postgres.SocketFactoryMySQL:
socketFactory = com.google.cloud.sql.mysql.SocketFactoryMicrosoft 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.
On the General tab, click the Add icon () and select Custom JARs….
Navigate to the built or downloaded JAR library file (in our case, postgres-socket-factory-1.2.0-jar-with-dependencies.jar).
From the Class list, select the appropriate driver class (for example, org.postgresql.Driver).
Click Apply.
Step 2. Create a data source
Open data source properties. You can open data source properties by using one of the following options:
Navigate to
.Press Ctrl+Alt+Shift+S.
In the Database Explorer ( ), click the Data Source Properties icon .
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 () and select the driver that you prepared on Step 1.
In the Connection type list, select URL only.
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.
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=guestMySQL:
jdbc:mysql:///guest?cloudSqlInstance=mysql-304410:europe-west3:mysql&user=guest&password=guestMicrosoft 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.
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
Open a page of your Cloud SQL Instance.
Select the Connections tab and click Add network.
In the Network field, type your external IP address or a range of IP addresses in the CIDR notation.
Click Done and then Save.
Wait until your instance is updated.
Step 2. Create a data source
Open data source properties. You can open data source properties by using one of the following options:
Navigate to
.Press Ctrl+Alt+Shift+S.
In the Database Explorer ( ), click the Data Source Properties icon .
In the Data Sources and Drivers dialog, click the Add icon () and select your database vendor (for example, PostgreSQL).
At the bottom of the data source settings area, click the Download missing driver files link. 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.
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.
From the Authentication list, select User & Password.
In the User field, type the username that has access to the database.
In the Password field, type the user's password.
In the Database field, type the database name to which you want to connect.
To ensure that the connection to the data source is successful, click the Test Connection link.