The Simplest Way to Make Cloud SQL and Terraform Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Using cloud sql and terraform lets teams build and update managed databases from code. This pair tracks database instances, network paths, and user roles in version control. Teams do not need to click through cloud consoles to make changes. Every update goes through code review before it hits your live database. Still, managing database users and day-to-day queries brings clear security risks. How cloud sql and terraform handle roles and network paths Terraform talks to the Google Cloud API

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.

Using cloud sql and terraform lets teams build and update managed databases from code. This pair tracks database instances, network paths, and user roles in version control.

Teams do not need to click through cloud consoles to make changes. Every update goes through code review before it hits your live database. Still, managing database users and day-to-day queries brings clear security risks.

How cloud sql and terraform handle roles and network paths

Terraform talks to the Google Cloud API to create and change database instances. An identity with cloud permissions runs the plan and applies the new state. During this run, the tool sets up private networks, disk sizes, and backups.

Database user setup on Cloud SQL for PostgreSQL works in a specific way. Because it is a managed service, customers cannot create or use true superuser accounts. Instead, a system account managed by Google holds the full superuser rights. Customers cannot delete or change these system roles or users.

When you set up admin accounts, they get a custom cloud admin role. This role gives some high privileges, like creating extensions that need superuser rights. It can also set up event triggers, replication users, and replication publications. The default postgres user belongs to this role and can create roles and databases. Still, this default user starts without a password, so you must set one first. It also lacks true superuser or replication attributes.

Common failure modes in database automation

Putting database users in code often leads to shared logins and leaked secrets. If you create database passwords in code, those passwords sit in the state file. Anyone who can read that state file can read the raw database passwords. Teams then share the default admin login to run quick fixes or check data.

Shared accounts break your audit trail. When five engineers use the same postgres login, logs show one user name. You cannot tell who ran a bad update or read private customer tables. Broad service accounts also create risk if a build server leaks its keys.

Terraform Security (tfsec, Checkov) + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

To fix this, keep state files in locked storage with strict read rules. Only let the build pipeline change the cloud instances. Do not use code to hand out shared static passwords to human engineers. Give the automation tool only the cloud permissions it needs for its job. Keep network paths closed to the public internet and use private IP routing.

Scoping day-to-day database sessions

Provisioning the instance is only the first step of database management. Engineers and apps still need to log in and run queries every day. Using the same admin role for both code runs and daily work is risky.

Split your setup duties from your daily query duties. Let your code pipeline manage the instance settings, networks, and base roles. For human access, tie each session to a real person instead of a shared role. Scope read and write rules to the exact tables each team needs.

When teams run queries on live data, protect sensitive columns at runtime. Data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in results. This lets developers check app bugs without exposing raw customer details in their query output.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

Does the default postgres user in Cloud SQL have full superuser rights?

No, customers cannot get true superuser attributes in Cloud SQL for PostgreSQL. The default postgres user gets a custom cloud role that can create roles, databases, and extensions. A system user managed by Google holds the actual superuser rights.

Why is managing database passwords in Terraform risky?

Terraform stores resource details in its state file in plain text. If you set up user passwords in code, those secrets live in that state file. Anyone with read access to the state backend can take those passwords.

Should engineers use the Terraform service account to run SQL queries?

No, build accounts should only run automated code changes from your pipeline. Human engineers should log in with their own identities for clear audit logs. Mixing human queries with pipeline accounts hides who made specific data changes.

The query runs. The emails stay hidden.