Skip to content

Commit 5317f16

Browse files
authored
docs: add cloud database guides (medusajs#12758)
* docs: add cloud database guides * fixes * fixes * small fix
1 parent 4443d74 commit 5317f16

File tree

6 files changed

+194
-7
lines changed

6 files changed

+194
-7
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
export const metadata = {
2+
title: `Database`,
3+
}
4+
5+
# {metadata.title}
6+
7+
In this guide, you'll learn about the managed database service that Cloud provides as part of your project environments.
8+
9+
## Managed Database Service
10+
11+
Cloud offers a managed PostgreSQL database service for your project environments. Each environment has its own dedicated database that is automatically provisioned when the environment is created.
12+
13+
So, when you create a new project, Cloud creates a production database for the production environment. If you create a staging environment, Cloud creates a separate database for that environment as well.
14+
15+
This isolation between environment databases allows you to safely make or test changes in environments without affecting one another, especially the production environment and its data.
16+
17+
![Diagram showcasing isolation between environment databases](https://res.cloudinary.com/dza7lstvk/image/upload/v1750174759/Cloud/databases-cloud_hzjyrn.jpg)
18+
19+
---
20+
21+
## Database Configurations
22+
23+
Cloud automatically configures the database for your environments. 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.
24+
25+
---
26+
27+
## Database Backups
28+
29+
Cloud provides automatic backups for your environment databases. These backups are retained for 14 days and allow you to restore your database to a previous state, if needed.
30+
31+
If you need to restore a database backup, you can [contact support](#) to request a point-in-time recovery. This is useful if you accidentally delete data or need to revert changes made to the database.
32+
33+
---
34+
35+
## Accessing the Database
36+
37+
In some cases, you may need to access the database directly, such as to export or import data. However, since Cloud is a managed service, you can't directly access your database. Cloud also doesn't expose the database connection details.
38+
39+
This section provides alternative ways to interact with your database based on your needs.
40+
41+
### Option 1: Database Dump
42+
43+
Cloud allows you to export and import database dumps for any environment. This is useful for seeding the database with initial data, migrating from other hosting platforms, or debugging issues locally.
44+
45+
To import or export a database dump for an environment:
46+
47+
1. From the [organization's dashboard](../organizations/page.mdx), click on the environment's project.
48+
2. In the project's dashboard, click on the name of the environment. For example, "Production".
49+
3. In the environment's dashboard, click on the "Settings" tab.
50+
4. Scroll down to the "Database dump" section.
51+
52+
![Database dump section highlighted in the environment settings](https://res.cloudinary.com/dza7lstvk/image/upload/v1750169625/Cloud/CleanShot_2025-06-17_at_17.13.21_2x_l7vw3w.png)
53+
54+
In this section, you can either:
55+
56+
- **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:
57+
58+
```bash
59+
pg_dump -Fc -d postgres://postgres:secret@localhost:5432/mydatabase -f mydata.dump
60+
```
61+
62+
<Note>
63+
64+
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.
65+
66+
</Note>
67+
68+
- **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:
69+
70+
```bash
71+
pg_restore --no-acl --no-owner --no-privileges --clean --if-exists -d 'postgres://postgres:secret@localhost:5432/mydatabase' mydata.dump
72+
```
73+
74+
### Option 2: Custom API Routes
75+
76+
In some cases, you may need more flexibility interacting with the database. For example, you might need to retrieve specific data for analysis, or update data coming from an external source.
77+
78+
To interact with an environment's database with more flexibility, you can:
79+
80+
- [Create a custom API route](!docs!/learn/fundamentals/api-routes) that retrieves or updates data in the database.
81+
- Make sure to create the API route under the `/admin` prefix, or [protect the API route with an API key](!docs!/learn/fundamentals/api-routes/protected-routes).
82+
- Create a secret API key for a user using the [Medusa Admin](!user-guide!/settings/developer/secret-api-keys#create-secret-api-key).
83+
- Call the API route to perform database operations, [passing the API key in the header](!api!/admin#2-api-token). For example, to send the request in a Node.js application:
84+
85+
```ts
86+
const api_key_token = "YOUR_API_KEY"
87+
fetch(`https://my-project.medusajs.app/admin/my-route`, {
88+
headers: {
89+
Authorization: `Basic ${
90+
Buffer.from(`${api_key_token}:`).toString("base64")
91+
}`,
92+
},
93+
})
94+
```
95+
96+
Where:
97+
98+
- `YOUR_API_KEY` is the secret API key you created.
99+
- `my-project.medusajs.app` is the [environment's URL](../deployments/page.mdx#find-environments-url).
100+
- `my-route` is the path to the custom API route you created.
101+
102+
This approach allows you to perform any database operations you need, such as retrieving or updating data, without directly accessing the database.
103+
104+
---
105+
106+
## Change Preview Environment Database
107+
108+
By default, when Cloud creates a [preview environment](../environments/page.mdx#preview-environments), it replicates the Production database. This allows you to test changes in a safe environment that mirrors production, without affecting the live data.
109+
110+
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.
111+
112+
Learn more in the [Environments](../environments/page.mdx#manage-shared-previews-settings) guide.

www/apps/cloud/app/deployments/page.mdx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Table } from "docs-ui"
1+
import { Table, InlineIcon } from "docs-ui"
2+
import { EllipsisHorizontal } from "@medusajs/icons"
23

34
export const metadata = {
45
title: `Deployments`,
@@ -273,4 +274,32 @@ To redeploy a deployment:
273274

274275
This will trigger the redeployment process for the selected deployment. The deployment will go through [the same lifecycle](#deployment-statuses-and-lifecycle) as a new deployment.
275276

276-
Once the redeployment is complete, the deployment's status will change to "Live" and it will become the new live version of the environment.
277+
Once the redeployment is complete, the deployment's status will change to "Live" and it will become the new live version of the environment.
278+
279+
---
280+
281+
## Change Deployment Rules
282+
283+
For each environment, you can change the rules that trigger a new deployment. For example, you can change the branch that the environment is connected to, which changes when a new deployment is created.
284+
285+
<Note>
286+
287+
Changing the branch associted with an environment only works for long-lived environments.
288+
289+
</Note>
290+
291+
To change the deployment rules for an environment:
292+
293+
1. [Go to the environment's dashboard](#find-environment-deployments).
294+
2. Click on the "Settings" tab.
295+
3. Scroll down to the "Deployment rules" section. You'll find a `branch` rule in this section.
296+
297+
![Deployment rules section in the environment's settings tab](https://res.cloudinary.com/dza7lstvk/image/upload/v1750171235/Cloud/CleanShot_2025-06-17_at_17.40.13_2x_v0vn1k.png)
298+
299+
4. You can edit it by clicking the <InlineIcon Icon={EllipsisHorizontal} alt="three-dots" /> icon and choosing "Edit" from the dropdown.
300+
5. In the side window that opens, you can change the branch that the environment is connected to. For example, you can change it from `main` to `staging` to create a new deployment every time you push a commit to the `staging` branch.
301+
6. Click "Save" to apply the changes.
302+
303+
The deployment rules will take effect for next deployments. For example, if you change the branch to `staging`, the next deployment will be created from the latest commit in the `staging` branch.
304+
305+
![Change environment branch in the environment's settings tab](https://res.cloudinary.com/dza7lstvk/image/upload/v1750170517/Cloud/CleanShot_2025-06-17_at_17.28.25_2x_nfx79a.png)

www/apps/cloud/app/environments/page.mdx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,27 @@ You can rename the file to `.env` and use it in your local development.
262262

263263
---
264264

265-
## Manage Environment's Deployment Rules
265+
## Change Environment's Branch
266+
267+
<Note>
268+
269+
You can't change the branch of preview environments. This section only applies to long-lived environments, such as Production or Staging.
270+
271+
</Note>
266272

267273
In an environment's "Settings" tab, you can manage the deployment rules for the environment. Deployment rules allow you to control how and when deployments are triggered for the environment.
268274

269-
Learn more in the [Deployments](#) guide.
275+
To change the branch associated with an environment using deployment rules:
276+
277+
1. In the [environment's dashboard](#open-environment-dashboard), click on the "Settings" tab.
278+
2. In the "Deployment rules" section, click the <InlineIcon Icon={EllipsisHorizontal} alt="three-dots" /> icon at the right side of the `branch` rule.
279+
3. Choose "Edit" from the dropdown menu.
280+
4. In the side window that opens, you can change the branch associated with the environment.
281+
5. Once you're done, click the "Save" button.
282+
283+
Changes will take effect the next time you push a commit to the new branch. The environment will be redeployed with the latest changes from the new branch.
284+
285+
![Change environment branch in the environment's settings tab](https://res.cloudinary.com/dza7lstvk/image/upload/v1750170517/Cloud/CleanShot_2025-06-17_at_17.28.25_2x_nfx79a.png)
270286

271287
---
272288

@@ -323,7 +339,7 @@ To open a preview environment's dashboard:
323339
1. Go to its [project's dashboard](../projects/page.mdx#open-project-dashboard).
324340
2. In the "Previews" card, click on the preview environment you want to view.
325341

326-
Then, you can manage the preview environment just like any other long-lived environment, as mentioned throughout this guide. You can [manage its environment variables](#manage-environment-variables), [deployment rules](#manage-environments-deployment-rules), and [import/export its database dump](#import-and-export-environments-database-dump).
342+
Then, you can manage the preview environment just like any other long-lived environment, as mentioned throughout this guide. You can [manage its environment variables](#manage-environment-variables) and [import/export its database dump](#import-and-export-environments-database-dump).
327343

328344
![Preview environment dashboard](https://res.cloudinary.com/dza7lstvk/image/upload/v1749817815/Cloud/CleanShot_2025-06-13_at_15.29.58_2x_h9vn34.png)
329345

www/apps/cloud/generated/edit-dates.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ export const generatedEditDates = {
22
"app/page.mdx": "2025-06-12T12:31:48.681Z",
33
"app/organization/page.mdx": "2025-06-12T14:43:20.772Z",
44
"app/projects/page.mdx": "2025-06-17T11:20:16.174Z",
5-
"app/environments/page.mdx": "2025-06-17T11:12:50.824Z",
5+
"app/environments/page.mdx": "2025-06-17T14:29:55.288Z",
66
"app/deployments/page.mdx": "2025-06-17T11:17:03.236Z",
77
"app/organizations/page.mdx": "2025-06-17T10:48:50.969Z",
8-
"app/notifications/page.mdx": "2025-06-17T12:29:18.819Z"
8+
"app/notifications/page.mdx": "2025-06-17T12:29:18.819Z",
9+
"app/database/page.mdx": "2025-06-17T14:34:57.104Z"
910
}

www/apps/cloud/generated/sidebar.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ export const generatedSidebars = [
5555
}
5656
]
5757
},
58+
{
59+
"loaded": true,
60+
"isPathHref": true,
61+
"type": "category",
62+
"title": "Resources",
63+
"initialOpen": true,
64+
"children": [
65+
{
66+
"loaded": true,
67+
"isPathHref": true,
68+
"type": "link",
69+
"title": "Database",
70+
"path": "/database",
71+
"children": []
72+
}
73+
]
74+
},
5875
{
5976
"loaded": true,
6077
"isPathHref": true,

www/apps/cloud/sidebar.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ export const sidebar = [
3939
},
4040
],
4141
},
42+
{
43+
type: "category",
44+
title: "Resources",
45+
initialOpen: true,
46+
children: [
47+
{
48+
type: "link",
49+
title: "Database",
50+
path: "/database",
51+
},
52+
],
53+
},
4254
{
4355
type: "category",
4456
title: "Monitoring & Support",

0 commit comments

Comments
 (0)