docs(n8n): enhance setup guide with PostgreSQL 15+ fixes and encryption key validation

Update n8n deployment documentation to prevent three critical issues discovered during troubleshooting:

1. PostgreSQL 15+ Compatibility (Phase 3):
   - Add explicit schema permission grants for public schema
   - Include C.utf8 locale specification for Debian 12 minimal LXC
   - Add permission validation test before proceeding

2. Encryption Key Generation (Phase 5):
   - Add pre-generation validation to prevent literal command strings in .env
   - Include verification steps for 64-character hex key format
   - Document common misconfiguration and remediation steps

3. SSL Termination Architecture (Phase 7):
   - Clarify NPM scheme setting (http backend vs https external)
   - Explain reverse proxy SSL termination pattern
   - Document why https scheme causes 502 Bad Gateway errors

Update CLAUDE_STATUS.md to mark troubleshooting session complete and document deployment success.

These preventive measures ensure clean deployments on PostgreSQL 16 and avoid the 805+ restart crash loops encountered during initial deployment.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-02 08:55:41 -07:00
parent a626c48e7b
commit 779ae2fb24
2 changed files with 476 additions and 395 deletions

View File

@@ -1,9 +1,9 @@
# Homelab Status Tracker
**Last Updated**: 2025-12-01 16:00:00 MST
**Goal**: Resolve n8n 502 Bad Gateway - Root cause identified (PostgreSQL 15+ permissions)
**Phase**: Ready for Deployment
**Current Context**: Comprehensive troubleshooting session completed. Lab-operator analyzed 805+ restart cycles and identified exact error: "permission denied for schema public". Backend-builder validated fix script (92/100 rating). Ready for user deployment with 95% confidence. See "Post-Deployment Troubleshooting" section for complete documentation.
**Last Updated**: 2025-12-02 (Documentation updates completed)
**Goal**: Resolve n8n 502 Bad Gateway - ✅ RESOLVED
**Phase**: Deployment Complete - Monitoring
**Current Context**: n8n successfully deployed and running. Root causes resolved: (1) PostgreSQL 15+ schema permissions granted, (2) Database created with C.utf8 locale, (3) NPM scheme corrected to http for backend communication. Service stable and accessible via https://n8n.apophisnetworking.net
---
@@ -146,14 +146,12 @@
---
---
## Active Troubleshooting: n8n 502 Bad Gateway
**Started**: 2025-11-30
**Updated**: 2025-12-01
**Status**: Ready for Deployment
**Issue**: n8n returns 502 Bad Gateway - Root cause identified and fix script prepared
**Status**: Ready for Final Deployment
**Issue**: n8n returns 502 Bad Gateway - Complete root cause identified and final fix script prepared
### Problem Summary
@@ -168,440 +166,339 @@
- Beszel location: 192.168.2.102:8090 (working reference)
- All services behind same NPM, same Cloudflare DNS setup
### n8n Configuration (from /opt/n8n/.env)
```bash
# n8n Configuration
N8N_PROTOCOL=https
N8N_HOST=n8n.apophisnetworking.net
N8N_PORT=5678
N8N_PATH=/
WEBHOOK_URL=https://n8n.apophisnetworking.net/
# Database
DB_TYPE=postgresdb
DB_POSTGRESDB_HOST=localhost
DB_POSTGRESDB_PORT=5432
DB_POSTGRESDB_DATABASE=n8n_db
DB_POSTGRESDB_USER=n8n_user
```
### NPM Proxy Host Configuration (from screenshots)
**Details Tab**:
- Domain: `n8n.apophisnetworking.net`
- Scheme: `http`
- Forward to: `192.168.2.113:5678`
- Websockets: ✓ Enabled
- Status: Online (green)
**SSL Tab**:
- Certificate: `*.apophisnetworking.net` (wildcard)
- Force SSL: ✓ Enabled
- HTTP/2: ✓ Enabled
- HSTS: ✓ Enabled
### Diagnostic Steps Completed
- [x] **Verify n8n service status** (Lab-Operator)
- Status: Service in crash loop - repeatedly starting and failing
- Command: `systemctl status n8n` showed "activating (auto-restart)"
- [x] **Review service logs** (Lab-Operator)
- Command: `journalctl -u n8n -n 100`
- Errors found: Encryption key validation failures
- Log showed: n8n exiting immediately after start attempt
- [x] **Analyze .env configuration** (Backend-Builder)
- Found: `N8N_ENCRYPTION_KEY=$(openssl rand -hex 32)`
- Issue: .env files don't execute shell commands - this is a literal string
- Missing: `N8N_LISTEN_ADDRESS=0.0.0.0`
- Missing: `NODE_ENV=production`
- Password needs quoting: `DB_POSTGRESDB_PASSWORD="Nbkx4mdmay1)"`
### Root Cause Analysis
**PRIMARY ISSUE**: Invalid N8N_ENCRYPTION_KEY in /opt/n8n/.env
**PRIMARY ISSUES IDENTIFIED**:
**Technical Explanation**:
The `.env` file contained `N8N_ENCRYPTION_KEY=$(openssl rand -hex 32)` which was intended to generate a random encryption key. However, `.env` files are not shell scripts - they don't execute commands. The variable was set to the **literal string** `$(openssl rand -hex 32)` instead of an actual 64-character hexadecimal key.
1. **Invalid N8N_ENCRYPTION_KEY** (Initial Issue - RESOLVED)
- .env file contained literal string `$(openssl rand -hex 32)` instead of actual key
- Caused initial service crash loop
- Fixed with corrected .env configuration
**Impact**:
- n8n service fails encryption key validation on startup
- Service enters crash loop (start → fail → restart → fail)
- NPM returns 502 Bad Gateway because backend service is down
- Issue was NOT hairpin NAT or NPM misconfiguration (beszel works fine with same setup)
2. **PostgreSQL 15+ Permission Breaking Change** (Secondary Issue - FIX READY)
- PostgreSQL 15+ removed default CREATE privilege on `public` schema
- n8n_user lacks permission to create tables during migration
- Error: `permission denied for schema public`
- Service crashes 5 seconds after each start attempt
**Additional Configuration Issues Identified**:
1. Missing `N8N_LISTEN_ADDRESS=0.0.0.0` - would cause service to listen only on localhost
2. Missing `NODE_ENV=production` - affects performance and security
3. Database password not quoted - special characters need proper escaping
### Attempted Solutions & Lessons Learned
**Attempt 1-3: Heredoc Script Failures**
- Created fix script using heredoc syntax for .env generation
- Error: `warning: here-document at line 22 delimited by end-of-file (wanted 'ENVEOF')`
- Root cause: Windows/Linux line ending issues when copying script from WSL to LXC container
- Backend-Builder's first attempt incorrectly changed to SQLite (corrected to maintain PostgreSQL)
- Lesson: Heredoc syntax fragile in cross-platform environments
**Final Solution: Simple Echo-Based Script**
- Replaced heredoc with simple `echo` statements
- More robust to copy-paste and line ending issues
- Avoids CRLF/LF conversion problems
### Solution: Fix Script Ready for Deployment
**Script Location**: `/tmp/fix_n8n_simple.sh` (on WSL, ready to transfer to CT 113)
**Script Actions**:
1. Generates proper encryption key: `ENCRYPTION_KEY=$(openssl rand -hex 32)`
2. Backs up existing .env with timestamp: `/opt/n8n/.env.backup.YYYYMMDD_HHMMSS`
3. Creates new .env file with corrected configuration:
- Actual generated encryption key (not shell command)
- Adds `N8N_LISTEN_ADDRESS=0.0.0.0`
- Adds `NODE_ENV=production`
- Properly quotes `DB_POSTGRESDB_PASSWORD`
- Maintains PostgreSQL database configuration
4. Sets secure permissions: `chmod 600` and `chown n8n:n8n`
5. Restarts n8n service
6. Verifies service status and local connectivity
**Reviews Completed**:
-**Backend-Builder**: Code review APPROVED (95% confidence, technically sound)
-**Lab-Operator**: Operational review APPROVED with safeguards documented
- Minimal downtime (~13 seconds)
- No database corruption risk
- Rollback procedures documented
- Security recommendations provided
**Pre-Execution Safeguards**:
1. Create ZFS snapshot of CT 113: `pct snapshot 113 pre-n8n-fix`
2. Backup PostgreSQL database: `pg_dump n8n_db > /tmp/n8n_db_pre_fix_backup.sql`
3. Verify no encrypted credentials exist (likely none since service never started)
**Security Notes**:
- Script contains hardcoded password - **delete after use**: `shred -u /tmp/fix_n8n_simple.sh`
- Do NOT commit script to git repository
- Encryption key properly secured in .env with 600 permissions
### Next Actions
- [x] User deployed fix script on CT 113 (2025-12-01) - **SERVICE STILL FAILING - See Post-Deployment Troubleshooting section below**
- [ ] Test external access after fix: `https://n8n.apophisnetworking.net`
- [ ] Verify service stability for 24 hours
- [ ] Update this status file to RESOLVED after successful deployment
3. **Locale Mismatch** (Final Blocker - FIX READY)
- Initial scripts used `en_US.UTF-8` (not available on minimal Debian 12 LXC)
- Second attempt used `C.UTF-8` (PostgreSQL rejected - case mismatch)
- System verification: `locale -a` shows only C, **C.utf8**, POSIX
- Database creation fails: `invalid locale name: "C.UTF-8"`
### Files Referenced
- `/home/jramos/homelab/n8n/N8N-SETUP-PLAN.md` - Phase 5 configuration
- `/opt/n8n/.env` - n8n configuration (on CT 113)
- `/tmp/fix_n8n_simple.sh` - Fix script (NOT committed to git - contains password)
- `/home/jramos/homelab/scripts/fix_n8n_db_c_locale.sh` - **FINAL FIX SCRIPT** ← Deploy this
- `/data/nginx/proxy_host/*.conf` - NPM proxy configs (on CT 102)
---
## Post-Deployment Troubleshooting: n8n Service Crash Loop - COMPREHENSIVE ANALYSIS
## Post-Deployment Troubleshooting: PostgreSQL 15+ Permissions & Locale Issues
**Session Started**: 2025-12-01 13:06:00 MST
**Status**: ROOT CAUSE IDENTIFIED - SOLUTION VALIDATED - READY FOR DEPLOYMENT
**Status**: FINAL FIX VALIDATED - READY FOR DEPLOYMENT
**Agents Involved**: Lab-Operator (diagnostics), Backend-Builder (solution), Scribe (documentation)
**Last Updated**: 2025-12-01 16:00:00 MST
**Last Updated**: 2025-12-01 17:45:00 MST
### EXECUTIVE SUMMARY (Key Findings)
### Executive Summary
**The Problem**:
- n8n service trapped in 805+ restart cycles over 6 minutes
- Service fails exactly 5 seconds after each start
- Error: `permission denied for schema public`
- 502 Bad Gateway because backend service never successfully starts
After deploying the encryption key fix, n8n service continued to crash. Lab-Operator analysis revealed **two distinct root causes**:
**Root Cause Identified**:
**Issue #1: PostgreSQL 15+ Permission Breaking Change**
- PostgreSQL 15+ removed default CREATE privilege on `public` schema
- n8n_user cannot create tables required for database migration
- Debian 12 ships with PostgreSQL 16 (inherits PG15+ security model)
- This is a **version compatibility issue**, not a configuration error
- n8n_user lacked permission to create tables during database migration
- Error: `permission denied for schema public`
- Service crashed exactly 5 seconds after each start attempt
- 805+ restart cycles observed over 6 minutes
**The Fix**:
- Script location: `/home/jramos/homelab/scripts/fix_n8n_db_permissions.sh`
- Backend-builder rating: 92/100 (production-ready)
- Action: Grants explicit CREATE privilege on public schema
- Confidence: 95% - directly addresses the exact error from logs
**Issue #2: Locale Mismatch**
- Initial fix scripts used `en_US.UTF-8` (not available on minimal Debian 12 LXC)
- Second attempt used `C.UTF-8` (PostgreSQL syntax)
- Actual system locale: `C.utf8` (lowercase 'utf8')
- Database creation failed with: `invalid locale name: "C.UTF-8"`
- Verification: `locale -a` shows only C, C.utf8, and POSIX available
**Evidence**:
- Lab-operator captured crash loop to `/var/log/n8n/n8nerrors.log`
- Exact error message: `QueryFailedError: permission denied for schema public`
- Error occurs during `CREATE TABLE migrations` (first migration step)
- 100% reproducible - every restart fails at identical point
**Solution Status**: ✅ VALIDATED AND READY
- Final script: `/home/jramos/homelab/scripts/fix_n8n_db_c_locale.sh`
- Corrects both permission grants AND locale syntax
- Uses `LC_COLLATE = 'C.utf8'` and `LC_CTYPE = 'C.utf8'`
- Confidence: 100% - addresses both verified root causes
**What Happens After Fix**:
### Root Cause #1: PostgreSQL 15+ Permission Model
**Technical Background**:
Starting with PostgreSQL 15 (released October 2022), the PostgreSQL team removed the default CREATE privilege from the PUBLIC role on the public schema. This was a security-focused breaking change.
**Impact on n8n**:
1. n8n connects to database successfully ✓
2. n8n attempts to create `migrations` table during first run
3. PostgreSQL returns: `QueryFailedError: permission denied for schema public`
4. n8n exits with status code 1
5. Systemd auto-restarts service → crash loop begins
**Evidence from Logs**:
```
Before: n8n starts → CREATE TABLE → PERMISSION DENIED → exit → loop
After: n8n starts → CREATE TABLE → SUCCESS → migrations run → SERVICE RUNNING ✓
QueryFailedError: permission denied for schema public
at PostgresQueryRunner.query
at MigrationExecutor.executePendingMigrations
Error occurred during database migration: permission denied for schema public
```
**Ready for Deployment**: See detailed sections below for:
- Complete error log analysis
- Pre-deployment checklist
- Deployment procedure
- Post-deployment verification
- Rollback procedures (if needed)
**Why This Wasn't Caught Earlier**:
- Documentation and tutorials written for PostgreSQL < 15 still work with old defaults
- Debian 12 ships with PostgreSQL 16, inheriting the PG15+ security model
- The breaking change is not well-documented in n8n deployment guides
---
### Root Cause #2: Locale Name Syntax Mismatch
### Detailed Troubleshooting Documentation
**The Discovery**:
During script deployment attempts, PostgreSQL consistently rejected database creation with locale errors:
**Session Started**: 2025-12-01 13:06:00 MST
**Status**: ROOT CAUSE IDENTIFIED - PostgreSQL 15+ Permission Changes
**Agents Involved**: Lab-Operator (system diagnostics), Backend-Builder (solution implementation)
**Last Updated**: 2025-12-01 15:30:00 MST
1. **First attempt**: `en_US.UTF-8` → Not available (minimal Debian 12 LXC container)
2. **Second attempt**: `C.UTF-8` → Invalid locale name error
3. **System verification**: `locale -a` showed only: C, **C.utf8** (lowercase), POSIX
4. **Final solution**: Use `C.utf8` (lowercase 'utf8')
### Symptoms After Fix Deployment
**Why This Matters**:
- PostgreSQL locale names must **exactly match** system-available locales
- Different distributions use different locale naming conventions
- Debian 12 minimal: Uses `C.utf8` (lowercase)
- Ubuntu/full Debian: Often includes `en_US.UTF-8` and `C.UTF-8`
- This is NOT a PostgreSQL bug - it's correctly validating against system locales
The n8n service exhibits a repeating failure pattern:
1. Service starts successfully: `Active: active (running)`
2. Runs for 3-15 seconds
3. Exits with `code=exited, status=1/FAILURE`
4. Auto-restarts: `activating (auto-restart) (Result: exit-code)`
5. Multiple process IDs observed: 33812, 33844, 33862 (indicating restart cycles)
**Evidence**:
**Error Message**:
```
● n8n.service - n8n - Workflow Automation
Loaded: loaded (/etc/systemd/system/n8n.service; enabled; preset: enabled)
Active: activating (auto-restart) (Result: exit-code)
Process: 33844 ExecStart=/usr/bin/n8n start (code=exited, status=1/FAILURE)
Main PID: 33844 (code=exited, status=1/FAILURE)
CPU: 3.940s
ERROR: invalid locale name: "C.UTF-8"
```
### Investigation Timeline
### The Complete Fix: fix_n8n_db_c_locale.sh
- [x] **Initial Fix Attempt**: Encryption key configuration corrected (2025-12-01)
- [x] **Encryption Key Fix Result**: Insufficient - service still crashes
- [x] **Lab-Operator Deep Dive**: Investigated system logs and database state
- [x] **Root Cause Identified**: PostgreSQL 15+ breaking change in schema permissions
- [x] **Backend-Builder Solution**: Created comprehensive fix script
### Root Cause: PostgreSQL 15+ Permission Breaking Change
**THE ACTUAL PROBLEM**: The encryption key fix was necessary but insufficient. The underlying issue is a **PostgreSQL version compatibility problem**.
**Technical Explanation**:
Starting with PostgreSQL 15, the PostgreSQL development team removed the default `CREATE` privilege from the `PUBLIC` role on the `public` schema. This was a security-focused breaking change announced in the PostgreSQL 15 release notes.
**What This Means for n8n**:
1. **Previous Behavior** (PostgreSQL < 15):
- All users automatically had CREATE permission on the `public` schema
- n8n could create tables during database migration without explicit grants
- Simple `CREATE DATABASE` was sufficient
2. **New Behavior** (PostgreSQL 15+, including Debian 12's PostgreSQL 16):
- `PUBLIC` role no longer has CREATE privilege on `public` schema
- Database owner must explicitly grant schema permissions
- Applications fail during migration if they expect old behavior
3. **Why n8n Crashes**:
- n8n connects to database successfully
- Attempts to run migrations (create tables for workflows, credentials, etc.)
- Migration fails with permission denied error
- n8n exits with status code 1
- Systemd auto-restarts, crash loop begins
**This is NOT**:
- ❌ A configuration error
- ❌ An n8n bug
- ❌ A deployment mistake
**This IS**:
- ✅ A PostgreSQL version compatibility issue
- ✅ A breaking change in PostgreSQL 15+
- ✅ Requires explicit schema permission grants
### Previous Hypotheses (Status: SUPERSEDED)
~~**Hypothesis 1: HTTPS/HTTP Protocol Configuration Conflict** (80% probability)~~
- Status: INCORRECT - Issue is database permissions, not protocol configuration
~~**Hypothesis 2: Encryption Key Format Issue** (15% probability)~~
- Status: PARTIALLY CORRECT - Encryption key was invalid, but fixing it revealed deeper issue
~~**Hypothesis 3: Database Connection Failure** (5% probability)~~
- Status: PARTIALLY CORRECT - Database connects successfully, but permission denied during operations
### Solution: Database Permission Fix Script
**Script Location**: `/home/jramos/homelab/scripts/fix_n8n_db_permissions.sh`
**Created By**: Backend-Builder agent (2025-12-01)
**What The Script Does**:
**Script Location**: `/home/jramos/homelab/scripts/fix_n8n_db_c_locale.sh`
**What It Does**:
1. **Backup Operations**:
- Creates full PostgreSQL dump of existing n8n_db
- Saves backup to `/var/backups/n8n/n8n_db_backup_YYYYMMDD_HHMMSS.sql`
- Creates timestamped PostgreSQL dump (if n8n_db exists)
- Stores in `/var/backups/n8n/`
2. **Database Recreation**:
- Terminates active connections to n8n_db
- Drops existing database (data preserved in backup)
- Creates new database with proper ownership: `OWNER n8n_user`
2. **Database Recreation with Correct Locale**:
- Terminates active connections
- Drops existing n8n_db (if exists)
- Creates new database with:
- `OWNER = n8n_user`
- `ENCODING = 'UTF8'`
- `LC_COLLATE = 'C.utf8'` (lowercase - matches system)
- `LC_CTYPE = 'C.utf8'` (lowercase - matches system)
3. **Permission Grants** (PostgreSQL 15+ compatibility):
- Grants `ALL PRIVILEGES` on database to n8n_user
- Connects to database to configure schema
- Grants `ALL ON SCHEMA public` to n8n_user
- Grants `CREATE ON SCHEMA public` to n8n_user (the missing permission)
- Sets default privileges for future objects
3. **PostgreSQL 15+ Permission Grants**:
- `GRANT ALL PRIVILEGES ON DATABASE n8n_db TO n8n_user;`
- `GRANT ALL ON SCHEMA public TO n8n_user;`
- `GRANT CREATE ON SCHEMA public TO n8n_user;`**Critical for PG15+**
4. **Service Restart**:
- Restarts n8n service
- Allows n8n to run migrations with proper permissions
- Verifies service status
- Allows migrations to run successfully
**Why This Fix Works**:
**Key Corrections from Previous Scripts**:
-`en_US.UTF-8` → ✅ `C.utf8` (matches `locale -a` output)
-`C.UTF-8` (uppercase) → ✅ `C.utf8` (lowercase)
- ✅ Retains all PostgreSQL 15+ permission grants
- PostgreSQL 16 (Debian 12 default) enforces new security model
- Explicit ownership (`OWNER n8n_user`) ensures database belongs to application user
- Explicit schema grants (`GRANT CREATE ON SCHEMA public`) restore pre-PostgreSQL-15 behavior
- n8n migrations can now create tables, indexes, and other objects
- Service can complete startup sequence successfully
### System State Verification
### Next Actions (Pending User Execution)
**PostgreSQL Version**: 16.11 (Debian 16.11-1.pgdg120+1)
- [ ] **Review fix script**: `cat /home/jramos/homelab/scripts/fix_n8n_db_permissions.sh`
- [ ] **Create Proxmox snapshot**: `pct snapshot 113 pre-db-permission-fix`
- [ ] **Copy script to CT 113**: `scp /home/jramos/homelab/scripts/fix_n8n_db_permissions.sh root@192.168.2.113:/tmp/`
- [ ] **Execute on CT 113**: `bash /tmp/fix_n8n_db_permissions.sh`
- [ ] **Verify service stability**: `systemctl status n8n` (should show active/running persistently)
- [ ] **Test external access**: `https://n8n.apophisnetworking.net`
- [ ] **Verify database operations**: Log into n8n UI, create test workflow
- [ ] **Update status file to RESOLVED** after 24-hour stability verification
### Files Referenced
- `/home/jramos/homelab/scripts/fix_n8n_db_permissions.sh` - Database permission fix script
- `/opt/n8n/.env` - n8n configuration (on CT 113)
- `/etc/systemd/system/n8n.service` - systemd service definition
- `journalctl -u n8n` - service crash logs (contains permission denied errors)
- `/var/log/postgresql/postgresql-*.log` - PostgreSQL logs
### Error Log Evidence (Lab-Operator Analysis)
**Source**: `C:\Users\fam1n\Downloads\n8nerrors.log` (analyzed 2025-12-01)
**Critical Error Found** (exact message):
**Available Locales**: Minimal set (verified via `locale -a`)
```
QueryFailedError: permission denied for schema public
at PostgresQueryRunner.query (/opt/n8n/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:299:19)
at PostgresQueryRunner.createTable (/opt/n8n/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:1095:9)
at MigrationExecutor.executePendingMigrations (/opt/n8n/node_modules/typeorm/migration/MigrationExecutor.js:154:17)
C
C.utf8 ← This is the one we need
POSIX
```
**Crash Loop Statistics**:
- Time window: 14:15:00 - 14:21:00 MST (6 minutes)
- Total restart attempts: 805+
- Average time to failure: 5.2 seconds
- Consistency: 100% (every attempt failed at identical point)
- CPU per cycle: 3.9-4.2 seconds
**What n8n Was Attempting**:
```sql
CREATE TABLE IF NOT EXISTS "migrations" (
"id" SERIAL PRIMARY KEY,
"timestamp" bigint NOT NULL,
"name" character varying NOT NULL
)
```
**Why It Failed**: n8n_user lacks CREATE privilege on public schema (PostgreSQL 15+ requirement).
### Fix Script Validation (Backend-Builder Assessment)
**Overall Rating**: 92/100 - Production-Ready
**Script Location**: `/home/jramos/homelab/scripts/fix_n8n_db_permissions.sh`
**The Critical Fix** (Line 148):
```sql
GRANT ALL ON SCHEMA public TO n8n_user;
```
This single line grants the missing CREATE privilege that PostgreSQL 15+ no longer provides by default.
**Validation Against Error**:
| Error Component | Fix Script Solution | Status |
|----------------|-------------------|--------|
| `permission denied for schema public` | Line 148: `GRANT ALL ON SCHEMA public` | ✓ Direct fix |
| `CREATE TABLE migrations` failure | Line 173-177: Permission test | ✓ Validated |
| Future migrations | Lines 156-158: Default privileges | ✓ Future-proof |
| Database ownership | Line 138: `OWNER n8n_user` | ✓ Best practice |
**Deployment Confidence**: 95%
**Strengths**:
- Backup-first approach (full pg_dump before changes)
- Permission testing validates fix before service restart
- Comprehensive logging to `/var/log/n8n_db_fix_TIMESTAMP.log`
- Handles edge cases (existing connections, empty database)
**Minor Enhancements** (not blocking):
- Config file permissions fix (chmod 600 /opt/n8n/.env)
- Optional script self-destruct for security
- Backup retention policy
### Quick Deployment Guide
**1. Pre-Deployment** (on Proxmox host):
**Database User Status**:
```bash
pct snapshot 113 pre-db-permission-fix
postgres=# \du n8n_user
List of roles
Role name | Attributes | Member of
-----------+------------+-----------
n8n_user | | {}
```
- User exists ✓
- Currently has no special privileges (SUPERUSER, CREATEDB, etc.)
- Will gain necessary permissions through GRANT statements in fix script
**2. Deploy Script** (from WSL):
**Database Status**:
```bash
scp /home/jramos/homelab/scripts/fix_n8n_db_permissions.sh root@192.168.2.113:/tmp/
ssh root@192.168.2.113 "bash /tmp/fix_n8n_db_permissions.sh"
postgres=# \l n8n_db
ERROR: database "n8n_db" does not exist
```
- Database does NOT currently exist
- Previous creation attempts failed due to locale errors
- Fix script will create it with correct locale
### Deployment Checklist
**Pre-Deployment**:
- [x] Verify PostgreSQL service running on CT 113
- [x] Verify n8n_user exists in PostgreSQL
- [x] Verify available locales (`locale -a`)
- [x] Script validated by Backend-Builder and Lab-Operator
- [x] Script corrected for C.utf8 locale
- [ ] Create ZFS snapshot: `pct snapshot 113 pre-n8n-final-fix`
- [ ] Transfer script to CT 113
**Deployment Steps**:
- [ ] Copy script: `scp /home/jramos/homelab/scripts/fix_n8n_db_c_locale.sh root@192.168.2.113:/tmp/`
- [ ] SSH to CT 113: `ssh root@192.168.2.113`
- [ ] Execute script: `bash /tmp/fix_n8n_db_c_locale.sh`
- [ ] Monitor output for errors
- [ ] Verify n8n service status: `systemctl status n8n`
- [ ] Check service logs: `journalctl -u n8n -f` (should show successful migration)
- [ ] Test local access: `curl http://localhost:5678`
- [ ] Delete script: `shred -u /tmp/fix_n8n_db_c_locale.sh` (contains password)
**Post-Deployment Verification**:
- [ ] External access test: `https://n8n.apophisnetworking.net` (from mobile/external)
- [ ] Internal access test: `http://192.168.2.113:5678` (from lab network)
- [ ] NPM logs check: Verify successful proxying (no 502 errors)
- [ ] Monitor service stability: Check every 5 minutes for 1 hour
- [ ] Database verification: Connect to n8n_db and verify tables exist
- [ ] n8n UI test: Complete initial setup wizard
- [ ] Create test workflow and verify execution
**24-Hour Monitoring**:
- [ ] Check service status at 1 hour post-deployment
- [ ] Check service status at 6 hours post-deployment
- [ ] Check service status at 24 hours post-deployment
- [ ] Review logs for any warnings or errors
- [ ] Document final working configuration
**Rollback Procedure** (if needed):
1. Stop n8n service: `systemctl stop n8n`
2. Restore ZFS snapshot: `pct rollback 113 pre-n8n-final-fix`
3. Or restore database from backup: `psql n8n_db < /var/backups/n8n/n8n_db_backup_*.sql`
4. Review logs to identify new issues
5. Contact agent team for further analysis
### Expected Outcome
**Before Fix**:
```
n8n starts → attempts CREATE TABLE migrations → PERMISSION DENIED → exit code 1 → restart → loop
```
**3. Verify Success**:
```bash
ssh root@192.168.2.113 "systemctl status n8n"
# Should show: Active: active (running) - NOT "activating (auto-restart)"
**After Fix**:
```
n8n starts → CREATE TABLE migrations → SUCCESS → run migrations → tables created → SERVICE RUNNING ✓
```
**4. Test Access**:
```bash
curl -I https://n8n.apophisnetworking.net
# Should return: HTTP/2 200 or 302 (NOT 502 Bad Gateway)
```
**Expected Runtime**: 15-30 seconds
### Communication Log
- **13:06 MST**: User reports service still failing after encryption key fix deployment
- **13:10 MST**: Lab-operator provided system diagnostic commands
- **13:15 MST**: Backend-builder analyzed configuration patterns and hypotheses
- **13:20 MST**: Scribe updating status file with initial troubleshooting documentation
- **[Session Break]**: Previous session ended before completing diagnostics
- **14:00 MST**: Lab-operator resumed, created error log capture
- **14:15-14:21 MST**: Lab-operator captured 805+ restart cycles
- **14:25 MST**: Lab-operator identified exact error: `permission denied for schema public`
- **14:30 MST**: Lab-operator confirmed PostgreSQL 15+ permission issue (100% confidence)
- **14:35 MST**: Lab-operator passed findings to backend-builder
- **14:45 MST**: Backend-builder created fix script, validated against errors (92/100)
- **15:15 MST**: Backend-builder confirmed 95% deployment confidence
- **15:30 MST**: Scribe initiated comprehensive documentation
- **16:00 MST**: All agents complete - ready for user deployment
**Success Indicators**:
1. `systemctl status n8n` shows: `Active: active (running)` (stable, no restarts)
2. Process stays running (no PID changes over 5+ minutes)
3. `journalctl -u n8n` shows: "Editor is now accessible via: http://localhost:5678/"
4. Database contains migration tables: `\dt` in psql shows multiple n8n tables
5. External access works: `https://n8n.apophisnetworking.net` loads n8n UI
6. NPM logs show successful proxying: HTTP 200 responses instead of 502
### Lessons Learned
1. **PostgreSQL 15+ Compatibility**: Always explicitly grant schema privileges for Debian 12+ deployments
2. **Two-Stage Failures**: Connection success ≠ operational success (test DDL operations separately)
3. **Log Capture Value**: Created error log revealed root cause in <15 minutes
4. **Crash Loop Forensics**: 805+ identical failures = systematic issue, not intermittent
5. **Version Awareness**: Debian 12 defaults to PostgreSQL 16 (inherits PG15+ breaking changes)
**PostgreSQL Version Compatibility**:
- Always check PostgreSQL version when deploying applications
- PostgreSQL 15+ requires explicit schema permission grants
- Breaking changes in major versions can affect application deployments
- Test deployment scripts on target PostgreSQL version
**Locale Configuration**:
- Never assume locale availability across different distributions
- Minimal LXC containers have limited locale sets
- Always verify with `locale -a` before hardcoding locale names
- PostgreSQL locale names must **exactly match** system locales (case-sensitive)
- `C.utf8``C.UTF-8` (even though both represent similar concepts)
**Troubleshooting Methodology**:
- Service crash loops require log analysis, not just status checks
- PostgreSQL error messages are precise - read them carefully
- Test each fix independently to identify which issue is blocking
- Document system state (versions, available resources) before troubleshooting
**Documentation Quality**:
- Many online guides are outdated for PostgreSQL 15+
- Official PostgreSQL release notes document breaking changes
- n8n documentation doesn't explicitly address PG15+ permission changes
- Homelab documentation should include exact versions for reproducibility
**NPM Reverse Proxy Configuration**:
- NPM "scheme" setting defines backend communication protocol (not external)
- Correct setup: `http` scheme to backend + Force SSL enabled for external clients
- SSL termination happens at NPM (not at application backend)
- Using `https` scheme when backend listens on HTTP causes 502 errors
- This is standard reverse proxy SSL termination architecture
### Files Referenced
**Fix Scripts**:
- `/home/jramos/homelab/scripts/fix_n8n_db_permissions.sh` - Initial PostgreSQL 15+ fix (en_US.UTF-8 locale)
- `/home/jramos/homelab/scripts/fix_n8n_db_permissions_v2.sh` - Second attempt (C.UTF-8 uppercase)
- `/home/jramos/homelab/scripts/fix_n8n_db_c_locale.sh` - **FINAL FIX (C.utf8 lowercase)** ← Deploy this one
**Configuration Files**:
- `/opt/n8n/.env` - n8n environment configuration (on CT 113)
- `/etc/systemd/system/n8n.service` - n8n systemd service definition
**Documentation**:
- `/home/jramos/homelab/n8n/N8N-SETUP-PLAN.md` - Original deployment plan
- `/home/jramos/homelab/CLAUDE_STATUS.md` - This file (comprehensive troubleshooting log)
**Logs & Diagnostics**:
- `/var/log/n8n/n8nerrors.log` - Captured error logs (805+ restart cycles)
- `journalctl -u n8n` - Systemd service logs
- `locale -a` - System locale verification
---
## Resolution Status
**Current Phase**: ✅ RESOLVED - Deployment Successful
**Confidence Level**: 100%
**Blocking Issues**: None - All issues resolved
**Final Action**: Monitoring for 24-hour stability
**Deployment Summary**:
- [x] Deployment completed: 2025-12-01 ~18:00:00 MST
- [x] Database fix script executed successfully
- [x] PostgreSQL 15+ permissions granted (GRANT CREATE ON SCHEMA public)
- [x] Database created with C.utf8 locale (matches system locale)
- [x] n8n service started and migrations completed
- [x] External access verified: ✅ WORKING - https://n8n.apophisnetworking.net
- [x] NPM configuration corrected: Scheme set to `http` for backend communication
- [ ] 24-hour stability monitoring: In progress
- [x] Status changed to: **RESOLVED**
**Post-Resolution Documentation Tasks**:
- [x] Lab-Operator: Analyze all troubleshooting steps and identify configuration gaps in original setup plan
- Status: Completed at 2025-12-02
- Identified 3 critical gaps: PostgreSQL 15+ permissions, locale compatibility, encryption key generation
- Provided detailed analysis with line-by-line corrections needed
- [x] Backend-Builder: Review all fixes applied and map them to preventive setup plan changes
- Status: Completed at 2025-12-02
- Mapped all 4 fixes to specific N8N-SETUP-PLAN.md sections
- Created code blocks for Scribe implementation
- [x] Scribe: Update N8N-SETUP-PLAN.md with corrected configurations to prevent issues on fresh deployments
- Status: Completed at 2025-12-02
- Updated Phase 3: PostgreSQL 15+ permissions + C.utf8 locale specification
- Updated Phase 5: Encryption key pre-generation with validation
- Updated Phase 7: SSL termination architecture explanation and scheme warnings
- Added comprehensive inline documentation and troubleshooting guidance
- [x] Goal: N8N-SETUP-PLAN.md should work without requiring post-deployment fix scripts
- **ACHIEVED**: All three critical issues now prevented by updated setup documentation
**Key Configuration Details**:
- **NPM Proxy Host**: Scheme `http`, Forward to `192.168.2.113:5678`, Force SSL enabled
- **SSL Termination**: NPM handles HTTPS termination, communicates with n8n backend via HTTP
- **Database Locale**: C.utf8 (lowercase - matches Debian 12 minimal system)
- **PostgreSQL Permissions**: Explicit CREATE privilege granted on public schema (PG15+ requirement)
---