-- Fix PostgreSQL permissions for schema creation
-- Run this script as a PostgreSQL superuser (usually 'postgres')
--
-- Option 1: Run with psql (replace dmenuuser and dmenutest with your values):
--   psql -U postgres -d postgres -f fix-db-permissions.sql
--
-- Option 2: Run with sudo (no password needed):
--   sudo -u postgres psql -d postgres -f fix-db-permissions.sql
--
-- Option 3: Copy and paste these commands into psql after connecting

-- Replace 'dmenuuser' and 'dmenutest' with your actual DB_USER and DB_NAME from .env
-- Current values from your .env: DB_USER=dmenuuser, DB_NAME=dmenutest
-- 
-- IMPORTANT: For PostgreSQL 15+, we need to make the user the owner of the database and schema

-- Make the user the owner of the database (PostgreSQL 15+ requirement)
ALTER DATABASE dmenutest OWNER TO dmenuuser;

-- Connect to the target database (run this in psql, then continue with schema commands)
-- \c dmenutest

-- Make the user the owner of the public schema (PostgreSQL 15+ requirement)
ALTER SCHEMA public OWNER TO dmenuuser;

-- Grant all necessary permissions (redundant but safe)
GRANT CONNECT ON DATABASE dmenutest TO dmenuuser;
GRANT USAGE ON SCHEMA public TO dmenuuser;
GRANT CREATE ON SCHEMA public TO dmenuuser;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO dmenuuser;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO dmenuuser;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO dmenuuser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO dmenuuser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO dmenuuser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO dmenuuser;

-- For PostgreSQL 15+ - ensure full database access
GRANT ALL ON DATABASE dmenutest TO dmenuuser;

