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. This key is important for our deployment process and testing. We’re using Docker Compose with a custom Postgres database. I was thinking maybe we could add the user with an SQL command, but I’m not sure how to do it.

Here’s a bit of our setup:

n8n:
  image: custom/n8n:latest
  environment:
    - DB_TYPE=postgres
    - DB_NAME=workflow
    - DB_HOST=database
    - DB_PORT=5432
    - DB_USER=admin
    - DB_PASS=secret123
  volumes:
    - ./data:/app/data
    - ./files:/app/files

database:
  image: postgres:13
  environment:
    - POSTGRES_DB=workflow
    - POSTGRES_USER=admin
    - POSTGRES_PASSWORD=secret123
  volumes:
    - ./db:/var/lib/postgresql/data

I checked the n8n docs, but they only talk about setting up the admin account manually. Is there a way to do this automatically?

For your automated n8n admin setup, consider leveraging environment variables in your Docker Compose file. You can define admin credentials as environment variables and pass them to n8n during container initialization. This approach is more secure than hardcoding values or using SQL commands.

Add these to your n8n service configuration:

environment:
  - [email protected]
  - N8N_PASSWORD=your_secure_password
  - N8N_ENCRYPTION_KEY=your_encryption_key

N8N will use these to create the initial admin account on first run. Remember to use secure values and consider using Docker secrets for sensitive data in production environments. This method aligns well with DevOps practices and allows for easier management across different deployment scenarios.

I’ve faced a similar challenge with automating n8n admin setup. One effective method I’ve used is creating a custom entrypoint script. You can add this script to your Dockerfile or mount it as a volume.

The script would wait for n8n to start, then use the n8n CLI to create the admin user. Here’s a basic example:

#!/bin/sh
set -e

# Wait for n8n to start
until nc -z localhost 5678; do
  sleep 1
done

# Create admin user
n8n user create --email [email protected] --password securePassword --firstName Admin --lastName User

# Start n8n
exec n8n start

Remember to adjust the script permissions and update your Docker Compose file to use this custom entrypoint. This approach gives you more control and flexibility over the admin account creation process.

hey alexr1990, u could try using n8n’s REST API to create the admin account programmatically. write a script that runs after n8n starts up, sends a POST request to /api/users endpoint with the account details. just make sure to secure ur API key afterwards. good luck with ur setup!