clawdie-ai/docs/internal/scripts/fix-permissions.sh
Operator & Codex f6acf8e256 Prune stale first-boot docs and scripts (Sam & Codex)
Make the first-boot implementation spec self-contained, remove the superseded secrets handoff and obsolete manual jail setup scripts, and align hostname defaulting with the assistant-name separation rule. Update PostgreSQL permission notes and sync the public first-boot page into Astro docs.

---

Build: pass

Tests: pass — 2197 passed (164 files)

---
Build: pass | Tests: pass — 2197 passed (650 files)
2026-05-07 12:40:47 +02:00

77 lines
2.5 KiB
Bash
Executable file

#!/bin/sh
# fix-permissions.sh — Fix PostgreSQL permissions on existing databases
#
# Usage: ./fix-permissions.sh [db_user] [db_name]
#
# Defaults to values from .env if not provided.
#
# This script fixes the "permission denied for table memories" error
# by granting ALL PRIVILEGES on existing tables and sequences,
# and setting up DEFAULT PRIVILEGES for future tables.
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
cd "${PROJECT_ROOT}"
# ── Parse arguments or read from .env ───────────────────────────────────────
if [ $# -ge 2 ]; then
DB_USER="$1"
DB_NAME="$2"
else
# Read from .env
env_get() {
local key="$1" default="$2"
local val
val=$(grep -m1 "^${key}=" .env 2>/dev/null | cut -d= -f2- | sed "s/^['\"]//;s/['\"]$//")
printf '%s' "${val:-$default}"
}
DB_USER="${1:-$(env_get MEMORY_DB_USER system_brain)}"
DB_NAME="${2:-$(env_get MEMORY_DB_NAME system_brain)}"
fi
echo "==> Fixing PostgreSQL permissions"
echo " Database: ${DB_NAME}"
echo " User: ${DB_USER}"
echo ""
# ── Check if psql is available ─────────────────────────────────────────────
if ! command -v psql >/dev/null 2>&1; then
echo "ERROR: psql not found in PATH" >&2
exit 1
fi
# ── Check for permissions.sql ─────────────────────────────────────────────
PERMISSIONS_SQL="${PROJECT_ROOT}/docs/internal/sql/permissions.sql"
if [ ! -f "${PERMISSIONS_SQL}" ]; then
echo "ERROR: ${PERMISSIONS_SQL} not found" >&2
echo " Make sure you're running from the Clawdie-AI repository" >&2
exit 1
fi
# ── Run permissions.sql ────────────────────────────────────────────────────
echo "==> Applying permissions..."
psql -v db_user="${DB_USER}" -d "${DB_NAME}" -f "${PERMISSIONS_SQL}"
if [ $? -eq 0 ]; then
echo ""
echo "✓ Permissions fixed successfully"
echo ""
echo " Tables in ${DB_NAME} are now accessible by ${DB_USER}"
echo " Future tables will auto-inherit permissions"
echo ""
else
echo ""
echo "✗ Failed to apply permissions"
echo " Check that:"
echo " - Database '${DB_NAME}' exists"
echo " - User '${DB_USER}' exists"
echo " - You have postgres superuser access"
exit 1
fi