How can I automatically configure the admin account for a self-hosted n8n setup?

I’m running n8n on my own server as part of our system. We need to make sure the admin account is set up exactly how we want it. This is important because we need an n8n API key for our deployment process, tests, and live environment.

Here’s what our setup looks like:

database:
  image: postgres:current
  ports:
    - '5432:5432'
  env:
    POSTGRES_USER: dbuser
    POSTGRES_PASSWORD: dbpass
    PGDATA: /data/pgdata
  volumes:
    - ./storage/db/:/data
    - ./setup/pg:/setup:ro

workflow:
  image: docker.n8n.io/n8nio/n8n
  ports:
    - '5678:5678'
  env:
    - DB_TYPE=postgresdb
    - DB_PREFIX=n8n
    - DB_NAME=n8n
    - DB_HOST=database
    - DB_PORT=5432
    - DB_USER=dbuser
    - DB_PASS=dbpass
  volumes:
    - ./storage/n8n/data:/home/node/.n8n
    - ./storage/n8n/files:/files

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

hey dave, i’ve dealt with this before. you could try using environment variables to set up the admin account. something like:

[email protected]
N8N_PASSWORD=your_password
N8N_FIRST_NAME=Admin
N8N_LAST_NAME=User

add these to your workflow env section. n8n should pick em up and create the account on first run. hope this helps!

I’ve found a workaround that might solve your issue, Dave. Instead of relying solely on environment variables or the API, you could leverage Docker’s entrypoint override functionality. Create a custom entrypoint script that runs before n8n starts, setting up your admin account precisely.

Here’s a rough outline of what you could do:

  1. Write a bash script that uses n8n’s CLI to create the admin user
  2. Set this script as the entrypoint in your Docker configuration
  3. The script can then start n8n as usual once it’s done

This method gives you fine-grained control over the admin setup process and integrates smoothly with your existing Docker setup. It’s been reliable in my experience, especially when dealing with complex configurations or multiple environments.

Just remember to properly handle secrets and ensure your script is secure. Good luck with your setup!

I’ve encountered a similar challenge in my n8n deployments. While environment variables are a good start, they don’t offer complete control over the admin account setup. Consider using the n8n API to programmatically configure the account post-installation. You can create a script that runs after n8n starts, utilizing the /users endpoint to create or modify the admin user with exact specifications. This approach allows for more granular control and can be integrated into your deployment pipeline. Remember to secure your API calls and credentials appropriately. Additionally, exploring n8n’s command-line interface (CLI) options might provide alternative methods for initial setup automation.