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 set up the admin account in a specific way so we can get an API key for our deployment process, tests, and live environment. Here’s what our docker-compose setup looks like:

db:
  image: customdb:latest
  ports:
    - "1234:1234"
  environment:
    DB_USER: dbuser
    DB_PASSWORD: dbpass
    DB_DATA: /data/db
  volumes:
    - ./data:/data/db
    - ./init:/docker-init:ro
n8n:
  image: n8n/customized:latest
  ports:
    - "8080:8080"
  environment:
    - DB_TYPE=customdb
    - DB_PREFIX=n8n_
    - DB_NAME=n8ndb
    - DB_HOST=dbserver
    - DB_PORT=1234
    - DB_USER=dbuser
    - DB_PASSWORD=dbpass
  volumes:
    - ./n8n_data:/app/data
    - ./n8n_files:/app/files

We’re using a custom database, so maybe we could add the user with SQL? The n8n docs talk about setting up the owner, but don’t say how to do it automatically. Any ideas on how to make this happen without manual steps?

As someone who’s set up n8n in various environments, I can share a trick that’s worked well for me. Instead of messing with the database directly, you can use n8n’s CLI to create the admin user programmatically. Here’s what I do:

  1. Add a custom entrypoint script to your n8n service in the docker-compose file.
  2. In this script, use the n8n CLI to create the user if it doesn’t exist.

Here’s a sample entrypoint.sh:

#!/bin/sh
n8n user:create --email [email protected] --firstName Admin --lastName User --password YourSecurePassword --role global:owner

# Start n8n
exec node /usr/local/bin/n8n

Make sure to chmod +x the script and update your docker-compose to use it:

n8n:
  ...
  volumes:
    - ./entrypoint.sh:/entrypoint.sh
  entrypoint: /entrypoint.sh

This approach is flexible and works across different database setups. Just remember to secure your entrypoint script as it contains sensitive info.

hey luna23, i’ve dealt with this before. you could try adding a custom init script to ur db container. something like:

INSERT INTO n8n_user (email, password, firstname, lastname, role) VALUES ('[email protected]', 'hashedpassword', 'Admin', 'User', 'global:owner');

just make sure to hash the password properly. good luck!

I’ve encountered a similar challenge in my n8n deployments. One effective approach is to leverage environment variables in your docker-compose file. You can set variables for the admin email, password, and other details, then use n8n’s built-in environment variable support to configure the initial user.

Add something like this to your n8n service environment:

[email protected]
N8N_ADMIN_PASSWORD=securePa$$word
N8N_ADMIN_FIRSTNAME=Admin
N8N_ADMIN_LASTNAME=User

n8n will automatically create this user on first run if it doesn’t exist. This method avoids direct database manipulation and works seamlessly with n8n’s internal user management system. Remember to use strong, unique passwords and consider using Docker secrets for sensitive information in production environments.