> ## Documentation Index
> Fetch the complete documentation index at: https://docs.beam.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# Managed Databases

> Create Postgres, Redis, MySQL, and MongoDB services and connect your apps

You can run Postgres, Redis, MySQL, or MongoDB on Beam. Each database includes a durable disk, generated credentials, and a TLS connection.

| CLI kind | Engine | Default disk | Connection variable |
| - | - | - | - |
| `postgres` | Postgres 16 | 10Gi | `DATABASE_URL` |
| `redis` | Redis 7 | 5Gi | `REDIS_URL` |
| `mysql` | MySQL 8 | 10Gi | `DATABASE_URL` |
| `mongo` | MongoDB 7 | 10Gi | `DATABASE_URL` |

## Create a Database

```bash theme={null}
beam db postgres create web-db
beam db redis create cache
beam db mysql create analytics-db
beam db mongo create documents-db
beam db list
```

Each command creates a separate database with 1 CPU and 512 MB of memory. Names must be unique in your workspace. Use `--cpu`, `--memory`, `--database`, or `--username` to customize it.

To supply a password, use `--password-from-env VARIABLE_NAME` or `--password-stdin`.

Databases are also available in the dashboard and through MCP's `create_database` tool.

## Connect an App

Pass the database URL as an [environment reference](/v2/hosting/references):

```bash theme={null}
beam deploy --dockerfile Dockerfile --name web --port 8000 \
  --env DATABASE_URL='${{db.web-db.DATABASE_URL}}' \
  --env REDIS_URL='${{db.cache.REDIS_URL}}'
```

Python apps accept the same references in `env`:

```python theme={null}
from beam import function

@function(env={"DATABASE_URL": "${{db.web-db.DATABASE_URL}}"})
def inspect_configuration():
    import os
    return {"database_configured": bool(os.environ.get("DATABASE_URL"))}
```

For an existing app, edit its environment variables in the dashboard or use MCP's `connect_services`. Both deploy a new version.

## Connect from Your Machine

Print a connection command for Postgres or Redis:

```bash theme={null}
beam db postgres connect web-db --psql
beam db redis connect cache --redis-cli
```

Run the printed command with `psql` or `redis-cli` installed. The output contains credentials.

For other clients, use `beam db KIND credentials NAME`. Redis also supports `connect --node-redis` and `--ioredis` for TLS connection examples.

## Serverless or Always On

Databases shut down after 300 idle seconds by default. To avoid cold starts, keep one replica running:

```bash theme={null}
beam db postgres scale web-db --always-on
beam db postgres scale web-db --serverless
beam db postgres scale web-db --cpu 2 --memory 2Gi
```

Use `--min-replicas 1` at creation for always-on mode. Each database has one writable replica; clients should reconnect after restarts or resource changes.

## Credentials and Lifecycle

```bash theme={null}
beam db postgres status web-db
beam logs web-db --since 30m
beam db postgres rotate web-db
```

Rotation restarts the database and apps using its generated secrets. Update any connection strings you copied manually.

`beam db postgres delete web-db` deletes the service and credentials, but **keeps the durable disk**. Use `beam disk list` to find it. See [disk snapshots](/v2/data/durable-disks#snapshots) for backups.
