Skip to content

Cloud SQL for Postgres

Runway supports provisioning and connecting to managed Cloud SQL for Postgres instances from your GKE workloads. Unlike unmanaged instances, Runway owns the lifecycle of the database — you do not need to enable PSC or obtain PSC details.

File an MR to add your database to config/runtimes/gke/cloud-sql/managed.yml in the Runway provisioner:

config/runtimes/gke/cloud-sql/managed.yml
- name: my-service-db
provider: GCP
db_version: "POSTGRES_18"
region: us-east1
zone: us-east1-b
env_configuration:
staging:
instance_type: 2xsmall
disk_size_gb: 10
backup_configuration:
transaction_log_retention_days: 2
retained_backups_count: 3
production:
instance_type: xsmall
disk_size_gb: 10
backup_configuration:
transaction_log_retention_days: 7
retained_backups_count: 7
labels:
owner_email_handle: my-team
department: eng-infra
department_group: eng-infra-my-group
product_category: my-category

The name you choose here is your database name and is referenced in the steps below.

Each environment under env_configuration also accepts prevent_db_deletion, which defaults to true and keeps the instance from being deleted. Removing an instance, or one of its environments, takes two MRs: first set prevent_db_deletion: false for the environment(s) you are removing, merge, and let the apply run; then, in a follow-up MR, remove the entry (or the environment key) from managed.yml together with any postgres_instances references to it in workloads.yml. An MR that removes the entry while prevent_db_deletion is still true on the default branch fails inventory validation. See the managed Cloud SQL schema for all the available per-environment options.

The application credentials (username and password) created for the app should be stored in Vault by the app owner and exposed to the workload as environment variables via workloadSecrets in gke-service.yaml — choose whatever variable names make sense for your application. See secrets management for details.

Members of a Google group can query the database in Cloud SQL Studio using IAM authentication, with no password to share.

Add it to the relevant environment in managed.yml:

config/runtimes/gke/cloud-sql/managed.yml
- name: my-service-db
...
env_configuration:
staging:
iam_groups_ro:
- my-team@gitlab.com

Members get SELECT on every table in every database on that instance and nothing else, writes and schema changes are rejected. Because access follows group membership, adding or removing someone needs no MR; changes take about 15 minutes to reach Cloud SQL.

In config/runtimes/gke/workloads.yml, add a postgres_instances key to your workload entry with the database name from the previous step:

config/runtimes/gke/workloads.yml
your-service-gke:
postgres_instances:
- my-service-db

This grants the GKE workload’s service account the IAM permissions required to connect to Cloud SQL.

On Runway v2, two manifests describe the database. The Fairway manifest declares which databases your chart needs, and the Runway manifest says which Cloud SQL instance provides each of them.

Declare the databases in your Fairway manifest

Section titled “Declare the databases in your Fairway manifest”

Declare one named binding per database under spec.infrastructure.postgresqlBindings:

.runway/fairway.yaml
spec:
infrastructure:
postgresqlBindings:
- name: main
presence: REQUIRED
- name: workflow
presence: OPTIONAL

The name of a binding is the key that joins the two manifests. Runway passes it through unchanged. It must be a DNS label of at most 36 characters, and unique among your bindings. presence is either REQUIRED or OPTIONAL.

Reference the instances in your Runway manifest

Section titled “Reference the instances in your Runway manifest”

Add one entry per binding to the spec.infrastructure.cloudsqlInstances map in your deployment.yaml, keyed by the binding’s name:

.runway/deployment.yaml
apiVersion: runway/v2
kind: RunwayManifest
metadata:
name: your-service-gke
spec:
infrastructure:
cloudsqlInstances:
main:
instanceConnectionName: us-east1:my-service-db
databaseName: my_service
workflow:
instanceConnectionName: us-east1:my-service-workflow-db
databaseName: workflow

Each key is the name of the Fairway binding the instance provides, and follows the same rules: a DNS label of at most 36 characters. Each value has these fields:

Field Description
instanceConnectionName <region>:<name>, where <name> is the database name from managed.yml. Runway adds the gitlab-runway-<env> project for the environment it deploys to.
databaseName The logical Postgres database to connect to.

Several entries may point at the same instance with different databaseName values. A manifest can declare at most 100 instances.

runwayctl checks the two manifests against each other at deploy time:

Fairway binding Matching cloudsqlInstances entry Result
REQUIRED yes wired
REQUIRED no deploy fails
OPTIONAL yes or no wired when set
not declared yes deploy fails

All mismatches are reported together, and nothing is deployed until they are fixed.

Before cloudsqlInstances, a Runway manifest declared a single instance with the cloudsqlInstance key:

.runway/deployment.yaml
spec:
infrastructure:
cloudsqlInstance:
instanceConnectionName: us-east1:my-service-db
databaseName: my_service

cloudsqlInstance is kept for backwards compatibility only, and cannot be combined with cloudsqlInstances. How Runway uses it depends on the Fairway manifest:

Fairway manifest Result
The deprecated, unnamed spec.infrastructure.postgresql wired to values.infrastructure.postgresql
postgresqlBindings with a single binding wired to that binding, with a warning
postgresqlBindings with several bindings deploy fails

The single-binding case lets you migrate the Fairway manifest to postgresqlBindings before the Runway manifest. Runway uses the binding’s name, as if you had written cloudsqlInstances with that key, and logs a warning asking you to move the instance there:

