db Commands - Medusa CLI Reference
Commands in the Medusa CLI starting with db: perform actions on the database.
db:setup#
Creates a database for the Medusa application with the specified name, if it doesn't exist. Then, it runs migrations and syncs links.
It also updates your .env file with the database name.
Options#
Option | Description | Required | Default |
|---|---|---|---|
| The database name. | Yes | - |
| Skip syncing links to the database. | No | Links are synced by default. |
| Skip prompts when syncing links and execute only safe actions. | No | Prompts are shown for unsafe actions, by default. |
| Skip prompts when syncing links and execute all (including unsafe) actions. | No | Prompts are shown for unsafe actions, by default. |
| Disable the command's prompts. | No | - |
| Skip creating, altering, and dropping search indexes. This option is added starting from Medusa v2.21.1. | No | The command migrates search indexes by default if your application registers the Search Module, which is the default since Medusa v2.21.1. |
| Skip prompts when migrating search indexes and leave every index that no definition declares any more in place. This option is added starting from Medusa v2.21.1. | No | Prompts are shown for unsafe actions, by default. |
| Skip prompts when migrating search indexes and drop every index that no definition declares any more. This option is added starting from Medusa v2.21.1. | No | Prompts are shown for unsafe actions, by default. |
db:create#
Creates a database for the Medusa application with the specified name, if it doesn't exist.
It also updates your .env file with the database name.
Options#
Option | Description | Required | Default |
|---|---|---|---|
| The database name. | Yes | - |
| Disable the command's prompts. | No | - |
db:generate#
Generate a migration file for the latest changes in one or more modules.
Arguments#
Argument | Description | Required |
|---|---|---|
| The name of one or more modules (separated by spaces) to generate migrations for. For example, | Yes |
db:migrate#
Run the latest migrations to reflect changes on the database, sync link definitions with the database, and run migration data scripts.
Options#
Option | Description | Required | Default |
|---|---|---|---|
| Skip syncing links to the database. | No | Links are synced by default. |
| Skip running data migration scripts. This option is added starting from Medusa v2.3.0. | No | Data migration scripts are run by default starting from Medusa v2.3.0. |
| Skip prompts when syncing links and execute only safe actions. | No | Prompts are shown for unsafe actions, by default. |
| Skip prompts when syncing links and execute all (including unsafe) actions. | No | Prompts are shown for unsafe actions, by default. |
| Skip creating, altering, and dropping search indexes. This option is added starting from Medusa v2.21.1. | No | The command migrates search indexes by default if your application registers the Search Module, which is the default since Medusa v2.21.1. |
| Skip prompts when migrating search indexes and leave every index that no definition declares any more in place. This option is added starting from Medusa v2.21.1. | No | Prompts are shown for unsafe actions, by default. |
| Skip prompts when migrating search indexes and drop every index that no definition declares any more. This option is added starting from Medusa v2.21.1. | No | Prompts are shown for unsafe actions, by default. |
db:migrate:search#
Create, alter, and drop the physical search indexes that match the search index definitions in your application. The db:migrate command runs this command for you if your application registers the Search Module, which is the default since Medusa v2.21.1. So, run it on its own to migrate the search indexes without touching the database.
The command prompts you to select which indexes to drop when no definition declares them any more. Pass one of the options below to skip the prompt, which you must do when you run the command in a CI pipeline or another environment without a terminal. Otherwise, the command leaves those indexes in place.
Options#
Option | Description | Required | Default |
|---|---|---|---|
| Skip prompts and leave every index that no definition declares any more in place. | No | Prompts are shown for unsafe actions, by default. |
| Skip prompts and drop every index that no definition declares any more. | No | Prompts are shown for unsafe actions, by default. |
db:rollback#
Revert the last migrations run on one or more modules.
Arguments#
Argument | Description | Required |
|---|---|---|
| The name of one or more modules (separated by spaces) to rollback their migrations for. For example, | Yes |
db:sync-links#
Sync the database with the link definitions in your application, including the definitions in Medusa's modules.
Options#
Option | Description | Required | Default |
|---|---|---|---|
| Skip prompts when syncing links and execute only safe actions. | No | Prompts are shown for unsafe actions, by default. |
| Skip prompts when syncing links and execute all (including unsafe) actions. | No | Prompts are shown for unsafe actions, by default. |
Reset the Database#
The Medusa CLI does not provide a dedicated command to drop or reset the database. To reset your database, drop it manually using PostgreSQL tools, then run db:setup to recreate and reinitialize it.
For example, using the psql command:
Then, run db:setup to create the database and run all migrations: