Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/r2-sql/configuration.md
1# R2 SQL Configuration23Setup and configuration for R2 SQL queries.45## Prerequisites67- R2 bucket with Data Catalog enabled8- API token with R2 permissions9- Wrangler CLI installed (for CLI queries)1011## Enable R2 Data Catalog1213R2 SQL queries Apache Iceberg tables in R2 Data Catalog. Must enable catalog on bucket first.1415### Via Wrangler CLI1617```bash18npx wrangler r2 bucket catalog enable <bucket-name>19```2021Output includes:22- **Warehouse name** - Typically same as bucket name23- **Catalog URI** - REST endpoint for catalog operations2425Example output:26```27Catalog enabled successfully28Warehouse: my-bucket29Catalog URI: https://abc123.r2.cloudflarestorage.com/iceberg/my-bucket30```3132### Via Dashboard33341. Navigate to **R2 Object Storage** → Select your bucket352. Click **Settings** tab363. Scroll to **R2 Data Catalog** section374. Click **Enable**385. Note the **Catalog URI** and **Warehouse** name3940**Important:** Enabling catalog creates metadata directories in bucket but does not modify existing objects.4142## Create API Token4344R2 SQL requires API token with R2 permissions.4546### Required Permission4748**R2 Admin Read & Write** (includes R2 SQL Read permission)4950### Via Dashboard51521. Navigate to **R2 Object Storage**532. Click **Manage API tokens** (top right)543. Click **Create API token**554. Select **Admin Read & Write** permission565. Click **Create API Token**576. **Copy token value** - shown only once5859### Permission Scope6061| Permission | Grants Access To |62|------------|------------------|63| R2 Admin Read & Write | R2 storage operations + R2 SQL queries + Data Catalog operations |64| R2 SQL Read | SQL queries only (no storage writes) |6566**Note:** R2 SQL Read permission not yet available via Dashboard - use Admin Read & Write.6768## Configure Environment6970### Wrangler CLI7172Set environment variable for Wrangler to use:7374```bash75export WRANGLER_R2_SQL_AUTH_TOKEN=<your-token>76```7778Or create `.env` file in project directory:7980```81WRANGLER_R2_SQL_AUTH_TOKEN=<your-token>82```8384Wrangler automatically loads `.env` file when running commands.8586### HTTP API8788For programmatic access (non-Wrangler), pass token in Authorization header:8990```bash91curl -X POST https://api.cloudflare.com/client/v4/accounts/{account_id}/r2/sql/query \92-H "Authorization: Bearer <your-token>" \93-H "Content-Type: application/json" \94-d '{95"warehouse": "my-bucket",96"query": "SELECT * FROM default.my_table LIMIT 10"97}'98```99100**Note:** HTTP API endpoint URL may vary - see [patterns.md](patterns.md#http-api-query) for current endpoint.101102## Verify Setup103104Test configuration by querying system tables:105106```bash107# List namespaces108npx wrangler r2 sql query "my-bucket" "SHOW DATABASES"109110# List tables in namespace111npx wrangler r2 sql query "my-bucket" "SHOW TABLES IN default"112```113114If successful, returns JSON array of results.115116## Troubleshooting117118### "Token authentication failed"119120**Cause:** Invalid or missing token121122**Solution:**123- Verify `WRANGLER_R2_SQL_AUTH_TOKEN` environment variable set124- Check token has Admin Read & Write permission125- Create new token if expired126127### "Catalog not enabled on bucket"128129**Cause:** Data Catalog not enabled130131**Solution:**132- Run `npx wrangler r2 bucket catalog enable <bucket-name>`133- Or enable via Dashboard (R2 → bucket → Settings → R2 Data Catalog)134135### "Permission denied"136137**Cause:** Token lacks required permissions138139**Solution:**140- Verify token has **Admin Read & Write** permission141- Create new token with correct permissions142143## See Also144145- [r2-data-catalog/configuration.md](../r2-data-catalog/configuration.md) - Detailed token setup and PyIceberg connection146- [patterns.md](patterns.md) - Query examples using configuration147- [gotchas.md](gotchas.md) - Common configuration errors148