This guide explains how to use pgstream with Google CloudSQL for Postgres, covering snapshots and replication. 👉 Throughout this guide:Documentation Index
Fetch the complete documentation index at: https://xata.io/docs/llms.txt
Use this file to discover all available pages before exploring further.
pgstreamsourcerefers to the user provided in the pgstream source URL.pgstreamtargetrefers to the user provided in the pgstream target URL.
Snapshots
From CloudSQL Postgres (Snapshots)
Quick Checklist
-
Create a source user (
pgstreamsource) with access to required schemas/tables. -
Decide how to handle roles:
- Disabled → no special config.
- Without passwords → no special config.
- With passwords → enable
cloudsql.pg_authid_select_roleflag.
- Update YAML config with correct snapshot settings.
Steps
-
User privileges
Ensure the
pgstreamsourceuser (from the pgstream source URL) can access the database schema and tables you need. -
Roles handling
- No roles → no changes required.
- Roles without passwords → no changes required.
-
Roles with passwords → grant
pgstreamsourceaccess topg_authidby enabling thecloudsql.pg_authid_select_roledatabase flag (via CloudSQL UI or API).- Reference: GCP documentation
-
Config when not snapshotting roles
If roles are disabled or not manually managed, add the following to avoid failures:
Example full configuration:
cloudsqlsuperuser, postgres) will not be snapshotted.
To CloudSQL Postgres (Snapshots)
Quick Checklist
- Create a target user (
pgstreamtarget) for the pgstream target URL. - Grant privileges for schema/database ownership.
- Grant optional privileges depending on features (create DB, create roles, set replication role).
- If using CloudSQL, disable
disable_triggers.
Steps
Thepgstreamtarget user (from the pgstream target URL) must have the following privileges:
-
Schema ownership
-
Database creation (if
create_target_dbis enabled) -
Role creation (if
role_snapshot_modeisenabled/no_passwords)⚠️pgstreamtargetmust already hold any privileges it assigns (e.g.,REPLICATION). If many privileges are needed, use thepostgresuser, which is a pseudo-superuser. -
Disable triggers (if
disable_triggersis enabled)Without this, pgstream fails with:⚠️ On CloudSQL, thepostgresrole cannot setsession_replication_role. Always disabledisable_triggersin this case.
Replication
From CloudSQL Postgres (Replication)
Quick Checklist
- Enable
cloudsql.logical_decodingflag. - Use
postgresuser for initialization. - Grant
cloudsqlreplica+ replication privileges. - Optionally transfer ownership to
pgstreamsourcefor streaming.
Steps
-
Enable logical replication
Enable the
cloudsql.logical_decodingflag. -
Replication phases
- Initialization → requires
postgres(superuser-like privileges). - Streaming → can switch to
pgstreamsource(from the pgstream source URL).
- Initialization → requires
Initialization
Initialization does the following:- Creates
pgstreamschema - Creates replication slot (if missing)
- Creates event triggers/functions for schema changes
postgres role. Also grant replication privileges explicitly:
Streaming
After initialization, you can:- Keep using
postgres, or - Transfer ownership to a dedicated
pgstreamsourcerole.
To CloudSQL Postgres (Replication)
Quick Checklist
- Ensure
pgstreamtargethas schema/database ownership. - If combined with snapshot → follow Snapshot target requirements.
Steps
Privileges required:Troubleshooting
permission denied for table pg_authid
permission denied for table pg_authid
Cause: Roles with passwords require pg_authid access.
Fix: Enable
Fix: Enable
cloudsql.pg_authid_select_role flag.permission denied to set parameter "session_replication_role"
permission denied to set parameter "session_replication_role"
Cause: CloudSQL roles can’t set replication role.
Fix: Disable
Fix: Disable
disable_triggers.Role creation fails
Role creation fails
Cause:
Fix: Use
pgstreamtarget lacks privileges it tries to assign.Fix: Use
postgres or grant required privileges first.