The Simplest Way to Make Cloud SQL and Google Pub/Sub Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

You link cloud sql and google pub/sub by sending database state changes to message topics. This setup lets background worker apps process events without blocking main database writes. Teams use this pattern to split heavy work from fast user requests. An app writes a record to the database and publishes a message. Or a change reader watches the database log and sends messages out. Downstream services read those messages at their own pace. How permissions and data flow between cloud sql and go

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.

You link cloud sql and google pub/sub by sending database state changes to message topics. This setup lets background worker apps process events without blocking main database writes.

Teams use this pattern to split heavy work from fast user requests. An app writes a record to the database and publishes a message. Or a change reader watches the database log and sends messages out. Downstream services read those messages at their own pace.

How permissions and data flow between cloud sql and google pub/sub

Cloud SQL for PostgreSQL is a managed database service. Customers cannot make or use accounts with full superuser rights. Google keeps a system account named cloudsqladmin for full superuser tasks. Customers cannot delete or change these system roles or users.

Instead, you assign the cloudsqlsuperuser role to your admin accounts. This role gives some high privileges for setup tasks. It can create extensions, event triggers, replication users, and replication publications. The default postgres user belongs to this role and has login rights. You must set a password for postgres before you log in. The default postgres user does not hold superuser or replication attributes.

To stream database updates to a message topic, you need replication publications. A user with the cloudsqlsuperuser role sets up the publication and replication user. A worker service reads that stream and sends events to the message bus. Each service uses its own cloud role to talk to the message topic. The database checks the database user role, while Google checks topic permissions.

Common failure modes and how to scope roles

Many teams make the mistake of sharing one admin account across services. When a worker app uses the default postgres user, every table stays exposed. If a bad script runs in that worker, it can drop tables. Shared accounts also ruin your logs because every query looks identical.

Broad tokens cause another major problem between database apps and message topics. A service account with project-wide rights can read or write any topic. If an attacker grabs that key, they can inject fake events. They can also read private customer data like emails and phone numbers.

End-to-End Encryption + SQL Query Filtering: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Fix this by giving each worker its own small role. Use the cloudsqlsuperuser role only during initial setup to create publications. After setup, run your daily worker apps with a basic database user. Grant that user read rights only on the tables it needs. On the message side, limit the worker to one specific topic.

Keeping sensitive event data safe in transit

Database rows often hold private details like card numbers or national IDs. When a worker copies a full row into a message, that data spreads. Every subscriber on that topic now gets a copy of private fields. Unlogged queries during manual debugging make this risk even worse. Engineers often log in to check stuck rows and run raw queries.

You should trim event payloads before they leave the database worker. Send only the record ID or the non-sensitive fields that changed. If downstream apps need more data, let them ask for specific columns. For human debugging, apply data masking to rewrite matched fields in results. Masking replaces emails or card numbers with safe placeholder text during queries. You also need clear query logs tied to individual engineer identities.

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

Can I use a true PostgreSQL superuser to set up replication for events?

No, customers cannot get true superuser accounts in Cloud SQL for PostgreSQL. You must use an account with the cloudsqlsuperuser role to create publications.

Should my event publisher app use the default postgres user?

No, the default postgres user has broad rights to create roles and databases. Create a dedicated user with limited table permissions or specific replication rights instead.

How do I stop private fields from leaking into message topics?

Publish only record IDs or safe status fields to your message topics. For direct database checks, use dynamic masking to rewrite emails and card numbers.

The query runs. The emails stay hidden.