Jelastic environment variables not working with N8N database host configuration

I’m having issues setting up N8N using a Jelastic JPS template. The main problem is that the environment variable for the PostgreSQL database host isn’t getting the correct IP address automatically.

type: install
id: workflow-automation
version: 1.0
name: Workflow Engine Setup
globals:
  database_user: workflow-${fn.random(5000)}
  database_pass: ${fn.password}
  secret_key: app${fn.password}

nodes:
- nodeType: nginx
  displayName: Proxy Server
  cloudlets: 8
  nodeGroup: proxy
- image: n8nio/n8n:latest
  displayName: Workflow Engine
  cloudlets: 12
  nodeGroup: app
  links: database:pg
  env:
    N8N_BASIC_AUTH_ACTIVE: true
    N8N_BASIC_AUTH_USER: ${globals.database_user}
    N8N_BASIC_AUTH_PASSWORD: ${globals.database_pass}
    N8N_ENCRYPTION_KEY: ${globals.secret_key}
    DB_TYPE: postgresdb
    DB_POSTGRESDB_DATABASE: workflow
    DB_POSTGRESDB_HOST: ${nodes.database[0].address}
    DB_POSTGRESDB_PORT: 5432
    DB_POSTGRESDB_USER: ${globals.database_user}
    DB_POSTGRESDB_PASSWORD: ${globals.database_pass}
- image: postgres:15
  cloudlets: 8
  nodeGroup: database
  displayName: Database Server
  env:
    POSTGRES_USER: ${globals.database_user}
    POSTGRES_PASSWORD: ${globals.database_pass}
    POSTGRES_DB: workflow

I tried different ways to reference the database IP but none work:

DB_POSTGRESDB_HOST: ${nodes.database[0].address}
DB_POSTGRESDB_HOST: ${nodes.database.first.address}
DB_POSTGRESDB_HOST: ${nodes.database.master.address}

The issue seems to be timing related. When the environment variables are set, the database node doesn’t exist yet so there’s no IP to assign. How can I set this variable after the database node is created? Any ideas would be helpful.

The variable gets resolved during template parsing before the nodes even exist - that’s why you’re hitting this issue. Don’t try to resolve the IP at install time. Just use the Docker linking you’ve already set up. You’ve got links: database:pg configured, so N8N can hit PostgreSQL directly using the hostname pg. Set your database host to DB_POSTGRESDB_HOST: pg and ditch the IP resolution stuff. Docker’s internal networking is way more reliable than hardcoded IPs anyway. I’ve run into similar timing issues with other apps, and using link aliases always works better than trying to do dynamic IP resolution in Jelastic.

I encountered the same challenge while deploying N8N on Jelastic recently. The problem arises because those node reference variables like ${nodes.database[0].address} are not available at the time environment variables are initialized. A solution that worked for me was creating a startup script to dynamically update the database configuration once all containers are up and running. You can configure your N8N node to run a script that verifies database connectivity and subsequently restarts it with the updated settings. Alternatively, employing actions like onAfterScaleOut or onAfterCloneNodes within your JPS can help to run scripts for updating environment variables post-deployment. This timing issue is quite a common occurrence in Jelastic setups involving multiple nodes, especially when one service depends on runtime information from another.

try usin the onAfterReturn action to set db vars after nodes are created. also, use the link alias like pg:5432 instead of the IP addr since you’ve defined the link in your app node config.