Execute commands on Kubernetes pods — run SQL queries via psql or Python code via Django shell. Use when you need to verify logic, check data, or test code against test/staging environments (sandbox, demo, preprod). Direct kubectl exec — no Docker overhead.
Run SQL queries or Python code directly on Kubernetes pods. Two modes: psql for database queries, Django shell for ORM / app logic.
~/.claude/skills/k8s-exec/k8s_exec.sh
kubectl installed and configuredK8S_NAMESPACE env var set, or the user provides a namespace-n flagK8S_NAMESPACE env var (user's default namespace)Well-known environment names (sandbox, demo, preprod) auto-resolve to the correct kubectl context and carta-web namespace. Production is blocked.
Use psql when you need raw SQL — counting rows, joins, aggregations, schema inspection, CSV exports. It's faster and has output formatting options.
Use Django shell when you need the ORM, app models, Django utilities, or want to test Python logic against real data. Use it to verify code behavior, inspect model relationships, or run application-level checks.
# Run a query
~/.claude/skills/k8s-exec/k8s_exec.sh -q "SELECT count(*) FROM some_table"
# Target a specific environment
~/.claude/skills/k8s-exec/k8s_exec.sh -n sandbox -q "SELECT count(*) FROM some_table"
# CSV output
~/.claude/skills/k8s-exec/k8s_exec.sh --csv -q "SELECT * FROM some_table LIMIT 10"
# Describe a table
~/.claude/skills/k8s-exec/k8s_exec.sh --describe some_table
# List databases
~/.claude/skills/k8s-exec/k8s_exec.sh --list-databases
# List pods
~/.claude/skills/k8s-exec/k8s_exec.sh --list-pods
# Interactive psql shell
~/.claude/skills/k8s-exec/k8s_exec.sh --shell
# SQL from file
~/.claude/skills/k8s-exec/k8s_exec.sh -f /path/to/query.sql
# Different database
~/.claude/skills/k8s-exec/k8s_exec.sh -d carta_web_audit -q "SELECT 1"
# Full options
~/.claude/skills/k8s-exec/k8s_exec.sh --help
# Run Python code (ORM query)
~/.claude/skills/k8s-exec/k8s_exec.sh --python "from myapp.models import MyModel; print(MyModel.objects.count())"
# Multi-line Python (use quotes)
~/.claude/skills/k8s-exec/k8s_exec.sh --python "
from ce_migration.models import CEMigrationMetadata
qs = CEMigrationMetadata.objects.filter(status='completed')
for obj in qs[:10]:
print(f'{obj.corporation_id}: {obj.carta_europe_id}')
"
# Python from file
~/.claude/skills/k8s-exec/k8s_exec.sh --python-file /path/to/script.py
# Python from stdin
echo "print('hello')" | ~/.claude/skills/k8s-exec/k8s_exec.sh --python-stdin
# Interactive Django shell
~/.claude/skills/k8s-exec/k8s_exec.sh --django-shell
# Target a specific environment
~/.claude/skills/k8s-exec/k8s_exec.sh -n sandbox --python "from django.conf import settings; print(settings.DATABASES.keys())"
When using Django shell mode:
.save(), .delete(), or bulk updates unless explicitly requestedWhen targeting a well-known environment (sandbox, demo, preprod), kubectl commands may fail with permission/access/forbidden errors like:
Error from server (Forbidden): pods is forbiddencannot list resource "pods" in API groupUnauthorizedThis means you don't have temporary access to the namespace. Automatically invoke the grant-k8s-access skill to request access, then retry the original command:
Use Skill tool: skill = "grant-k8s-access", args = "<ENVIRONMENT_NAME>"
Where <ENVIRONMENT_NAME> is the well-known environment name (e.g., sandbox). After the skill reports success, retry the original command.
The script auto-discovers the right pod:
psql directly (fast)carta-web app pod and uses manage.py dbshellcarta-web app pod (needs Django environment)