CloudSQLDatabaseHook

Google

Serves DB connection configuration for Google Cloud SQL (Connections of gcpcloudsqldb:// type).

View Source

Last Updated: Apr. 27, 2021

Access Instructions

Install the Google provider package into your Airflow environment.

Import the module into your DAG file and instantiate it with your desired params.

Parameters

gcp_cloudsql_conn_idstrURL of the connection
gcp_conn_idstrThe connection ID used to connect to Google Cloud for cloud-sql-proxy authentication.
default_gcp_project_idstrDefault project id used if project_id not specified in the connection URL

Documentation

Serves DB connection configuration for Google Cloud SQL (Connections of gcpcloudsqldb:// type).

The hook is a “meta” one. It does not perform an actual connection. It is there to retrieve all the parameters configured in gcpcloudsql:// connection, start/stop Cloud SQL Proxy if needed, dynamically generate Postgres or MySQL connection in the database and return an actual Postgres or MySQL hook. The returned Postgres/MySQL hooks are using direct connection or Cloud SQL Proxy socket/TCP as configured.

Main parameters of the hook are retrieved from the standard URI components:

  • user - User name to authenticate to the database (from login of the URI).

  • password - Password to authenticate to the database (from password of the URI).

  • public_ip - IP to connect to for public connection (from host of the URI).

  • public_port - Port to connect to for public connection (from port of the URI).

  • database - Database to connect to (from schema of the URI).

Remaining parameters are retrieved from the extras (URI query parameters):

  • project_id - Optional, Google Cloud project where the Cloud SQL

    instance exists. If missing, default project id passed is used.

  • instance - Name of the instance of the Cloud SQL database instance.

  • location - The location of the Cloud SQL instance (for example europe-west1).

  • database_type - The type of the database instance (MySQL or Postgres).

  • use_proxy - (default False) Whether SQL proxy should be used to connect to Cloud SQL DB.

  • use_ssl - (default False) Whether SSL should be used to connect to Cloud SQL DB. You cannot use proxy and SSL together.

  • sql_proxy_use_tcp - (default False) If set to true, TCP is used to connect via proxy, otherwise UNIX sockets are used.

  • sql_proxy_binary_path - Optional path to Cloud SQL Proxy binary. If the binary is not specified or the binary is not present, it is automatically downloaded.

  • sql_proxy_version - Specific version of the proxy to download (for example v1.13). If not specified, the latest version is downloaded.

  • sslcert - Path to client certificate to authenticate when SSL is used.

  • sslkey - Path to client private key to authenticate when SSL is used.

  • sslrootcert - Path to server’s certificate to authenticate when SSL is used.

Example DAGs

Improve this module by creating an example DAG.

View Source
  1. Add an `example_dags` directory to the top-level source of the provider package with an empty `__init__.py` file.
  2. Add your DAG to this directory. Be sure to include a well-written and descriptive docstring
  3. Create a pull request against the source code. Once the package gets released, your DAG will show up on the Registry.

Was this page helpful?