---
title: Add a database
description: Create a PostgreSQL, MySQL, MongoDB or Redis database in an environment and connect your application to it with the secrets Kapten generates.
---

1. **Choose the engine**

    In your environment, create a database and pick **PostgreSQL**, **MySQL**, **MongoDB** or **Redis**.

2. **Name it**

    Enter its name, the engine version, and optionally the name of the logical database to create in it. The name decides the names of its [secrets](#connect-your-application).

3. **Choose provisioning and access**

    - **In cluster** runs the database on the environment's cluster. **Managed** provisions your cloud provider's database service.
    - **Private** keeps it reachable from inside the cluster only. Choose **Public** only if something outside the cluster must connect to it.

4. **Size it**

    Set its CPU, memory and disk.

5. **Choose the options**

    Turn on disk encryption, high availability and backups as needed, then review and create the database.

## Connect your application

Kapten adds the database's credentials to the environment as secrets. For a database named `main`, every component of the environment gets:

```bash
KAPTEN_DATABASE_MAIN_HOST
KAPTEN_DATABASE_MAIN_USERNAME
KAPTEN_DATABASE_MAIN_PASSWORD
KAPTEN_DATABASE_MAIN_DATABASE
KAPTEN_DATABASE_MAIN_CONNECTION_STRING
```

Read them in your code, or map them to the name your framework expects with a component variable. Redeploy applications that were running before the database existed so they pick up the new secrets.

```js
// Node.js, with the pg package
import pg from "pg";

const pool = new pg.Pool({
  connectionString: process.env.KAPTEN_DATABASE_MAIN_CONNECTION_STRING,
});
```

See [Database secrets](/concepts/variables-and-secrets#database-secrets) for how the names are formed.
