Raw
1 import { getOsShortcut, Kbd, InlineIcon, Table } from "docs-ui"
2 import { EllipsisHorizontal } from "@medusajs/icons"
3
4 export const metadata = {
5 title: `Database`,
6 }
7
8 # {metadata.title}
9
10 In this guide, you'll learn about the managed database service that Medusa provides as part of your project environments on Cloud.
11
12 ## Managed Database Service on Cloud
13
14 Medusa provisions and manages databases for each of your environments on Cloud.
15
16 Each project environment has a dedicated PostgreSQL database. These databases are entirely isolated from one another.
17
18 This isolation allows you to safely make or test changes in environments without affecting one another, especially the production environment and its data.
19
20 ![Diagram showcasing isolation between environment databases](https://res.cloudinary.com/dza7lstvk/image/upload/v1750174759/Cloud/databases-cloud_hzjyrn.jpg)
21
22 ---
23
24 ## Cloud Database Configurations
25
26 Medusa automatically configures the database for your environments on Cloud. So, you don't need to worry about setting up the database, configuring it with your Medusa application, or monitoring performance and availability on your end.
27
28 ---
29
30 ## Cloud Database Backups
31
32 Medusa provides automatic backups for your environment databases on Cloud. The retention period depends on your plan:
33
34 <Table>
35 <Table.Header>
36 <Table.Row>
37 <Table.HeaderCell>
38 Plan
39 </Table.HeaderCell>
40 <Table.HeaderCell>
41 Retention Period
42 </Table.HeaderCell>
43 </Table.Row>
44 </Table.Header>
45 <Table.Body>
46 <Table.Row>
47 <Table.Cell>
48 Develop
49 </Table.Cell>
50 <Table.Cell>
51 Not supported
52 </Table.Cell>
53 </Table.Row>
54 <Table.Row>
55 <Table.Cell>
56 Launch
57 </Table.Cell>
58 <Table.Cell>
59 7 days
60 </Table.Cell>
61 </Table.Row>
62 <Table.Row>
63 <Table.Cell>
64 Scale
65 </Table.Cell>
66 <Table.Cell>
67 14 days
68 </Table.Cell>
69 </Table.Row>
70 <Table.Row>
71 <Table.Cell>
72 Enterprise
73 </Table.Cell>
74 <Table.Cell>
75 14 days
76 </Table.Cell>
77 </Table.Row>
78 </Table.Body>
79 </Table>
80
81 You can upgrade your plan as explained in the [Manage Plans](../billing/plans/page.mdx#change-plan) guide.
82
83 To access the database backups of an environment:
84
85 <Note title="Tip">
86
87 You can access the database settings from the [command palette](../command-palette/page.mdx). Press <Kbd>{getOsShortcut()}</Kbd> + <Kbd>K</Kbd>, then select **Storage**.
88
89 </Note>
90
91 1. If you're in a different organization, [switch to the organization](../organizations/page.mdx#switch-organization).
92 2. Click **Projects** in the sidebar and select the project that contains the environment you want to view.
93 3. In the project's dashboard, click on the name of the environment. For example, "Production".
94 4. Click **Database** in the sidebar under the environment's section.
95
96 The **Database Backups** section shows a table of available backups with the following columns:
97
98 - **Generated At**: When the backup was created.
99 - **Source**: The backup origin. **Scheduled** backups are created automatically; **manual** backups are triggered by you.
100 - **Size**: The backup file size.
101
102 ### Download a Backup
103
104 To download a backup to your device:
105
106 1. In the **Database Backups** table, find the backup you want to download.
107 2. Click the <InlineIcon Icon={EllipsisHorizontal} alt="three-dots" /> icon and choose **Download**.
108
109 This will start a download of the backup file to your device. It is a `.dump` file that you can restore to a PostgreSQL database using tools like `pg_restore`. For example:
110
111 ```bash
112 pg_restore --no-acl --no-owner --no-privileges --clean --if-exists -d 'postgres://postgres:secret@localhost:5432/mydatabase' mybackup.dump
113 ```
114
115 ### Restore a Backup
116
117 <Note type="warning">
118
119 Restoring a backup replaces your current database with the backup data. This action is irreversible.
120
121 </Note>
122
123 To restore your database from a backup:
124
125 1. In the **Database Backups** table, find the backup you want to restore.
126 2. Click the <InlineIcon Icon={EllipsisHorizontal} alt="three-dots" /> icon and choose **Restore**.
127 3. In the confirmation dialog, type `restore` to confirm the action.
128 4. Click **Restore backup** to confirm.
129
130 Medusa notifies you when the restore completes or if an error occurs.
131
132 <Note title="Tip">
133
134 You can't restore a backup while a database import is in progress.
135
136 </Note>
137
138 ### Trigger a Manual Backup
139
140 A manual backup is triggered when you export a database dump from the environment settings. Learn how to do that in the [Import/Export Cloud Database Dumps](#importexport-cloud-database-dumps) section below.
141
142 ---
143
144 ## Connect to a Cloud Database
145
146 In some cases, you may need to access your Cloud project's database directly, such as to export or import data.
147
148 Medusa gives you read access to your Cloud project's database, which is useful when you need to export data, perform analytics, or troubleshoot issues.
149
150 ### Get Read-Only Connection String
151
152 To get the read-only connection string for an environment's database:
153
154 <Note title="Tip">
155
156 You can access the database settings from the [command palette](../command-palette/page.mdx). Press <Kbd>{getOsShortcut()}</Kbd> + <Kbd>K</Kbd>, then select **Storage**.
157
158 </Note>
159
160 1. If you're in a different organization, [switch to the organization](../organizations/page.mdx#switch-organization).
161 2. Click **Projects** in the sidebar and select the project that contains the environment you want to view.
162 3. In the project's dashboard, click on the name of the environment. For example, "Production".
163 4. Click **Database** in the sidebar under the environment's section.
164 5. In the **Database read-only connection string** section, toggle read access on if it is not already enabled.
165 6. Copy the read-only connection string from the code block.
166
167 Then, you can connect to your database using a PostgreSQL client. For example, using the `psql` command-line tool:
168
169 ```bash
170 psql "postgresql://..."
171 ```
172
173 After connecting to the database, you can run SQL queries to interact with your data.
174
175 ### Rotate Read Password
176
177 You can rotate the password for the read-only connection string from the **Database read-only connection string** section. To do that:
178
179 1. Click the **Rotate password** button.
180 2. Confirm the action in the dialog that opens by clicking **Rotate**.
181
182 Medusa generates a new read-only connection string. Update any tools or scripts that use the old connection string after rotating.
183
184 ### Get Write Connection String
185
186 All environments have a read-only connection string by default. This allows you to connect to the database and query data, but it doesn't allow you to modify the data or database schema. This is a security measure to prevent accidental changes to your database.
187
188 For advanced use cases where you need write access to your environment's database, such as importing data or performing maintenance tasks, Medusa allows organization owners to enable database write access.
189
190 <Note title="Important" type="warning">
191
192 Database write access should be used with caution as it allows direct modification of your database outside of your application. Only enable this feature if you understand the implications and need direct write access for specific tasks.
193
194 </Note>
195
196 To enable database write access for an environment:
197
198 <Note title="Tip">
199
200 You can access the database settings from the [command palette](../command-palette/page.mdx). Press <Kbd>{getOsShortcut()}</Kbd> + <Kbd>K</Kbd>, then select **Storage**.
201
202 </Note>
203
204 1. If you're in a different organization, [switch to the organization](../organizations/page.mdx#switch-organization).
205 2. Click **Projects** in the sidebar and select the project that contains the environment you want to view.
206 3. In the project's dashboard, click on the name of the environment. For example, "Production".
207 4. Click **Database** in the sidebar under the environment's section.
208 5. In the **Database write-access connection string** section, toggle the switch to enable write access.
209 6. Once enabled, copy the write connection string from the code block that appears.
210
211 <Note title="Permission Requirements">
212
213 Only organization owners can enable or disable database write access. If you can see the option to toggle write access, but it's disabled, contact your organization's owner to request access.
214
215 </Note>
216
217 Then, you can connect to your database using a PostgreSQL client with the write connection string. For example, using the `psql` command-line tool:
218
219 ```bash
220 psql "postgresql://..."
221 ```
222
223 ### Rotate Write Password
224
225 Organization owners can rotate the password for the write connection string from the **Database write-access connection string** section. To do that:
226
227 1. Click the **Rotate password** button.
228 2. Confirm the action in the dialog that opens by clicking **Rotate**.
229
230 Medusa generates a new write connection string. Update any tools or scripts that use the old connection string after rotating.
231
232 ### Restrict Database Access by IP Address
233
234 The **IP Allowlist** section of an environment's database settings lets you control which IP addresses can connect to the environment's database. You can restrict public access to the database entirely, or allow access only from specific IP addresses.
235
236 To restrict public access to an environment's database:
237
238 <Note title="Tip">
239
240 You can access the database settings from the [command palette](../command-palette/page.mdx). Press <Kbd>{getOsShortcut()}</Kbd> + <Kbd>K</Kbd>, then select **Storage**.
241
242 </Note>
243
244 1. If you're in a different organization, [switch to the organization](../organizations/page.mdx#switch-organization).
245 2. Click **Projects** in the sidebar and select the project that contains the environment you want to configure.
246 3. In the project's dashboard, click on the name of the environment. For example, "Production".
247 4. Click **Database** in the sidebar under the environment's section.
248 5. In the **Public Access** section, toggle the **Restrict public access** switch.
249
250 Medusa saves the change as soon as you toggle the switch.
251
252 To allow access to the database from specific IP addresses:
253
254 1. In the **Public Access** section, enter the IP addresses in the text area, with one IP address per line.
255 2. Click **Save**.
256
257 After saving, you can connect to the database using the [read-only](#get-read-only-connection-string) or [write](#get-write-connection-string) connection string from the allowed IP addresses.
258
259 ---
260
261 ## Import/Export Cloud Database Dumps
262
263 Medusa allows you to export and import database dumps for any environment on Cloud. This is useful for seeding the database with initial data, migrating from other hosting platforms, or debugging issues locally.
264
265 To import or export a database dump for an environment:
266
267 <Note title="Tip">
268
269 You can access the database settings from the [command palette](../command-palette/page.mdx). Press <Kbd>{getOsShortcut()}</Kbd> + <Kbd>K</Kbd>, then select **Storage**.
270
271 </Note>
272
273 1. If you're in a different organization, [switch to the organization](../organizations/page.mdx#switch-organization).
274 2. Click **Projects** in the sidebar and select the project that contains the environment you want to view.
275 3. In the project's dashboard, click on the name of the environment. For example, "Production".
276 4. Click **Database** in the sidebar under the environment's section.
277
278 In the "Database dump" section, you can either:
279
280 - **Import**: Upload a database dump file to import data into the environment's database. You can generate a database dump using `pg_dump`. For example:
281
282 ```bash
283 pg_dump -Fc -d postgres://postgres:secret@localhost:5432/mydatabase -f mydata.dump
284 ```
285
286 <Note>
287
288 Make sure you're generating the database dump from PostgreSQL v16 and that [pg_dump](https://www.postgresql.org/docs/16/app-pgdump.html) is compatible with that version.
289
290 </Note>
291
292 - **Export**: Download the current database as a dump file. You'll then receive a notification once the export is ready for download. Then, you can restore the database dump locally using `pg_restore`. For example:
293
294 ```bash
295 pg_restore --no-acl --no-owner --no-privileges --clean --if-exists -d 'postgres://postgres:secret@localhost:5432/mydatabase' mydata.dump
296 ```
297
298 ---
299
300 ## Change Preview Environment Database
301
302 By default, when Medusa creates a [preview environment](../environments/preview/page.mdx) on Cloud, it replicates the Production database. This allows you to test changes in a safe environment that mirrors production, without affecting the live data.
303
304 Cloud also allows you to configure which environment's database to replicate the preview database from. For example, you can replicate the Staging environment's database instead of Production.
305
306 Learn more in the [Preview Environments](../environments/preview/page.mdx#manage-shared-previews-settings) guide.
307
308 ---
309
310 ## Running Migrations on Cloud
311
312 Medusa automatically runs database migrations for every deployment on Cloud. This ensures that your database schema is always up-to-date with the latest changes in your Medusa application.
313
314 When you deploy changes to an environment on Cloud, Medusa:
315
316 1. Runs any [pending database migrations](!docs!/learn/fundamentals/data-models/write-migration).
317 2. [Syncs links](!docs!/learn/fundamentals/module-links).
318 3. [Runs pending data migration scripts](!docs!/learn/fundamentals/data-models/write-migration#data-migration-scripts).
319
320 So, if you define new database migrations in your module, define new links, or add data migration scripts, Medusa will apply those changes automatically when you deploy the changes to Cloud.
321
322 You don't need to add a `predeploy` script to your `package.json`, or run `medusa db:migrate` in your `start` script, to apply migrations on Cloud. If you're moving a self-hosted application that runs migrations this way, remove the `medusa db:migrate` command from your `start` script.
323
324 ### How Data Migration Scripts Run on Cloud
325
326 Each environment has its own database, so Medusa runs a data migration script once per environment. So:
327
328 - A script that completed in a previous deployment doesn't run again in later deployments.
329 - A script that throws an error, or that was interrupted, runs again in the next deployment until it completes successfully.
330
331 ### Run One-Off Data Scripts on Cloud
332
333 To run a one-off data task on a Cloud environment, such as seeding data or filling a configuration table, create a [data migration script](!docs!/learn/fundamentals/data-models/write-migration#data-migration-scripts) in the `src/migration-scripts` directory instead of a [custom CLI script](!docs!/learn/fundamentals/custom-cli-scripts) that you run with `medusa exec`.
334
335 For example:
336
337 ```ts title="src/migration-scripts/seed-config.ts"
338 import { ExecArgs } from "@medusajs/framework/types"
339
340 export default async function seedConfig({
341 container,
342 }: ExecArgs) {
343 // perform the data changes
344 // by running a workflow
345 }
346 ```
347
348 When you deploy the commit that adds the script, Medusa runs it during the deployment and doesn't run it again in that environment's later deployments.
349
350 <Note>
351
352 Test the script locally with `npx medusa db:migrate` and deploy it to a [preview environment](../environments/preview/page.mdx) before merging it to your production branch.
353
354 </Note>