The Simplest Way to Make Cloud SQL and Gitlab Work Like They Should
Teams use cloud sql and gitlab together to run database schema updates during code deploys. Build jobs read source files from the repo and apply changes to the database. This workflow keeps database tables in sync with new app code on every release. Still, linking a runner to a managed database requires strict role and network rules. Without clear boundaries, build runners often hold too much power over production data. How cloud sql and gitlab handle permissions and data flow Data flows fro
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
Teams use cloud sql and gitlab together to run database schema updates during code deploys. Build jobs read source files from the repo and apply changes to the database.
This workflow keeps database tables in sync with new app code on every release. Still, linking a runner to a managed database requires strict role and network rules. Without clear boundaries, build runners often hold too much power over production data.
How cloud sql and gitlab handle permissions and data flow
Data flows from your code repo to the database during a pipeline run. A GitLab runner picks up a job and reads the migration files. Next, the runner opens a network path to the Google Cloud database instance. The runner logs in with a database user and runs the schema commands. After the changes finish, the runner closes the session and reports the job status.
Permissions on the database side need careful setup because of how managed roles work. Cloud SQL for PostgreSQL is a managed service, so customers cannot get true superuser attributes. Instead, you can assign the cloudsqlsuperuser role to an admin user for setup tasks. This role can create extensions that need superuser rights and set up event triggers. It can also create replication users, publications, and subscriptions for data sync.
The default postgres admin user belongs to this role and can create roles and databases. It starts without a password, so you must set one before you log in. It does not hold the raw superuser or replication attributes itself. A Google system user named cloudsqladmin holds the actual superuser rights on the instance. Customers cannot delete or change these built-in Cloud SQL system roles or users.
Common failure modes and how to scope roles
Many teams put the default postgres admin user into their GitLab variables. This is a major risk because every pipeline job gets broad database rights. If a bad script runs in a build job, it can drop tables. Shared static passwords in project settings are also hard to rotate without breaking builds.
Another failure mode is giving read and write jobs the same database user. A job that only checks schema drift does not need rights to alter tables. Unlogged queries from shared runner accounts also make incident reviews very hard. When five pipelines share one login, you cannot tell which job ran a bad query.
GitLab CI Security + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
To fix this, create separate database users for each pipeline stage. Use the default admin account only to create smaller roles with narrow permissions. Give your migration runner a role that only alters tables in one specific schema. Give test jobs a read-only role so they cannot change production records. Masking rules can also rewrite emails, phone numbers, card numbers, and national IDs in results.
Securing network paths between runners and databases
Network paths between GitLab runners and Cloud SQL also fail when left too open. Opening a database public IP to all runner ranges exposes the login port. Instead, run your build agents inside a private network that peers with Google Cloud. This keeps all schema migration traffic off the public internet during deployments.
Lock down network rules so only specific runner hosts can reach the database port. Pair private routing with distinct database users for each project repo. When you combine tight network paths with small roles, pipeline errors stay contained.
Govern statements and responses across databases and APIs
Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.
First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.
Frequently asked questions
Can a GitLab runner use a true PostgreSQL superuser in Cloud SQL?
No, customers cannot create or use accounts with true superuser attributes in Cloud SQL. The cloudsqladmin system user holds those rights, while customers use the cloudsqlsuperuser role.
Should I use the default postgres user in GitLab pipeline variables?
No, the default postgres user belongs to cloudsqlsuperuser and can create roles and databases. Create a dedicated user with narrower table rights for your GitLab runner jobs instead.
How do I stop build jobs from exposing sensitive fields in logs?
Give build jobs a restricted role and apply data masking to the query results. Masking rewrites matched fields like emails, phone numbers, and card numbers before logs print them.
Related guides
In this post