WARNING: spec.infrastructure.cloudsql_instance is deprecated; binding it to the application's only PostgreSQL binding "postgresql". Move it to spec.infrastructure.cloudsql_instances[postgresql] instead.

New services should use postgresqlBindings and cloudsqlInstances. cloudsqlInstances, in turn, requires the Fairway manifest to declare postgresqlBindings.

cloudsqlInstance and cloudsqlInstances are only meaningful on GKE. An EKS deployment ignores them with a warning; use RDS there instead.

Runway runs one Cloud SQL Auth Proxy sidecar per binding, named cloud-sql-proxy-<name>. With the scalar form, the deprecated cloudsqlInstance gets a single sidecar named cloud-sql-proxy. The proxies listen on localhost, on sequential ports starting at 5432, in the alphabetical order of the binding names. Adding or removing an instance can therefore change the ports of the others, so read the port from your configuration instead of hard-coding it.

Runway writes the connection settings into your Helm values, so you do not configure them yourself:

  • For each binding, values.infrastructure.postgresqlBindings[<name>] gets the host (127.0.0.1), port, databaseName and username.
  • With the scalar form, values.infrastructure.postgresql gets the same fields.

The proxy authenticates with IAM database authentication as your workload’s service account, so there is no password and no secret to manage. Runway sets username to the workload’s IAM database user, gke-<workload ID>@<Runway Project ID>.iam, for example gke-my-service-gke@gitlab-runway-staging.iam.

Everything else on a binding, such as clientName, maxConns or queryExecMode, is yours to set in your chart’s values and is preserved. If you set host, port, databaseName, username, secretRef, poolHost or poolPort, Runway overwrites them and logs a warning naming each field.

The provisioner creates a GitLab project on ops.gitlab.net for every Postgres instance. Each project has a scheduled pipeline that restores the latest backup to a separate Cloud SQL instance and, optionally, runs a data validation query against the restored database.

For production Cloud SQL instances, an alert will be fired if a restore validation pipeline was not triggered in the last 24 hours or if a pipeline has started but not completed within 2 hours.

The data validation query is optional and is configured in the env_configuration.<env>.backup_configuration.restore_validation field:

  • Omitted entirely - the scheduled pipeline still runs and restores the latest backup, but no data validation query is executed.
  • Set with database, user, vault_secret_name, vault_secret_key, and query - the restore test runs and the validation query is executed against the restored database.

Unlike the Cloud Run runtime, managed Cloud SQL on GKE does not auto-create the validation user. You must supply credentials for a user that exists in the source database with privileges to run the validation query. The password is read from a Vault KV v2 secret, where:

  • The secret name (path) is env/<environment>/service/<runway_service_id>/<vault_secret_name>.
  • <vault_secret_key> is the key inside that secret’s data whose value holds the password.

runway_service_id is auto-derived from the workload that owns this Cloud SQL instance - you only need to supply vault_secret_name (the last path segment) and vault_secret_key (the key within the secret).

When a validation query is provided, it should return 1 row of at least 2 columns to pass validation. The two columns can be data of any kind and will be printed in the CI job. Users can generate summary statistics and expose them through the two columns (see example below). The query should return no rows if the data is invalid. An alert will be fired if the validation query fails on production.

config/runtimes/gke/cloud-sql/managed.yml
- name: runway-db-foo
...
env_configuration:
staging:
backup_configuration:
restore_validation:
database: runway
user: validator
vault_secret_name: cloudsql-restore-validation
vault_secret_key: password
query: "SELECT COUNT(*) AS row_count, MAX(timestamp_column) AS most_recent_timestamp FROM your_table WHERE timestamp_column >= NOW() - INTERVAL '1 DAY' LIMIT 1;"

Removing a managed instance takes two provisioner MRs, because the instance has deletion protection enabled by default.

  1. Disable deletion protection. In config/runtimes/gke/cloud-sql/managed.yml, set prevent_db_deletion: false for every environment of the instance. Merge the MR and let the apply run. An MR that removes the instance while prevent_db_deletion is still true (or unset) on the default branch fails inventory validation.

    config/runtimes/gke/cloud-sql/managed.yml
    - name: runway-db-foo
    ...
    env_configuration:
    staging:
    prevent_db_deletion: false
    production:
    prevent_db_deletion: false
  2. Remove the instance. In a follow-up MR, remove the entry from managed.yml together with the instance name in the postgres_instances list of your workload in config/runtimes/gke/workloads.yml. Stop your service from connecting to the instance before you merge. Applying the MR destroys the instance, its networking and IAM bindings, and its restore validation resources, including the GitLab project on ops.gitlab.net.

  3. Ask an SRE to clean up the restore validation metrics. The restore validation pipeline pushes its metrics to the Pushgateway on blackbox-01-inf-gprd, which keeps the last pushed values indefinitely. Once the instance is gone, nothing updates them and the NoRestorePipelineStarted alert fires for it 24 hours after the last pipeline ran. Ask the Runway team for an SRE to delete the instance’s runway_csql_restore group. The steps are in the Pushgateway runbook. The group is job="runway_csql_restore" with tier="db", type="postgres" and resource set to the instance name.

    For example, on blackbox-01-inf-gprd, replace <resource> with the value of the resource label (such as runway-db-foo-production-us-east1):

    Terminal window
    curl -X DELETE http://127.0.0.1:9091/metrics/job/runway_csql_restore/tier/db/type/postgres/resource/<resource>