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

Published
Length
3 min read

Luis Felipe Dias

All posts

Teams use cloud sql and keycloak to manage user logins and store identity data. Keycloak handles single sign-on while Cloud SQL stores realms, users, and client rules. This pair gives engineering teams a reliable identity store with managed database backups. You can also use Keycloak identities to control who can query your database instances. How cloud sql and keycloak share identity and data Keycloak needs a relational database to save its state, user profiles, and session data. Cloud SQL

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 keycloak to manage user logins and store identity data. Keycloak handles single sign-on while Cloud SQL stores realms, users, and client rules.

This pair gives engineering teams a reliable identity store with managed database backups. You can also use Keycloak identities to control who can query your database instances.

How cloud sql and keycloak share identity and data

Keycloak needs a relational database to save its state, user profiles, and session data. Cloud SQL for PostgreSQL serves as a managed storage engine for these records.

When a user logs in, Keycloak reads and writes data to Cloud SQL tables. At the same time, engineers need a safe way to query those same instances.

Cloud SQL is a managed service, so customers cannot create true superuser accounts. Instead, Google provides a special admin role that holds partial superuser rights.

This role lets you create extensions, event triggers, and replication users when needed. The default postgres admin user belongs to this role and can create databases.

Still, the default admin user starts with no password set. You must set a password before you can log in with that account.

A Google-managed system user holds the actual superuser rights on the database server. Customers cannot delete or change these built-in Cloud SQL system roles or users.

Common failure modes and how to scope roles

Many teams connect Keycloak to Cloud SQL using the default postgres admin account. This choice creates a major risk if an attacker steals your app settings.

Other teams let developers share one database password for manual debugging and support tasks. Shared passwords make it impossible to see which person ran a bad query.

Keycloak + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

When everyone uses the same admin role, audit logs lose their real value. You see the shared role name in the log, not the human engineer.

To fix this, create a dedicated database user just for the Keycloak app. Grant this user rights only on the specific schema that Keycloak uses.

Do not give the Keycloak app user the ability to create new roles. Keep the built-in admin role locked away for rare setup tasks only.

For human engineers, map Keycloak groups to separate database roles with narrow rights. Give most developers a read-only role so they cannot drop tables by mistake.

Securing network paths and audit trails

Network setup is another spot where database security often breaks down in practice. Teams sometimes open public IP addresses to let remote workers reach the database.

Public endpoints expose your database login prompt to the entire internet right away. Instead, place your Cloud SQL instance on a private network with strict rules.

Allow network traffic only from your Keycloak containers and approved internal subnets. Next, make sure you turn on database audit logging for all user queries.

Audit logs show you every schema change and data read across your tables. Pair these logs with clear user roles to keep your security reviews simple.

Also, review your sensitive tables that hold emails, phone numbers, or national IDs. Limit which database roles can read those columns during routine support work.

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

Does Keycloak need a database superuser account on Cloud SQL?

No, Keycloak only needs standard rights to create and update tables in its schema. In fact, Cloud SQL does not let customers use true superuser accounts anyway.

How should we handle the default postgres user in Cloud SQL?

Set a strong password on the default postgres user right after creating the instance. Store that password in a secret manager and use scoped roles for daily work.

Can we map Keycloak groups to different PostgreSQL permissions?

Yes, you can assign users to groups like read-only or admin inside Keycloak. Then map those groups to matching PostgreSQL roles when users start a session.

The query runs. The emails stay hidden.