The Simplest Way to Make Cloud SQL and Firestore Work Like They Should
Teams pair cloud sql and firestore to run relational transactions alongside fast document reads. This setup lets your backend store strict financial records in SQL while serving live state to client apps. Running both databases in one project creates clear architectural wins. Yet it also doubles the identity and permission surface that your team must guard. You must manage two different role models, two network paths, and two audit streams. How permissions and data flow between cloud sql and
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 pair cloud sql and firestore to run relational transactions alongside fast document reads. This setup lets your backend store strict financial records in SQL while serving live state to client apps.
Running both databases in one project creates clear architectural wins. Yet it also doubles the identity and permission surface that your team must guard. You must manage two different role models, two network paths, and two audit streams.
How permissions and data flow between cloud sql and firestore
In a combined architecture, Cloud SQL usually holds the core source of truth. It processes structured writes, enforces foreign keys, and handles complex joins. After a transaction commits, a background worker or event function reads the new rows. That worker transforms the relational data into a flat document and writes it to Firestore. Client apps then read those documents for fast updates.
Identity handling differs between the two systems. Firestore relies on Google Cloud IAM roles and security rules to check every read and write. Cloud SQL for PostgreSQL combines Google Cloud IAM with internal database roles.
Because Cloud SQL is a managed service, customers cannot create users with true superuser attributes. A Google-managed system user named cloudsqladmin holds the actual superuser privileges on the instance. Customers cannot modify or delete these system roles. Instead, admins use the cloudsqlsuperuser role, which holds partial superuser privileges. The default postgres admin user belongs to cloudsqlsuperuser and has role creation, database creation, and login rights. It comes without a password, so you must set one before you log in.
Common failure modes in dual database setups
The biggest risk in this dual setup is a shared service account. Teams often create one broad identity for their backend app. They give that single account full write permissions in Firestore and admin rights in Cloud SQL. If an attacker steals that key, they gain control over both databases at once.
Developers also reuse the default postgres account for background sync jobs. While postgres lacks true superuser and replication attributes, it still holds broad rights across the instance. Sync workers do not need privileges to create new roles or databases. Using cloudsqlsuperuser for a simple read worker breaks the principle of least privilege.
Unlogged queries across two engines create another blind spot. When an engineer troubleshoots a broken sync, they often log in with shared credentials. Standard logs show that the shared account ran a query, but not which person sat at the keyboard. Sensitive fields like emails, phone numbers, and card numbers can spill into local consoles during these debug sessions.
End-to-End Encryption + SQL Query Filtering: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Scoping roles and network paths for safer operations
To fix these gaps, split your service accounts by task. Create one identity for the API that writes to Cloud SQL. Create a second identity for the sync worker. Give the sync worker read-only table permissions inside PostgreSQL and write permissions on specific Firestore collections.
Reserve the cloudsqlsuperuser role for rare schema migrations, extension setup, or event trigger creation. If you use logical replication to push SQL changes out, remember that cloudsqlsuperuser can create replication users, publications, and subscriptions. Daily app traffic and human readers should use restricted PostgreSQL roles with narrow table grants.
Keep network paths tight between your compute services and your data stores. Route traffic over private internal networks instead of public endpoints. Add data masking to your human query workflows so it rewrites matched fields like national IDs or card numbers in results. This keeps sensitive records safe while engineers fix sync bugs.
Add runtime guardrails and masking beside your databases and APIs
Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits beside the service as an open-source proxy, a sidecar for your database or API.
Before a query runs, hoop.dev guardrails check the statement against your rules. An AI Analyzer you configure can score intent or hold risky calls for human review. Next, hoop.dev data masking rewrites sensitive fields in responses, and each verdict is logged per listener. Explore the MIT-licensed project on GitHub.
Frequently asked questions
Should Cloud SQL or Firestore act as the primary source of truth?
Use Cloud SQL as the primary source of truth when your data needs strict schemas and multi-row transactions. Push a subset of that data to Firestore when client apps need fast document reads.
Does the default postgres user in Cloud SQL have full superuser rights?
No, customers cannot use true superuser attributes in Cloud SQL. The default postgres user belongs to cloudsqlsuperuser, which grants rights to create databases, roles, extensions, and replication setups.
How should we authenticate the sync worker between the two databases?
Give the sync worker a dedicated service account with minimal IAM permissions for Firestore writes. Pair that with a restricted PostgreSQL user that only has read rights on the required source tables.
Short role checks help teams work fast. Simple rules make each review clear. Good logs keep every team safe. Scoped keys stop bad changes early.
Teams test new role rules in staging first. Clear owner lists speed up each audit. Small role scopes cut down on risk. Regular log checks catch drift right away.
Related guides
In this post