Search code examples
docker-composen8n

Is there a way to programmatically set the owner account for a selfhosted n8n instance?


We are selfhosting an n8n instance as part of our tech stack and we would like to ensure that the owner setup is completed according to our specification, as we need an n8n API key to integrate into our deployment pipeline, testing and also production setup. This is the docker-compose config, with a custom postgres, so we potentially could add the user as an SQL statement.

db:
  image: postgres:latest
  ports:
    - "5432:5432"
  environment:
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: postgres
    PGDATA: /var/lib/postgresql/data/pgdata
  volumes:
    - ./DOCKER/db/:/var/lib/postgresql/data
    - ./config/pg:/docker-entrypoint-initdb.d:ro
n8n:
  image: docker.n8n.io/n8nio/n8n
  ports:
    - "5678:5678"
  environment:
    - DB_TYPE=postgresdb
    - DB_TABLE_PREFIX=n8n
    - DB_POSTGRESDB_DATABASE=n8n
    - DB_POSTGRESDB_HOST=db
    - DB_POSTGRESDB_PORT=5432
    - DB_POSTGRESDB_USER=postgres
    - DB_POSTGRESDB_PASSWORD=postgres
  volumes:
    - ./DOCKER/n8n/data:/home/node/.n8n
    - ./DOCKER/n8n/files:/files

The n8n documentation does mention this step, but no way to automate it.


Solution

  • It appears there is no official way to do this. What we now did is to do a pg_dump after setting up the root user, even though this isn't an optimal strategy by any means.