How can I automate the owner account setup for a self-hosted n8n instance?

I’m running a self-hosted n8n instance as part of our system. We need to set up the owner account automatically to get an API key for our deployment pipeline and testing.

Here’s what our setup looks like:

database:
  image: postgres:13
  ports:
    - '5433:5432'
  env:
    POSTGRES_USER: admin
    POSTGRES_PASSWORD: secret123
    PGDATA: /data/postgres
  volumes:
    - ./data/db:/data/postgres
    - ./init:/docker-entrypoint-initdb.d:ro

workflow:
  image: n8nio/n8n:latest
  ports:
    - '5679:5678'
  env:
    - DB_TYPE=postgres
    - DB_PREFIX=n8n_
    - DB_DATABASE=workflow_db
    - DB_HOST=database
    - DB_PORT=5432
    - DB_USER=admin
    - DB_PASS=secret123
  volumes:
    - ./data/n8n:/home/node/.n8n
    - ./files:/workflow_files

Is there a way to add the owner account through SQL or some other method? The docs mention this step but don’t say how to automate it. Any ideas?

I’ve tackled this issue before in my own n8n setup. While there’s no built-in method to automate owner account creation, I found a workaround using SQL.

After your n8n instance is up, you can connect to the Postgres database and run an INSERT statement to create the owner account. Here’s the basic structure:

INSERT INTO public.user (email, password, firstName, lastName, role)
VALUES ('[email protected]', 'hashed_password', 'Admin', 'User', 'global:owner');

You’ll need to hash the password properly - n8n uses bcrypt. I usually create a small script to generate this hash.

To automate this, you could add a step in your deployment pipeline that waits for the n8n service to be ready, then executes this SQL. Be careful with credentials management though.

Remember to adjust the database name and table prefix if you’ve customized those. This approach has worked well for me in CI/CD environments.

hey there! i’ve actually found a neat trick for this. you can use the n8n CLI to create the owner account. just run this command after your container is up:

n8n user create --email [email protected] --password yourpassword --firstName Admin --lastName User

you can easily add this to a script in your deployment pipeline. hope this helps!

Having dealt with n8n setups myself, I can offer an alternative approach to automate the owner account creation. Instead of directly manipulating the database, consider using n8n’s REST API. You can make a POST request to the ‘/users’ endpoint once your instance is up and running.

Here’s a rough outline of the process:

  1. Wait for your n8n instance to fully start up.
  2. Send a POST request to ‘http://your-n8n-instance/api/v1/users’ with the necessary user details.
  3. The API will handle password hashing and proper role assignment.

You’ll need to implement this in your deployment script, perhaps using curl or a similar tool. This method avoids potential issues with direct database manipulation and leverages n8n’s built-in functionality.

Remember to secure your API key and consider implementing additional checks to prevent accidental user creation if the owner already exists.