Use a PG Environment in a WeChat Mini Program
Both PG mode and classic mode environments support calls from WeChat mini programs. After binding the accounts, pass the PG environment ID to wx.cloud.init and call the capabilities as shown below.
The CloudBase console in WeChat Developer Tools does not currently support displaying or operating PG environments. This does not affect calls from mini program code. Manage PG environments in the Tencent Cloud-side CloudBase console.
Prerequisites
- The mini program is associated with the Tencent Cloud account that created the PG environment. For the binding steps, see Use an Existing Tencent Cloud Account's CloudBase Environment in a WeChat Mini Program.
- You have the PG environment ID. To create an environment, see Create a CloudBase environment.
- Deploy cloud functions, create document collections, relational tables, or buckets as needed, and configure access permissions.
Initialization
Pass the PG environment ID to wx.cloud.init. No PostgreSQL connection string is required.
// app.js
App({
onLaunch() {
wx.cloud.init({ env: '<PG environment ID>' })
},
})
Supported capabilities
| Capability | Call pattern on the mini program side | Prerequisite | Access control |
|---|---|---|---|
| Cloud functions | wx.cloud.callFunction | The function is deployed | OPA authorization policies |
| Document database | wx.cloud.database() | The pgdoc plugin is enabled and the collection is created | Collection-level basic permissions |
| Tables | rdb() from @cloudbase/wx-cloud-client-sdk | The table is created in the public schema | Table privileges (GRANT) and row-level security (RLS) |
| Cloud storage | wx.cloud.uploadFile, downloadFile, getTempFileURL, deleteFile | A bucket is created | OPA authorization policies and RLS policies |
Cloud functions
const { result } = await wx.cloud.callFunction({
name: 'query-orders',
data: { page: 1 },
})
PG environments do not use cloud function security rules. Access is controlled by OPA authorization policies.
If the function uses wx-server-sdk to access CloudBase resources, use a version that supports PG mode. See SDK upgrade requirements.
Document database
Enable the pgdoc plugin and create a todos collection in the Document editor. Use wx.cloud.database() to read and write collections from the mini program.
const db = wx.cloud.database()
const { data } = await db
.collection('todos')
.where({ done: false })
.limit(20)
.get()
pgdoc supports collection-level basic permissions, not security rules. For complete read and write examples, see Use pgdoc.
Tables
Relational tables and document collections are created separately. This example queries the todos table in the public schema. Install the client SDK first:
npm install @cloudbase/wx-cloud-client-sdk
After installation, click "Tools → Build npm" in WeChat Developer Tools.
const { init } = require('@cloudbase/wx-cloud-client-sdk')
const cloudbase = init(wx.cloud)
const { data, error } = await cloudbase.rdb().from('todos').select('*').range(0, 9)
if (error) throw error
Configure table privileges and RLS policies for the current request role before making calls. See Data permission management.
For a complete example, see Access CloudBase PostgreSQL tables from a WeChat mini program.
Cloud storage
In a PG environment, cloudPath must start with the bucket name, in the format {bucket}/filepath:
const { fileID } = await wx.cloud.uploadFile({
cloudPath: 'avatars/example.png', // avatars is the bucket name
filePath: tempFilePath,
})
This example only demonstrates the bucket prefix. The actual object path must match the RLS policies. See storage permission management for configuration.
For more storage operations, see PG mode cloud storage.
Environment binding and management
After binding the accounts, mini program code can call the PG environment without converting it in WeChat Developer Tools.
The CloudBase console in WeChat Developer Tools does not currently support displaying or operating PG environments. Manage resources such as databases, cloud functions, and cloud storage in the Tencent Cloud-side CloudBase console.
Troubleshooting
| Symptom | What to check |
|---|---|
| The call returns environment not found or no permission | Check the environment ID, account binding, and access permissions for the capability |
| Document database queries report that the collection does not exist | Check that pgdoc is enabled, the collection is created, and the collection name is correct |
| Table queries return empty results or permission errors | Check error in the response first. If there is no error but the result is empty, check the table data, query conditions, and RLS policies for the current role |
| File uploads fail | Use the error details to check the local file, bucket, cloudPath, OPA authorization policies, and storage RLS policies |
| PG environments cannot be viewed or operated in Developer Tools | Manage them in the Tencent Cloud-side CloudBase console. Code-level calls are unaffected |