How can I automatically configure admin user for self-hosted n8n deployment?

I’m running a self-hosted n8n workflow automation tool in my infrastructure and need to automatically set up the admin user during deployment. My deployment process requires an API key from n8n to work with our CI/CD pipeline and production environment.

Here’s my current Docker setup using MySQL as the database:

mysql:
  image: mysql:8.0
  ports:
    - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: rootpass
    MYSQL_DATABASE: workflow_db
    MYSQL_USER: dbuser
    MYSQL_PASSWORD: dbpass
  volumes:
    - ./storage/mysql:/var/lib/mysql
    - ./scripts/init:/docker-entrypoint-initdb.d:ro

workflow:
  image: docker.n8n.io/n8nio/n8n
  ports:
    - "5679:5678"
  environment:
    - DB_TYPE=mysqldb
    - DB_TABLE_PREFIX=wf_
    - DB_MYSQL_DATABASE=workflow_db
    - DB_MYSQL_HOST=mysql
    - DB_MYSQL_PORT=3306
    - DB_MYSQL_USER=dbuser
    - DB_MYSQL_PASSWORD=dbpass
  volumes:
    - ./storage/n8n:/home/node/.n8n
    - ./shared:/shared

Since I have database access, I’m wondering if there’s a way to insert the admin user directly into the database or use some other automated approach. The manual setup process isn’t suitable for our automated deployments.

Another approach worth considering is using n8n’s REST API for automated user setup. After your containers are running, you can create a setup script that makes API calls to configure the initial admin account. The n8n API exposes an endpoint for owner setup that accepts POST requests with email and password parameters. I’ve implemented this in production by adding a health check loop in my deployment script that waits for n8n to be ready, then calls the setup endpoint programmatically. This gives you more control over the timing and error handling compared to environment variables. You can also retrieve the API key immediately after account creation through the same API session, which streamlines your CI/CD integration. The advantage is that you can handle setup failures gracefully and integrate this step directly into your existing deployment orchestration tools.

direct database insertion is def doable with ur mysql setup. just drop an sql script in ur init folder that adds the admin user to the user table with hashed pw. remember to hash the pw properly using bcrypt, or else login wont work right.

I faced a similar challenge when deploying n8n across multiple environments. The most reliable approach I found was using environment variables to preconfigure the owner account during the initial startup. You can set N8N_OWNER_EMAIL and N8N_OWNER_PASSWORD environment variables in your Docker configuration, which will automatically create the admin user on first launch without requiring manual intervention. Add these to your workflow service environment section: [email protected] and N8N_OWNER_PASSWORD=your_secure_password. This method works consistently and integrates well with secrets management systems. The user gets created during the initial database setup phase, so your CI/CD pipeline can immediately use API authentication without manual steps. Make sure to use strong passwords and consider rotating them through your deployment automation.