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 tech stack. We need to set up the owner account automatically to fit our needs. This is important because we use the n8n API key in our deployment pipeline, testing, and production setup.

We’re using a custom PostgreSQL database with our setup. I thought maybe we could add the user with an SQL statement, but I’m not sure how to do this.

Here’s a simplified version of our setup:

db:
  image: custom-postgres
  environment:
    DB_USER: admin
    DB_PASS: secret

n8n:
  image: custom-n8n
  environment:
    DB_TYPE: postgres
    DB_HOST: custom-db
    DB_USER: admin
    DB_PASS: secret

I checked the n8n docs, but they only mention manual setup. Is there a way to automate this process? Any help would be great!

hey there! have u tried using the n8n api itself? u could make a POST request to /api/users during ur deployment process. this way u can create the owner account programmatically. just make sure to include all the necessary user details in the request body. it’s pretty straightforward and doesn’t require messing with the database directly

Having dealt with this issue before, I can share a reliable approach. Consider utilizing environment variables in your n8n container configuration. You can set variables like N8N_EMAIL, N8N_PASSWORD, and N8N_FIRST_NAME. Then, modify your n8n startup script to check if these variables are set. If they are, use the n8n CLI to create the owner account automatically before starting the main n8n process. This method integrates seamlessly with your existing setup and doesn’t require direct database manipulation. It’s also more secure than hardcoding credentials in scripts or SQL statements. Remember to document this process for your team to ensure smooth deployments and maintenance.

I’ve encountered a similar situation when automating the n8n owner account setup. Instead of modifying the database directly with SQL, I’ve found that leveraging n8n’s CLI can be a more effective method. In my case, I implemented a shell script that runs immediately after the n8n container starts. This script calls the n8n CLI to create the owner account, specifying the email, password, and user details. Integrating this into the custom image ensures that the account is created reliably during each deployment. Just remember to secure your script and use environment variables for sensitive information.