How to connect to Cloud SQL PostgreSQL on Google Cloud Platform from a local environment with Java

The procedure for the title did not go well and it took a long time to resolve it, so I will write it as a memorandum. Basically https://cloud.google.com/sql/docs/postgres/connect-external-app?hl=ja#java If you do as follows, it will work.

The environment I went to this time Windows Subsystem for Linux Java8 sbt is. Don't worry why you're using sbt but Java. Scala is studying.

First of all, you need to install and authenticate the Cloud SDK, but I will omit it here. Next, write the required libraries in build.sbt. Please use the latest version at that time as appropriate. The following is the latest version as of December 26, 2017.

libraryDependencies ++= Seq(
  "org.postgresql" % "postgresql" % "42.1.4",
  "com.google.cloud.sql" % "postgres-socket-factory" % "1.0.4"
)

Then you can write a Java file like the one below and do `` `sbt run``` to connect to PostgreSQL in Cloud SQL.

import java.sql.*;
public class App {
    public static void main(String[] args) throws Exception {
        Connection connection = null;
        String jdbcUrl = String.format(
          "jdbc:postgresql://google/%s?socketFactory=com.google.cloud.sql.postgres.SocketFactory&socketFactoryArg=%s",
          dbName, 
          instanceConnectionName);
        try {
            connection = DriverManager.getConnection(jdbcUrl,
                userName,
                password);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }
}

Here, instanceConnectionName is the instance connection name of Cloud SQL, and it is listed on the Cloud SQL instance list screen.

Recommended Posts

How to connect to Cloud SQL PostgreSQL on Google Cloud Platform from a local environment with Java
How to connect to Cloud Firestore from Google Cloud Functions with python code
From python to running instance on google cloud platform
Building a development environment with Maven on Google App Engine [Java]
Strategy on how to monetize with Python Java
[Latest] How to build Java environment on Ubuntu
How to build Java environment on Ubuntu (Linux)
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
How to deploy a Django application on Alibaba Cloud
How to build a Django (python) environment on docker
How to build a Python environment on amazon linux 2
How to set up a Google Colab environment with Coursera's advanced machine learning courses
A memo on how to easily prepare a Linux exercise environment
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
How to build a new python virtual environment on Ubuntu
Build an Ubuntu python development environment on Google Cloud Platform
A note on how to load a virtual environment in PyCharm
How to build a python2.7 series development environment with Vagrant
Connect to centos6 on virtualbox with ssh connection from Mac
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
A story about switching a personally developed Web service from a rental server to GCP (Google Cloud Platform)
How to quickly create a machine learning environment using Jupyter Notebook on macOS Sierra with anaconda
How to customize U-Boot with OSD335X on a custom board (memo)
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
How to make a .dylib library from a .a library on OSX (El Capitan)
How to deploy a web application on Alibaba Cloud as a freelancer
Using properties files with Flexible Environment Java 8 on Google App Engine
Everything from building a Python environment to running it on Windows
Misunderstanding on how to connect cnn
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
How to fix the shit heavy when reading Google Cloud Storage images from Django deployed on GAE
I tried to execute SQL from the local environment using Looker SDK
Steps to create a Python virtual environment with VS Code on Windows
How to install python package in local environment as a general user
Connect to postgreSQL from Python and use stored procedures in a loop.
How to draw a vertical line on a heatmap drawn with Python seaborn
How to run a quickstart for Google Cloud Text-to-Speech .NET in Unity
How to read environment variables from .env file in PyCharm (on Mac)
Copy data from Amazon S3 to Google Cloud Storage with Python (boto)
Build a "Deep learning from scratch" learning environment on Cloud9 (jupyter miniconda python3)
Summary of how to build a LAMP + Wordpress environment with Sakura VPS
How to build a sphinx translation environment
How to add a package with PyCharm
How to update Google Sheets from Python
How to use Google Assistant on Windows 10
How to search Google Drive with Google Colaboratory
A note on enabling PostgreSQL with Django
How to create a repository from media
How to test on a Django-authenticated page
Build a Python + OpenCV environment on Cloud9
How to use SQLAlchemy / Connect with aiomysql
From nothing on Ubuntu 18.04 to setting up a Deep Learning environment in Tensor
How to publish a blog on Amazon S3 with the static Blog engine'Pelican'for Pythonista
How to quickly create a morphological analysis environment using Elasticsearch on macOS Sierra
When I connect to a remote Jupyter Server with VScode, it's remote but local
WEB scraping with python and try to make a word cloud from reviews
How to run a Django application on a Docker container (development and production environment)
Instructions for connecting Google Colab. To the local runtime in a Windows environment
How to execute a query from psycopg2 built with SAM to Postgres launched with Docker
Explaining how to make LINE BOT in the world's easiest way (2) [Preparing a bot application in a local environment with Django in Python]
Build a LAMP environment on your local Docker