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

I’m running an n8n instance on our own servers as part of our tech stack. We need to set up the admin account in a specific way to get an API key for our deployment pipeline and testing. I’ve been looking at ways to do this automatically.

Here’s what our docker-compose setup looks like:

db:
  image: customdb:latest
  ports:
    - "6543:6543"
  environment:
    DB_USER: admin
    DB_PASSWORD: secretpass
    DATA_DIR: /data/customdb
  volumes:
    - ./DATA/customdb/:/data/customdb
    - ./setup/db:/setup-scripts:ro

n8n_service:
  image: my-n8n-image:latest
  ports:
    - "8080:8080"
  environment:
    - DATABASE_TYPE=customdb
    - DATABASE_PREFIX=n8n_
    - DATABASE_NAME=n8n_data
    - DATABASE_HOST=customdb
    - DATABASE_PORT=6543
    - DATABASE_USER=admin
    - DATABASE_PASSWORD=secretpass
  volumes:
    - ./DATA/n8n/config:/app/n8n/config
    - ./DATA/n8n/storage:/app/storage

I’ve checked the docs but couldn’t find a way to automate the admin setup. Any ideas on how to do this programmatically? Maybe through a database script or API call?

For automating the admin account setup in your self-hosted n8n instance, consider leveraging the n8n CLI. You can create a shell script that runs after your containers are up, utilizing the n8n create-user command.

Here’s a basic example of what the command might look like:

n8n create-user --email [email protected] --password yourSecurePassword --firstName Admin --lastName User --role owner

Incorporate this into your deployment process, perhaps as a post-startup script. Ensure you’re using the latest n8n version, as CLI capabilities have expanded in recent releases. Remember to secure your script and any files containing sensitive information.

hey there! have u considered using environment variables for the admin setup? smth like N8N_ADMIN_EMAIL and N8N_ADMIN_PASSWORD in ur docker-compose file? that way u could set those values during deployment. might need to tweak ur n8n image to read those vars on startup tho. just a thought!

I’ve encountered a similar challenge with automating n8n admin setup. One approach that worked well for me was using a custom entrypoint script in the Docker container. You can create a bash script that checks if the admin account exists, and if not, uses the n8n API to create it.

Here’s a rough outline of what the script could do:

  1. Wait for the n8n service to fully start
  2. Use curl to check if the admin account exists
  3. If not, use the API to create the account with your desired settings

You’d need to modify your Dockerfile to include this script and set it as the entrypoint. It’s a bit more work upfront, but it gives you full control over the setup process and can be easily adapted for other initialization tasks too.

Remember to handle errors and add appropriate logging for troubleshooting. Good luck with your setup!