|
| 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 | + |
| 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 | + |
| 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. |
0 commit comments