Can the owner account for a self-hosted n8n instance be set up automatically?

I’m running a self-hosted n8n instance as part of our tech stack. We need to make sure the owner setup is done just right. We want to get an n8n API key for our deployment pipeline, testing, and production setup.

Here’s what our docker-compose setup looks like:

db:
  image: customdb:latest
  ports:
    - "6543:6543"
  environment:
    DB_USER: admin
    DB_PASSWORD: securepass
    DATA_DIR: /data/customdb
  volumes:
    - ./DATA/customdb/:/data/customdb
    - ./setup/db:/setup-scripts:ro

n8n_service:
  image: n8n-custom:latest
  ports:
    - "8080:8080"
  environment:
    - DATABASE_TYPE=customdb
    - TABLE_PREFIX=n8n_
    - DB_NAME=n8n_data
    - DB_HOST=customdb
    - DB_PORT=6543
    - DB_USER=admin
    - DB_PASSWORD=securepass
  volumes:
    - ./DATA/n8n/config:/n8n/config
    - ./DATA/n8n/storage:/n8n/storage

We’re using a custom database setup. Maybe we could add the user with an SQL command? The n8n docs talk about setting up the owner, but they don’t say how to do it automatically. Any ideas on how to make this part of our setup process?

As someone who’s worked extensively with n8n, I can share a method that’s worked well for me. Instead of modifying n8n’s core code, which can be risky, I’ve had success using a custom init container in Kubernetes (or a startup script in Docker).

This container runs before your main n8n container and uses curl commands to interact with n8n’s API. It checks if an owner exists, creates one if not, and generates an API key. You’ll need to ensure your n8n container is fully up before running these commands.

Here’s a rough outline of what the script might do:

  1. Wait for n8n to be ready (health check)
  2. Check for existing owner
  3. If no owner, create one
  4. Generate API key

You can then store the API key in a secret or config map for your deployment pipeline to use. This approach keeps your n8n setup clean and allows for easy updates.

I’ve encountered this issue before. One approach that worked for me was leveraging n8n’s REST API. You could create a separate initialization script that runs after n8n starts up. This script would use the /users endpoint to check if an owner exists, and if not, create one using predefined credentials.

For the API key, once the owner is set up, you can use the /users/:id/api-key endpoint to generate it programmatically. Store these credentials securely, perhaps as environment variables or in a separate configuration file.

Remember to ensure your initialization script runs only after n8n and the database are fully operational. You might need to implement a retry mechanism or health check to handle timing issues during startup.

hey zoeStar42, i’ve dealt with similar setups. for automatic owner setup, you could try adding an environment variable like N8N_OWNER_EMAIL and N8N_OWNER_PASSWORD to ur n8n_service. then use a custom entrypoint script that checks if owner exists, if not create it using those vars. might need to tweak n8n code a bit tho. good luck!