How can I auto-configure the owner for my self-hosted n8n instance?

I’m self-hosting n8n with a custom PostgreSQL setup using Docker Compose. How can I automatically set up the owner account to obtain the necessary API key for integration in both testing and production environments?

services:
  db_service:
    image: postgres:13-alpine
    ports:
      - "5433:5432"
    environment:
      USER: admin
      PASSWORD: adminpass
    volumes:
      - ./data/postgres:/var/lib/postgresql/data
      - ./init-scripts:/docker-entrypoint-initdb.d

  workflow_app:
    image: mycustom/n8n:latest
    ports:
      - "5679:5678"
    environment:
      - DATABASE_TYPE=postgresql
      - DATABASE_NAME=workflowdb
      - DATABASE_HOST=db_service
      - DATABASE_PORT=5432
      - DATABASE_USER=admin
      - DATABASE_PASSWORD=adminpass
    volumes:
      - ./n8n_data:/home/node/.n8n

hey, try adding a custom sql init script in your db container to create the owner if not exists. that auto inserts the api key entry at startup. works for both test an prod. give it a whirl!

I have encountered a similar issue with my self-hosted n8n setup. In my case, I’ve solved it by adding a custom startup script that queries the PostgreSQL database for the owner entry and creates it if it doesn’t exist, using environment variables to specify owner credentials. The script runs as a one-time initialization task before n8n launches, ensuring the API key is generated without manual intervention. This method proved robust across different environments, as it automatically adapts to both test and production configurations.