Raw
1 ---
2 sidebar_label: "db"
3 sidebar_position: 5
4 ---
5
6 import { Table, Note } from "docs-ui"
7
8 export const metadata = {
9 title: `db Commands - Medusa CLI Reference`,
10 }
11
12 # {metadata.title}
13
14 Commands in the Medusa CLI starting with `db:` perform actions on the database.
15
16 ## db\:setup
17
18 Creates a database for the Medusa application with the specified name, if it doesn't exist. Then, it runs migrations and syncs links.
19
20 It also updates your `.env` file with the database name.
21
22 ```bash npx2yarn
23 npx medusa db:setup --db <name>
24 ```
25
26 <Note title="Tip">
27
28 Use this command if you're setting up a Medusa project or database manually.
29
30 </Note>
31
32 ### Options
33
34 <Table>
35 <Table.Header>
36 <Table.Row>
37 <Table.HeaderCell>
38 Option
39 </Table.HeaderCell>
40 <Table.HeaderCell>
41 Description
42 </Table.HeaderCell>
43 <Table.HeaderCell>
44 Required
45 </Table.HeaderCell>
46 <Table.HeaderCell>
47 Default
48 </Table.HeaderCell>
49 </Table.Row>
50 </Table.Header>
51 <Table.Body>
52 <Table.Row>
53 <Table.Cell>
54
55 `--db <name>`
56
57 </Table.Cell>
58 <Table.Cell>
59
60 The database name.
61
62 </Table.Cell>
63 <Table.Cell>
64
65 Yes
66
67 </Table.Cell>
68 <Table.Cell>
69
70 \-
71
72 </Table.Cell>
73 </Table.Row>
74 <Table.Row>
75 <Table.Cell>
76
77 `--skip-links`
78
79 </Table.Cell>
80 <Table.Cell>
81
82 Skip syncing links to the database.
83
84 </Table.Cell>
85 <Table.Cell>
86
87 No
88
89 </Table.Cell>
90 <Table.Cell>
91
92 Links are synced by default.
93
94 </Table.Cell>
95 </Table.Row>
96 <Table.Row>
97 <Table.Cell>
98
99 `--execute-safe-links`
100
101 </Table.Cell>
102 <Table.Cell>
103
104 Skip prompts when syncing links and execute only safe actions.
105
106 </Table.Cell>
107 <Table.Cell>
108
109 No
110
111 </Table.Cell>
112 <Table.Cell>
113
114 Prompts are shown for unsafe actions, by default.
115
116 </Table.Cell>
117 </Table.Row>
118 <Table.Row>
119 <Table.Cell>
120
121 `--execute-all-links`
122
123 </Table.Cell>
124 <Table.Cell>
125
126 Skip prompts when syncing links and execute all (including unsafe) actions.
127
128 </Table.Cell>
129 <Table.Cell>
130
131 No
132
133 </Table.Cell>
134 <Table.Cell>
135
136 Prompts are shown for unsafe actions, by default.
137
138 </Table.Cell>
139 </Table.Row>
140 <Table.Row>
141 <Table.Cell>
142
143 `--no-interactive`
144
145 </Table.Cell>
146 <Table.Cell>
147
148 Disable the command's prompts.
149
150 </Table.Cell>
151 <Table.Cell>
152
153 No
154
155 </Table.Cell>
156 <Table.Cell>
157
158 \-
159
160 </Table.Cell>
161 </Table.Row>
162 <Table.Row>
163 <Table.Cell>
164
165 `--skip-search`
166
167 </Table.Cell>
168 <Table.Cell>
169
170 Skip creating, altering, and dropping search indexes. This option is added starting from
171 [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1).
172
173 </Table.Cell>
174 <Table.Cell>
175
176 No
177
178 </Table.Cell>
179 <Table.Cell>
180
181 The command migrates search indexes by default if your application registers the [Search Module](../../../infrastructure-modules/search/page.mdx),
182 which is the default since [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1).
183
184 </Table.Cell>
185 </Table.Row>
186 <Table.Row>
187 <Table.Cell>
188
189 `--execute-safe-search`
190
191 </Table.Cell>
192 <Table.Cell>
193
194 Skip prompts when migrating search indexes and leave every index that no definition declares any more in place. This option is added starting from
195 [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1).
196
197 </Table.Cell>
198 <Table.Cell>
199
200 No
201
202 </Table.Cell>
203 <Table.Cell>
204
205 Prompts are shown for unsafe actions, by default.
206
207 </Table.Cell>
208 </Table.Row>
209 <Table.Row>
210 <Table.Cell>
211
212 `--execute-all-search`
213
214 </Table.Cell>
215 <Table.Cell>
216
217 Skip prompts when migrating search indexes and drop every index that no definition declares any more. This option is added starting from
218 [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1).
219
220 </Table.Cell>
221 <Table.Cell>
222
223 No
224
225 </Table.Cell>
226 <Table.Cell>
227
228 Prompts are shown for unsafe actions, by default.
229
230 </Table.Cell>
231 </Table.Row>
232 </Table.Body>
233 </Table>
234
235 ---
236
237 ## db\:create
238
239 Creates a database for the Medusa application with the specified name, if it doesn't exist.
240
241 It also updates your `.env` file with the database name.
242
243 ```bash npx2yarn
244 npx medusa db:create --db <name>
245 ```
246
247 <Note title="Tip">
248
249 Use this command if you want to only create a database.
250
251 </Note>
252
253 ### Options
254
255 <Table>
256 <Table.Header>
257 <Table.Row>
258 <Table.HeaderCell>
259 Option
260 </Table.HeaderCell>
261 <Table.HeaderCell>
262 Description
263 </Table.HeaderCell>
264 <Table.HeaderCell>
265 Required
266 </Table.HeaderCell>
267 <Table.HeaderCell>
268 Default
269 </Table.HeaderCell>
270 </Table.Row>
271 </Table.Header>
272 <Table.Body>
273 <Table.Row>
274 <Table.Cell>
275
276 `--db <name>`
277
278 </Table.Cell>
279 <Table.Cell>
280
281 The database name.
282
283 </Table.Cell>
284 <Table.Cell>
285
286 Yes
287
288 </Table.Cell>
289 <Table.Cell>
290
291 \-
292
293 </Table.Cell>
294 </Table.Row>
295 <Table.Row>
296 <Table.Cell>
297
298 `--no-interactive`
299
300 </Table.Cell>
301 <Table.Cell>
302
303 Disable the command's prompts.
304
305 </Table.Cell>
306 <Table.Cell>
307
308 No
309
310 </Table.Cell>
311 <Table.Cell>
312
313 \-
314
315 </Table.Cell>
316 </Table.Row>
317 </Table.Body>
318 </Table>
319
320 ---
321
322 ## db\:generate
323
324 Generate a migration file for the latest changes in one or more modules.
325
326 ```bash npx2yarn
327 npx medusa db:generate <module_names...>
328 ```
329
330 ### Arguments
331
332 <Table>
333 <Table.Header>
334 <Table.Row>
335 <Table.HeaderCell>
336 Argument
337 </Table.HeaderCell>
338 <Table.HeaderCell>
339 Description
340 </Table.HeaderCell>
341 <Table.HeaderCell>
342 Required
343 </Table.HeaderCell>
344 </Table.Row>
345 </Table.Header>
346 <Table.Body>
347 <Table.Row>
348 <Table.Cell>
349
350 `module_names`
351
352 </Table.Cell>
353 <Table.Cell>
354
355 The name of one or more modules (separated by spaces) to generate migrations for. For example, `brand`.
356
357 </Table.Cell>
358 <Table.Cell>
359
360 Yes
361
362 </Table.Cell>
363 </Table.Row>
364 </Table.Body>
365 </Table>
366
367 ---
368
369 ## db\:migrate
370
371 Run the latest migrations to reflect changes on the database, sync link definitions with the database, and run migration data scripts.
372
373 ```bash npx2yarn
374 npx medusa db:migrate
375 ```
376
377 <Note title="Tip">
378
379 Use this command if you've updated the Medusa packages, or you've created customizations and want to reflect them in the database.
380
381 </Note>
382
383 ### Options
384
385 <Table>
386 <Table.Header>
387 <Table.Row>
388 <Table.HeaderCell>
389 Option
390 </Table.HeaderCell>
391 <Table.HeaderCell>
392 Description
393 </Table.HeaderCell>
394 <Table.HeaderCell>
395 Required
396 </Table.HeaderCell>
397 <Table.HeaderCell>
398 Default
399 </Table.HeaderCell>
400 </Table.Row>
401 </Table.Header>
402 <Table.Body>
403 <Table.Row>
404 <Table.Cell>
405
406 `--skip-links`
407
408 </Table.Cell>
409 <Table.Cell>
410
411 Skip syncing links to the database.
412
413 </Table.Cell>
414 <Table.Cell>
415
416 No
417
418 </Table.Cell>
419 <Table.Cell>
420
421 Links are synced by default.
422
423 </Table.Cell>
424 </Table.Row>
425 <Table.Row>
426 <Table.Cell>
427
428 `--skip-scripts`
429
430 </Table.Cell>
431 <Table.Cell>
432
433 Skip running data migration scripts. This option is added starting from
434 [Medusa v2.3.0](https://github.com/medusajs/medusa/releases/tag/v2.3.0).
435
436 </Table.Cell>
437 <Table.Cell>
438
439 No
440
441 </Table.Cell>
442 <Table.Cell>
443
444 Data migration scripts are run by default starting from
445 [Medusa v2.3.0](https://github.com/medusajs/medusa/releases/tag/v2.3.0).
446
447 </Table.Cell>
448 </Table.Row>
449 <Table.Row>
450 <Table.Cell>
451
452 `--execute-safe-links`
453
454 </Table.Cell>
455 <Table.Cell>
456
457 Skip prompts when syncing links and execute only safe actions.
458
459 </Table.Cell>
460 <Table.Cell>
461
462 No
463
464 </Table.Cell>
465 <Table.Cell>
466
467 Prompts are shown for unsafe actions, by default.
468
469 </Table.Cell>
470 </Table.Row>
471 <Table.Row>
472 <Table.Cell>
473
474 `--execute-all-links`
475
476 </Table.Cell>
477 <Table.Cell>
478
479 Skip prompts when syncing links and execute all (including unsafe) actions.
480
481 </Table.Cell>
482 <Table.Cell>
483
484 No
485
486 </Table.Cell>
487 <Table.Cell>
488
489 Prompts are shown for unsafe actions, by default.
490
491 </Table.Cell>
492 </Table.Row>
493 <Table.Row>
494 <Table.Cell>
495
496 `--skip-search`
497
498 </Table.Cell>
499 <Table.Cell>
500
501 Skip creating, altering, and dropping search indexes. This option is added starting from
502 [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1).
503
504 </Table.Cell>
505 <Table.Cell>
506
507 No
508
509 </Table.Cell>
510 <Table.Cell>
511
512 The command migrates search indexes by default if your application registers the [Search Module](../../../infrastructure-modules/search/page.mdx),
513 which is the default since [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1).
514
515 </Table.Cell>
516 </Table.Row>
517 <Table.Row>
518 <Table.Cell>
519
520 `--execute-safe-search`
521
522 </Table.Cell>
523 <Table.Cell>
524
525 Skip prompts when migrating search indexes and leave every index that no definition declares any more in place. This option is added starting from
526 [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1).
527
528 </Table.Cell>
529 <Table.Cell>
530
531 No
532
533 </Table.Cell>
534 <Table.Cell>
535
536 Prompts are shown for unsafe actions, by default.
537
538 </Table.Cell>
539 </Table.Row>
540 <Table.Row>
541 <Table.Cell>
542
543 `--execute-all-search`
544
545 </Table.Cell>
546 <Table.Cell>
547
548 Skip prompts when migrating search indexes and drop every index that no definition declares any more. This option is added starting from
549 [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1).
550
551 </Table.Cell>
552 <Table.Cell>
553
554 No
555
556 </Table.Cell>
557 <Table.Cell>
558
559 Prompts are shown for unsafe actions, by default.
560
561 </Table.Cell>
562 </Table.Row>
563 </Table.Body>
564 </Table>
565
566 ---
567
568 ## db\:migrate\:search
569
570 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](../../../infrastructure-modules/search/page.mdx), which is the default since [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1). So, run it on its own to migrate the search indexes without touching the database.
571
572 ```bash npx2yarn
573 npx medusa db:migrate:search
574 ```
575
576 <Note>
577
578 This command is available since [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1). Learn more about index migrations in the [Reindexing and Migrations guide](../../../infrastructure-modules/search/reindexing/page.mdx).
579
580 </Note>
581
582 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.
583
584 ### Options
585
586 <Table>
587 <Table.Header>
588 <Table.Row>
589 <Table.HeaderCell>
590 Option
591 </Table.HeaderCell>
592 <Table.HeaderCell>
593 Description
594 </Table.HeaderCell>
595 <Table.HeaderCell>
596 Required
597 </Table.HeaderCell>
598 <Table.HeaderCell>
599 Default
600 </Table.HeaderCell>
601 </Table.Row>
602 </Table.Header>
603 <Table.Body>
604 <Table.Row>
605 <Table.Cell>
606
607 `--execute-safe-search`
608
609 </Table.Cell>
610 <Table.Cell>
611
612 Skip prompts and leave every index that no definition declares any more in place.
613
614 </Table.Cell>
615 <Table.Cell>
616
617 No
618
619 </Table.Cell>
620 <Table.Cell>
621
622 Prompts are shown for unsafe actions, by default.
623
624 </Table.Cell>
625 </Table.Row>
626 <Table.Row>
627 <Table.Cell>
628
629 `--execute-all-search`
630
631 </Table.Cell>
632 <Table.Cell>
633
634 Skip prompts and drop every index that no definition declares any more.
635
636 </Table.Cell>
637 <Table.Cell>
638
639 No
640
641 </Table.Cell>
642 <Table.Cell>
643
644 Prompts are shown for unsafe actions, by default.
645
646 </Table.Cell>
647 </Table.Row>
648 </Table.Body>
649 </Table>
650
651 ---
652
653 ## db\:rollback
654
655 Revert the last migrations run on one or more modules.
656
657 ```bash npx2yarn
658 npx medusa db:rollback <module_names...>
659 ```
660
661 ### Arguments
662
663 <Table>
664 <Table.Header>
665 <Table.Row>
666 <Table.HeaderCell>
667 Argument
668 </Table.HeaderCell>
669 <Table.HeaderCell>
670 Description
671 </Table.HeaderCell>
672 <Table.HeaderCell>
673 Required
674 </Table.HeaderCell>
675 </Table.Row>
676 </Table.Header>
677 <Table.Body>
678 <Table.Row>
679 <Table.Cell>
680
681 `module_names`
682
683 </Table.Cell>
684 <Table.Cell>
685
686 The name of one or more modules (separated by spaces) to rollback their migrations for. For example, `brand`.
687
688 </Table.Cell>
689 <Table.Cell>
690
691 Yes
692
693 </Table.Cell>
694 </Table.Row>
695 </Table.Body>
696 </Table>
697
698 ---
699
700 ## db\:sync-links
701
702 Sync the database with the link definitions in your application, including the definitions in Medusa's modules.
703
704 ```bash npx2yarn
705 npx medusa db:sync-links
706 ```
707
708 ### Options
709
710 <Table>
711 <Table.Header>
712 <Table.Row>
713 <Table.HeaderCell>
714 Option
715 </Table.HeaderCell>
716 <Table.HeaderCell>
717 Description
718 </Table.HeaderCell>
719 <Table.HeaderCell>
720 Required
721 </Table.HeaderCell>
722 <Table.HeaderCell>
723 Default
724 </Table.HeaderCell>
725 </Table.Row>
726 </Table.Header>
727 <Table.Body>
728 <Table.Row>
729 <Table.Cell>
730
731 `--execute-safe`
732
733 </Table.Cell>
734 <Table.Cell>
735
736 Skip prompts when syncing links and execute only safe actions.
737
738 </Table.Cell>
739 <Table.Cell>
740
741 No
742
743 </Table.Cell>
744 <Table.Cell>
745
746 Prompts are shown for unsafe actions, by default.
747
748 </Table.Cell>
749 </Table.Row>
750 <Table.Row>
751 <Table.Cell>
752
753 `--execute-all`
754
755 </Table.Cell>
756 <Table.Cell>
757
758 Skip prompts when syncing links and execute all (including unsafe) actions.
759
760 </Table.Cell>
761 <Table.Cell>
762
763 No
764
765 </Table.Cell>
766 <Table.Cell>
767
768 Prompts are shown for unsafe actions, by default.
769
770 </Table.Cell>
771 </Table.Row>
772 </Table.Body>
773 </Table>
774
775 ---
776
777 ## Reset the Database
778
779 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.
780
781 <Note title="Warning" type="warning">
782
783 Dropping the database permanently deletes all data. Make sure to back up any data you want to keep before running these commands.
784
785 </Note>
786
787 For example, using the `psql` command:
788
789 ```bash
790 psql -c "DROP DATABASE <db_name>;"
791 ```
792
793 Then, run `db:setup` to create the database and run all migrations:
794
795 ```bash npx2yarn
796 npx medusa db:setup --db <db_name>
797 ```