Skip to main content

PG Mode Overview

PG mode is a new way of running CloudBase environments, built on a Postgres-Native architecture. When you create a CloudBase environment and choose PostgreSQL database, the environment runs in PG mode automatically — PostgreSQL acts as the unified foundation of the infrastructure layer:

  • Authentication: user data lives in the PostgreSQL auth schema, so you can read the current user identity directly in SQL
  • Cloud storage: file metadata is stored in the PostgreSQL storage schema, with access controlled by RLS policies
  • PostgreSQL database: a ready-to-use PostgreSQL instance with full SQL capabilities, plus RESTful APIs exposed automatically through PostgREST
  • Permission system: a two-layer model combining table-level permissions (GRANT) and row-level permissions (RLS Policy), with the anon / authenticated / service_role database roles handling authorization. See architecture and permission model, data permission management, and the RLS policy patterns
  • Business data: use the PostgreSQL instance to store relational data, with RLS-based access control. To get started quickly, read the quickstart and the hands-on tutorial
  • Native mini program support: after enabling the pgdoc capability, you can call document database APIs directly from a WeChat mini program using the native SDK

Compared with the classic mode, the biggest difference is not "a different database" — it is that the infrastructure layer changes from separate subsystems into a unified runtime built on PostgreSQL. PG mode therefore fits scenarios that need strongly-typed modeling, relational queries, transactional consistency, and fine-grained row-level permissions.

New environments only

PG mode is only available for newly created environments. Existing classic-mode environments cannot be upgraded. To use these capabilities, create a new environment and choose PostgreSQL database.

If you are on a classic-mode environment, see the classic mode overview to learn about its capabilities and how they differ.

Why Is It Called PG Mode?​

PG mode is not simply "adding a PostgreSQL instance to the existing environment". As described above, it changes the database access layer, the permission model, authentication, cloud storage, and access roles at the same time: PostgreSQL is not just a SQL database — it is the unified runtime foundation for identity, storage, permissions, and business data access.

Because these foundational capabilities are all rebuilt around PostgreSQL, we call it PG mode (built on the Postgres-Native architecture), to distinguish it from the classic running mode of CloudBase and to highlight PostgreSQL as the first-class citizen of the whole environment runtime.

Architecture​

In PG mode, client requests reach the database through a unified authentication and policy-checking chain:

Client (Web / Mini Program / Cloud Function)
│ Authorization: Bearer <Token>
│ (Publishable Key / access_token / API Key)
▼
CloudBase gateway
│ Parses the JWT → sets role + request.jwt.claims
▼
PostgREST (automatic RESTful API)
▼
PostgreSQL
├─ Layer 1: table-level permission check (GRANT ... TO <role>)
├─ Layer 2: RLS policy filtering (USING / WITH CHECK)
└─ schemas:
├─ auth account system (users, etc.)
├─ storage cloud storage metadata (buckets / objects)
└─ public business tables

Double-lock principle: an operation succeeds only when both the table-level GRANT and the RLS policy allow it. If either layer rejects the request, a permission error is returned.

Key Capabilities​

CapabilityDescription
Full SQL supportNative PostgreSQL features: tables, views, foreign keys, indexes, transactions, triggers, stored procedures, CTEs, window functions, and more
PostgREST RESTful APIRESTful endpoints generated from your schema, with filtering, sorting, pagination, relational queries, and RPC calls
Two-layer permission modelTable-level GRANT plus row-level RLS — permissions are SQL, sharing the same language and semantics as business queries
Cloud storage integrationFile read/write permissions are enforced by RLS, fully consistent with table permissions
Authentication integrationAccount data lives in the database auth schema, so you can read the current user identity in SQL through JWT claims
Rich extension ecosystemAvailable extensions include pgvector (vector search), vectorscale (DiskANN vector index), tencentdb_ai (Tencent Cloud AI), zhparser / pg_jieba (Chinese tokenization), TimescaleDB (time series), Apache AGE (graph), PL/V8 (JavaScript stored procedures), and more

Differences from Classic Mode​

PG mode removes security rules, and each capability uses a different permission mechanism:

CapabilityPermission control
PostgreSQL databaseTable-level GRANT + row-level RLS
Cloud storageRLS policies
Document databaseCollection-level basic permissions retained
Cloud functionsOPA

Configure the RLS/OPA rules before clients access data. Client-side data access to a PG environment must go through the HTTP API. Supported clients and access methods:

  • @cloudbase/js-sdk v3.0 or later
  • @cloudbase/node-sdk v4.2 or later, wx-server-sdk v5.0.1-beta.1 or later (version requirements)
  • Official Flutter SDK and C# SDK
  • WeChat mini program built-in SDK
  • Direct HTTP API calls

Both PG mode and classic mode environments support WeChat mini program calls. The CloudBase console in WeChat Developer Tools does not currently support displaying or operating PG environments. Manage PG environments in the Tencent Cloud-side console. For initialization and call examples, see Use a PG Environment in a WeChat Mini Program.

Document database​

After enabling the pgdoc plugin, PG mode is compatible with the classic mode document database capabilities.

  1. No collection-level backup and restore; use the PostgreSQL database-level backup and restore instead.
  2. No classic realtime database; use the PG realtime feature instead.
  3. No security rules; collection-level basic permissions are retained, and finer-grained RLS permissions are not yet available.

Cloud storage​

  1. Create a bucket in the environment before using cloud storage.
  2. No classic basic permissions or security rules; access is fully controlled by RLS policies.
  3. With the WeChat mini program built-in SDK, parameters such as cloudPath must include the bucket prefix, in the format {bucket}/filepath.

For more differences, see PG mode cloud storage.

Cloud functions​

  1. No function security rules; use OPA instead.
  2. If a cloud function uses wx-server-sdk to call resources in a PG environment, use v5.0.1-beta.1 or later. See SDK upgrade requirements.

For a capability comparison between the two modes, see the classic mode overview.

Next Steps​