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

I’m running a self-hosted n8n workflow automation tool and need to automatically set up the admin user during deployment. Our team requires an API key for our CI/CD pipeline and automated testing processes.

Currently I have to manually complete the owner setup through the web interface every time we deploy. This creates problems for our automated deployment process.

Here’s my current Docker setup:

postgres:
  image: postgres:13
  ports:
    - "5433:5432"
  environment:
    POSTGRES_DB: workflow_db
    POSTGRES_USER: dbuser
    POSTGRES_PASSWORD: dbpass
    PGDATA: /var/lib/postgresql/data/pgdata
  volumes:
    - ./data/postgres/:/var/lib/postgresql/data
    - ./scripts/init:/docker-entrypoint-initdb.d:ro

workflow:
  image: docker.n8n.io/n8nio/n8n
  ports:
    - "5679:5678"
  environment:
    - DB_TYPE=postgresdb
    - DB_TABLE_PREFIX=workflow
    - DB_POSTGRESDB_DATABASE=workflow_db
    - DB_POSTGRESDB_HOST=postgres
    - DB_POSTGRESDB_PORT=5432
    - DB_POSTGRESDB_USER=dbuser
    - DB_POSTGRESDB_PASSWORD=dbpass
  volumes:
    - ./data/n8n/config:/home/node/.n8n
    - ./data/n8n/storage:/storage

Since I’m using PostgreSQL, I could potentially add SQL statements to create the admin user directly in the database. Has anyone found a way to automate this owner account creation process?

I encountered a similar issue with automating the setup of the admin user in n8n. Instead of handling it through the web interface each time, I found that leveraging environment variables in the Docker setup is a practical solution. By including the following variables in your Docker configuration, you can streamline the creation of the admin account:

[email protected]
N8N_OWNER_PASSWORD=your_secure_password
N8N_OWNER_FIRST_NAME=Admin
N8N_OWNER_LAST_NAME=User

This approach allows you to bypass the manual setup process entirely, creating the owner account on the first launch while safeguarding sensitive information. For the API keys, while you have to initiate that creation through the interface once, after that, it will remain in your database for future iterations, making it simpler for ongoing deployments.

The environment variable approach works great for newer n8n versions. But if you’re on an older version or want more control, try using n8n’s CLI commands in your deployment script instead. Once your containers are up, run n8n user:create with the right flags to create the owner account programmatically. You’ll get better error handling this way, and it integrates into CI/CD pipelines much cleaner. Just make sure to add a health check so the database is ready before creating the user. For API keys, I’d use n8n’s REST API endpoints after setup - lets you automate everything including key provisioning for your test workflows.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.