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
authschema, so you can read the current user identity directly in SQL - Cloud storage: file metadata is stored in the PostgreSQL
storageschema, 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_roledatabase 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.
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
| Capability | Description |
|---|---|
| Full SQL support | Native PostgreSQL features: tables, views, foreign keys, indexes, transactions, triggers, stored procedures, CTEs, window functions, and more |
| PostgREST RESTful API | RESTful endpoints generated from your schema, with filtering, sorting, pagination, relational queries, and RPC calls |
| Two-layer permission model | Table-level GRANT plus row-level RLS — permissions are SQL, sharing the same language and semantics as business queries |
| Cloud storage integration | File read/write permissions are enforced by RLS, fully consistent with table permissions |
| Authentication integration | Account data lives in the database auth schema, so you can read the current user identity in SQL through JWT claims |
| Rich extension ecosystem | Available 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:
| Capability | Permission control |
|---|---|
| PostgreSQL database | Table-level GRANT + row-level RLS |
| Cloud storage | RLS policies |
| Document database | Collection-level basic permissions retained |
| Cloud functions | OPA |
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-sdkv3.0 or later@cloudbase/node-sdkv4.2 or later,wx-server-sdkv5.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.
- No collection-level backup and restore; use the PostgreSQL database-level backup and restore instead.
- No classic realtime database; use the PG realtime feature instead.
- No security rules; collection-level basic permissions are retained, and finer-grained RLS permissions are not yet available.
Cloud storage
- Create a bucket in the environment before using cloud storage.
- No classic basic permissions or security rules; access is fully controlled by RLS policies.
- With the WeChat mini program built-in SDK, parameters such as
cloudPathmust include the bucket prefix, in the format{bucket}/filepath.
For more differences, see PG mode cloud storage.
Cloud functions
- No function security rules; use OPA instead.
- If a cloud function uses
wx-server-sdkto 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
- PG authentication — how it works: JWT, three roles, GRANT/RLS, and SQL integration
- PG cloud storage
- PostgreSQL database — architecture and permission model
- PostgreSQL database — RPC (database functions)
- PostgreSQL database — quickstart (a 5-minute Hello World)
- PostgreSQL database — hands-on tutorial (end-to-end e-commerce mini program example)
- PostgreSQL database — RLS policy patterns (READONLY / PRIVATE / ADMINWRITE / ADMINONLY plus advanced scenarios)
- PostgreSQL database — common errors
- Migrating from Supabase