Environment variable not getting PostgreSQL IP address in Jelastic JPS deployment for N8N

I’m having trouble setting up N8N using a Jelastic JPS configuration file. The main issue is that the environment variable for the database host isn’t getting the correct PostgreSQL IP address.

I need to pass the PostgreSQL node’s IP to the DB_POSTGRESDB_HOST variable using something like ${nodes.database[0].address}. When I manually set this variable through the web interface it works fine, but it doesn’t work when defined in the JPS file.

type: install
name: N8N Automation Tool
version: 1.1
id: n8n-deploy
homepage: https://n8n.io/

globals:
  app_user: automation-${fn.random(999)}
  app_pass: ${fn.password}
  secret_key: app${fn.password}

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

I’ve tried different ways to reference the database IP:

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

I think the issue is timing related. The database node doesn’t have an IP yet when the environment variables are being set. How can I assign the IP address after the PostgreSQL container is created and running?

Any ideas on how to solve this?

You’re right about the timing issue. The ${nodes.database[0].address} variable gets evaluated during manifest processing, before the database container actually exists and gets an IP. I hit this same problem with a similar setup last year.

Use the onAfterServiceStart action to update the environment variable after all nodes are running. Add this to your JPS file:

onAfterServiceStart:
  - cmd[app]: |
      export DB_HOST=$(getent hosts node${nodes.database[0].id}-${env.domain} | awk '{print $1}')
      echo "DB_POSTGRESDB_HOST=$DB_HOST" >> /opt/node/.env
      supervisorctl restart n8n

Or just use the internal hostname instead of the IP address. PostgreSQL containers in Jelastic can be reached using node${nodes.database[0].id}-${env.domain} - it resolves automatically once the database is running. Way more reliable than trying to grab the IP during initial deployment.

Had this exact problem deploying multi-container setups in Jelastic. It’s a timing issue - environment variables get resolved during initial parsing before containers even exist. I fixed it by using the actions section with onAfterInstallJps instead of setting the DB host in the initial env block.

Remove DB_POSTGRESDB_HOST from your app node’s env section and add this:

actions:
  setDbConnection:
    - api: env.control.AddContainerEnvVars
      nodeGroup: app
      vars:
        DB_POSTGRESDB_HOST: ${nodes.database[0].intIP}
    - restartContainers:
        nodeGroup: app

onAfterInstallJps:
  - setDbConnection

Use intIP instead of address - gives you the internal network IP which is more stable. The restart makes sure N8N picks up the new environment variable. This waits until all nodes are fully up before setting database connection details.

Same headache here with jelastic jps timing. Switch to onAfterCloneNodes or onAfterRedeployContainer - they work better. Also, just use database.${env.domain} to hit your db node directly. Way easier than chasing IP addresses that keep changing.