Skip to main content

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​

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​

CapabilityCall pattern on the mini program sidePrerequisiteAccess control
Cloud functionswx.cloud.callFunctionThe function is deployedOPA authorization policies
Document databasewx.cloud.database()The pgdoc plugin is enabled and the collection is createdCollection-level basic permissions
Tablesrdb() from @cloudbase/wx-cloud-client-sdkThe table is created in the public schemaTable privileges (GRANT) and row-level security (RLS)
Cloud storagewx.cloud.uploadFile, downloadFile, getTempFileURL, deleteFileA bucket is createdOPA 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​

SymptomWhat to check
The call returns environment not found or no permissionCheck the environment ID, account binding, and access permissions for the capability
Document database queries report that the collection does not existCheck that pgdoc is enabled, the collection is created, and the collection name is correct
Table queries return empty results or permission errorsCheck 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 failUse 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 ToolsManage them in the Tencent Cloud-side CloudBase console. Code-level calls are unaffected