Skip to content

Cloud SQL for Postgres

Runway supports connecting to unmanaged Cloud SQL for Postgres instances from your GKE workloads. This allows you to connect workloads to an existing database in a different project.

Before configuring your GKE workload, complete the shared setup steps in the Cloud Run CloudSQL documentation:

  1. Enable PSC on your Cloud SQL instance
  2. Obtain PSC details (DNS name, service attachment link, connection name)
  3. Update the provisioner - use config/runtimes/gke/cloud-sql/unmanaged.yml instead of the cloud-run path

In the project where your Cloud SQL instance lives, grant the Cloud SQL Client role to your GKE workload’s service account.

Your workload’s service account will have the following format:

gke-<Workload ID>@<Runway Project ID>.iam.gserviceaccount.com

For example:

gke-my-service-gke@gitlab-runway-staging.iam.gserviceaccount.com

Using the gcloud CLI:

Terminal window
gcloud projects add-iam-policy-binding <DB Project ID> \
--member=serviceAccount:gke-<Runway workload ID>@<Runway Project ID>.iam.gserviceaccount.com \
--role=roles/cloudsql.client \
--condition="expression=resource.name == 'projects/<DB Project ID>/instances/<DB Instance ID>',title=access_specific_db"

Terraform example:

resource "google_project_iam_member" "runway-workload-cloudsql-client" {
project = var.project
role = "roles/cloudsql.client"
member = "serviceAccount:gke-<Runway workload ID>@<Runway Project ID>.iam.gserviceaccount.com"
condition {
title = "Cloud SQL client access for Runway workload"
expression = "resource.name == \"projects/${var.project}/instances/<DB instance ID>\""
}
}

Declare one named binding per database in your Fairway manifest, and one entry in the spec.infrastructure.cloudsqlInstances map of your deployment.yaml, keyed by the binding’s name. This works the same as for managed instances, including the deploy-time checks and the deprecated cloudsqlInstance key. Only instanceConnectionName differs:

.runway/deployment.yaml
spec:
infrastructure:
cloudsqlInstances:
users:
instanceConnectionName: project-a:us-east1:db-users-{% .Environment %}
databaseName: users
orders:
instanceConnectionName: project-b:us-east1:db-orders-{% .Environment %}
databaseName: orders

For an unmanaged instance, instanceConnectionName is <project>:<region>:<instance>. The <instance> part must contain the {% .Environment %} template, so that staging and production connect to different instances. Runway replaces it with staging or production at deploy time.

Runway runs one Cloud SQL Auth Proxy sidecar per instance and writes the connection settings into your Helm values, as described for managed instances. The proxy authenticates with IAM database authentication as your workload’s service account, so there is no password. Neither the provisioner nor Runway sets this up for an unmanaged instance. In addition to the Cloud SQL Client role granted above, do the following in the project where your Cloud SQL instance lives.

Grant the Cloud SQL Instance User role to your workload’s service account, which lets it log in to the database with IAM:

Terminal window
gcloud projects add-iam-policy-binding <DB Project ID> \
--member=serviceAccount:gke-<Runway workload ID>@<Runway Project ID>.iam.gserviceaccount.com \
--role=roles/cloudsql.instanceUser \
--condition="expression=resource.name == 'projects/<DB Project ID>/instances/<DB Instance ID>',title=access_specific_db_iam_login"

In Terraform, add a second google_project_iam_member like the one above, with role = "roles/cloudsql.instanceUser".

Enable IAM authentication on the instance by setting the cloudsql.iam_authentication database flag to on:

resource "google_sql_database_instance" "db" {
# ...
settings {
database_flags {
name = "cloudsql.iam_authentication"
value = "on"
}
}
}

Create the IAM database user for your workload’s service account:

Terminal window
gcloud sql users create gke-<Runway workload ID>@<Runway Project ID>.iam \
--instance=<DB Instance ID> \
--type=cloud_iam_service_account \
--project=<DB Project ID>

The user name is the service account’s email address without the .gserviceaccount.com suffix, and it is the username Runway configures for your application. Then, connected as a database administrator, grant the user the privileges your application needs on your database’s schemas and tables. In SQL, quote the user name, because it contains @ and .: "gke-<Runway workload ID>@<Runway Project ID>.iam".

  1. Verify instanceConnectionName (v2) or instance_connection_name (v1) is correct
  2. Check proxy sidecar logs from grafana (see logging):
    SELECT
    TimestampTime as "timestamp",
    Body as "body",
    SeverityText as "level",
    Attributes as "labels"
    FROM
    "observability"."otel_logs"
    WHERE
    (
    timestamp >= $__fromTime
    AND timestamp <= $__toTime
    )
    AND (
    labels['service.name'] = '{runway-service-id}-{staging|production}'
    )
    AND (labels['k8s.container.name'] = 'cloud-sql-proxy')
    ORDER BY
    timestamp DESC
    LIMIT
    1000
    On Runway v2, the sidecar of a named instance is called cloud-sql-proxy-<name>, so match it with labels['k8s.container.name'] LIKE 'cloud-sql-proxy%' instead.
  3. Ensure PSC endpoint is configured in provisioner