Skip to content

Commit 49895b9

Browse files
ImTotemclaude
andcommitted
feat(infra): auto-create n8n Postgres credential from .env
Postgres credential only needs simple connection params (host, port, db, user, password) — safe to automate via CLI import. Google Sheets credential still requires manual UI setup. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 720071e commit 49895b9

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

infra/scripts/init_n8n.sh

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
# n8n workflow import + owner setup
4+
# n8n workflow import + owner setup + Postgres credential
55
# Idempotent: skips if already configured
6-
# Credentials (Postgres, Google Sheets) must be set up manually in n8n UI
6+
# Google Sheets credential must be set up manually in n8n UI
77

88
COMPOSE="sudo docker compose -p bcsd-app --env-file .env -f infra/docker/docker-compose.yml"
99
MAX_RETRIES=15
@@ -65,6 +65,36 @@ print('setup' if d.get('userManagement',{}).get('showSetupOnFirstLoad') else 'do
6565
echo " Owner created: ${N8N_AUTH_USER}@bcsdlab.com"
6666
}
6767

68+
setup_pg_credential() {
69+
local existing
70+
existing=$($COMPOSE exec -T n8n n8n list:credential 2>&1 | grep -c "BCSD PostgreSQL" || true)
71+
if [ "$existing" -gt 0 ]; then
72+
echo " Postgres credential already exists — skipping"
73+
return 0
74+
fi
75+
echo " Creating Postgres credential from .env..."
76+
set -a; source .env; set +a
77+
local cred_json
78+
cred_json=$(python3 -c "
79+
import json, sys, uuid
80+
cred = [{
81+
'id': str(uuid.uuid4()),
82+
'name': 'BCSD PostgreSQL',
83+
'type': 'postgres',
84+
'data': {
85+
'host': '${POSTGRES_HOST:-postgres}',
86+
'port': ${POSTGRES_PORT:-5432},
87+
'database': '${POSTGRES_DB}',
88+
'user': '${POSTGRES_USER}',
89+
'password': '${POSTGRES_PASSWORD}',
90+
'ssl': 'disable'
91+
}
92+
}]
93+
json.dump(cred, sys.stdout)
94+
")
95+
echo "$cred_json" | $COMPOSE exec -T n8n n8n import:credentials --input=/dev/stdin
96+
}
97+
6898
echo "=== n8n Init ==="
6999

70100
echo "1. Starting n8n..."
@@ -80,9 +110,12 @@ fi
80110
echo "3. Setting up owner account..."
81111
setup_owner
82112

83-
echo "4. Importing workflows..."
113+
echo "4. Setting up Postgres credential..."
114+
setup_pg_credential
115+
116+
echo "5. Importing workflows..."
84117
import_workflow "/workflows/pg_sheets_sync.json" "PG → Sheets Sync (5min)"
85118
import_workflow "/workflows/link_auto_expire.json" "Link Auto-Expiration (hourly)"
86119

87120
echo "=== n8n Init complete ==="
88-
echo "NOTE: Set up Postgres + Google Sheets credentials in n8n UI if first deploy"
121+
echo "NOTE: Set up Google Sheets credential in n8n UI if first deploy"

0 commit comments

Comments
 (0)