CLI commands for n8n

N8n includes a command line interface (CLI) that allows you to perform tasks using the CLI rather than the n8n editor. This includes starting workflows, exporting and importing workflows and credentials.

Executing CLI commands

You can use CLI commands with self-hosted n8n. Depending on how you installed n8n, there are differences in how you run the commands:
Npm: n8n
You can use the command directly. This document uses this feature in the examples below.
Docker: n8n
The commands are available inside the Docker container:
docker exec -u node -it <n8n-container-name> <n8n-cli-command>

Start Workflow

You can start a workflow directly using the CLI. Run a saved workflow by ID:
The workflow ID can be found in the URL.
n8n execute --id <ID> n8n execute --id NGgtX67xWcwx2a3I
You can run it by entering it directly into Exec in the docker below.

Change the active status of a workflow

You can change the active state of a workflow using the CLI.
Restart required
This command operates on the n8n database. If you run it while n8n is running, the changes will not take effect until n8n is restarted.
Set the workflow's ID-based active state to false:
n8n update:workflow --id=<ID> --active=false
Set the workflow's ID-based active state to true:
n8n update:workflow --id=<ID> --active=true
Set the active state to false for all workflows:
n8n update:workflow --all --active=false
Set active status to true for all workflows:
n8n update:workflow --all --active=true

Export Workflows and Credentials

You can export workflows and credentials from n8n using the CLI.
Command flags:
Flag
Description
--Help
Help prompt.
--All
Exports all workflows/credentials.
--Backup
Sets --all --pretty --separate for backups. You can optionally set --output.
--Id
The ID of the workflow to export.
--Output
Outputs file name or directory if using separate files.
--Pretty
Formats the output in an easier to read fashion.
--Separate
Exports one file per workflow (useful for versioning). Must set a directory using --output.
--Decrypted
Exports the credentials in a plain text format.

Workflow

Export all workflows to standard output (terminal):
n8n export:workflow --all
Export workflows by ID and specify an output file name:
n8n export:workflow --id=<ID> --output=file.json
Export all workflows to a specific directory in a single file:
n8n export:workflow --all --output=backups/latest/file.json
Export all workflows to a specific directory using the --backup flag (details above):
n8n export:workflow --backup --output=backups/latest/

Credentials

Export all credentials to standard output (terminal):
n8n export:credentials --all
Export credentials by ID and specify an output file name:
n8n export:credentials --id=<ID> --output=file.json
Export all credentials from a single file to a specific directory:
n8n export:credentials --all --output=backups/latest/file.json
Export all credentials to a specific directory using the --backup flag (details above):
n8n export:credentials --backup --output=backups/latest/
Export all credentials in plain text format. This can be used to migrate from one installation to another with different secret keys in the configuration file.
Sensitive information
All sensitive information can be found in the file.
n8n export:credentials --all --decrypted --output=backups/decrypted.json

Get Workflow and Credentials

You can use the CLI to retrieve workflows and credentials from n8n.
ID Update
When exporting workflows and credentials, n8n also exports the IDs. If you have workflows and credentials with the same IDs in your existing database, they will be overwritten. To avoid this, delete or change the IDs before importing.
Available flags:
Flag
Description
--Help
Help prompt.
--Input
Input file name or directory if you use --separate.
--ProjectId
Import the workflow or credentials to the specified project. Can't be used with --userId .
--Separate
Imports *.json files from directory provided by --input.
--UserId
Import the workflow or credentials to the specified user. Can't be used with --projectId .
Migrating to SQLite
N8n limits workflow and credential names to 128 characters, but SQLite does not enforce a size limit.
In this case , errors such as data being too long for the column name may occur during the import process .
In this case, you can edit the name in the n8n interface and edit the JSON file directly before exporting or importing it again.

Workflow

Import a workflow from a specific file:
n8n import:workflow --input=file.json
Get all workflow files as JSON from a specified directory:
n8n import:workflow --separate --input=backups/latest/

Credentials

Retrieving credentials from a specific file:
n8n import:credentials --input=file.json
Get all credentials files as JSON from a specified directory:
n8n import:credentials --separate --input=backups/latest/

License

Erase

Delete existing licenses from n8n's database and reset n8n to default functionality:
n8n license:clear

Information

Displays information about existing licenses:
n8n license:info

User Management

You can reset user management using the n8n CLI. This will return user management to its default state. It will remove all user accounts.
Use this method if you have forgotten your password and your SMTP is not set up to perform password resets by email.
n8n user-management:reset

Disable MFA for a user

If a user has lost their recovery code, you can disable their MFA with this command. The user can then log in again and re-enable MFA.
n8n mfa:disable --email=johndoe@example.com

Disable LDAP

You can reset your LDAP settings using the command below.
n8n ldap:reset

Security Audit

You can run a security audit on your n8n instances to detect common security issues.
n8n audit

Workflow Postgres DB Connection

The database structure can be found here.
To connect to Postgres running with a docker-compose.yml file, you can use the following methods:
If you want to run it in a separate terminal like iterm2, click “Open in external terminal⁠” on the right.
If it comes out as /#, it means you are connected to a cell inside the container.
To connect to a Postgres database from inside a container, you need to use the psql command. To use the psql command, type:
: POSTGRES_USER set in docker-compose.yml.
: POSTGRES_DB set in docker-compose.yml.
psql -U <username> -d <database>
The initial settings in docker-compose.yml are as follows:
POSTGRES_USER=root
POSTGRES_PASSWORD=password
POSTGRES_DB=n8n
So, you can type psql -U root -d n8n .
The \dt command displays a list of tables in the current database.
1. Check the table structure
To check the structure of a specific table (column information, data types, etc.), use \d .
\D user
1.
Retrieve data from a table
To retrieve data from a table, you can use a regular SQL query. For example, to retrieve all data from the user table:
SELECT * FROM user;
1.
Add, Edit, Delete
a.
INSERT INTO user (id, name, email) VALUES (1, 'Alice', 'alice@example.com');
b.
UPDATE user SET name = 'Bob' WHERE id = 1;
c.
DELETE FROM user WHERE id = 1;
2.
Exit the shell
It ends with \q .