Skip to main content

Relational Database (PostgreSQL)

v3.4.0

Support for tcb db execute in PostgreSQL environments is available since CLI v3.4.0.

The tcb db execute command can be used in PostgreSQL environments to execute any SQL statement (DDL / DML / DQL, etc.). Common use cases include advanced Storage bucket management and RLS policy management.

tcb db execute

Execute a SQL statement.

tcb db execute --sql '<sql>' [options]

Parameters

ParameterDescriptionDefault
-s, --sql <sql>The SQL statement to execute
--role <role>Specify the role to execute the SQL (accepts any valid role, including user-defined roles)
-e, --env-id <envId>Environment ID
--jsonOutput results in JSON format
Built-in Read-Only Role

CloudBase provides a built-in read-only role cloudbase_read_only_user. Using --role cloudbase_read_only_user prevents unintended write operations from succeeding.

Examples

# Basic query
tcb db execute -e <pgEnvId> --sql "SELECT 1"

# List Storage buckets
tcb db execute -e <pgEnvId> --sql "SELECT name, public FROM storage.buckets ORDER BY created_at"

# Use the built-in read-only role to prevent accidental writes
tcb db execute -e <pgEnvId> --role cloudbase_read_only_user --sql "SELECT name, public FROM storage.buckets"

# Create a Storage bucket
tcb db execute -e <pgEnvId> --sql "INSERT INTO storage.buckets (id, name, public, created_at, updated_at) VALUES ('avatars', 'avatars', false, now(), now())"