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

I’m running a self-hosted n8n setup in my infrastructure and need to automatically configure the admin user during deployment. This is important because I need the n8n API key for my CI/CD pipeline and automated testing workflows.

Here’s my current docker-compose setup with PostgreSQL:

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

automation:
  image: docker.n8n.io/n8nio/n8n
  ports:
    - "5679:5678"
  environment:
    - DB_TYPE=postgresdb
    - DB_TABLE_PREFIX=workflow
    - DB_POSTGRESDB_DATABASE=n8n
    - DB_POSTGRESDB_HOST=database
    - 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 have direct access to the PostgreSQL database, I’m wondering if I can create the admin account through SQL queries or environment variables. The official docs mention the setup process but don’t explain how to automate it. Has anyone found a way to skip the manual owner setup step?

I’ve encountered this issue while deploying n8n in various environments as well. You can leverage the N8N_OWNER_EMAIL and N8N_OWNER_PASSWORD environment variables directly in your docker-compose file. Simply add them to the environment section of your automation service, and n8n will take care of creating the owner account automatically upon the first startup, eliminating the need for any manual setup.

Typically, I define these variables in a .env file or pass them through deployment scripts. Once the owner account is established, you can easily retrieve the API key programmatically using the n8n REST API with the same credentials. This approach consistently works across container restarts and completely removes the manual step from the process.

One caveat to keep in mind is that these environment variables only function during the initial deployment when the database is empty. If you’re rebuilding your containers while retaining the database, existing user data will take precedence, and the environment variables will be disregarded.

I’ve had good luck using n8n’s CLI commands with an init script. Mount a startup script that runs n8n user:create --owner after the database connects but before your main app starts. This gives you way more control over timing and handles edge cases that environment variables miss. The real win is you can grab the API key right after creating the user through the same CLI. I pipe that output to a shared volume or send it to my config management system for CI/CD access. This beats direct database manipulation since it uses n8n’s built-in user logic and handles password hashing automatically - no need to mess with bcrypt yourself.

there’s another option if you’ve got database access. just insert the user straight into the users table with a hashed password. set globalRole to ‘owner’ and make sure you hash the password with bcrypt. this worked for me when the env vars failed for whatever reason.