# Connect Your Database

> Using **Cube Cloud** or your own **Cube Core** deployment instead of
> Embeddable's built-in Cube? See [Bring your own
> Cube](https://docs.embeddable.com/data/bring-your-own-cube) — this page (and the rest of the **Connect
> your data** section) does not apply to you.

You can connect one or more databases to Embeddable. We call these **Connections**.

- **Read-only access:** Embeddable only reads from your database - it never writes to it.
- **Connection options:** Connect directly, via [SSH](https://docs.embeddable.com/data/connect-your-database#ssh-connections-bastion-host), or via [Amazon PrivateLink](https://docs.embeddable.com/data/connect-your-database#amazon-privatelink).
- **IP whitelisting:** Restrict access to your database by allowing only Embeddable’s fixed IPs ([learn more](https://docs.embeddable.com/deployment/deployment-regions#ips-to-whitelist)).

## Connections UI

Your Embeddable Workspace contains a section called **Database Connections** where you can manage all your database connections, including creating new ones, editing existing ones, and deleting connections you no longer need. You can find this section in the left-hand sidebar of your workspace.

We **strongly encourage** our users to work with the **Connections UI** for managing their database connections, as it provides a user-friendly interface and reduces the risk of misconfigurations. However, we do provide an API for those who prefer to manage connections programmatically.

## Connections API

Use our **Connections API** to connect Embeddable to your database(s).

- You can call the API directly using tools like [Postman](https://www.postman.com/) or Bruno (link below)

API collection: https://github.com/embeddable-hq/embeddable-public-apis

- Or you can use the handy sample javascript scripts in [your repo](https://github.com/embeddable-hq/remarkable-pro-boilerplate/tree/main/src/embeddable.com/scripts).

The below is a `CREATE` action, but all `CRUD` operations are available.

```javascript
const apiKey = '<your API Key>';
const connectionName = '<some unique name>';
const BASE_URL = 'https://api.us.embeddable.com'; // US API
// const BASE_URL = 'https://api.eu.embeddable.com'; // EU API

const resp = await fetch(`${BASE_URL}/api/v1/connections`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}` /* keep your API Key secure */,
    },
    body: JSON.stringify({
        name: connectionName,
        type: 'postgres', // Database type
        concurrency: 20, // optional, number - see "Connection Concurrency" below
        credentials: {
            host: '...',
            database: '...',
            user: '...',
            password: '...',
            port: ..., // optional, number
            ssl: true, // optional, boolean
        },
    }),
});
```

- `apiKey`: This is located on the homepage of your workspace.
- `connectionName`: Unique name for the database connection within your workspace.
- `type`: Which driver to use (postgres, bigquery, etc). Find your database [here](./credentials).
- `concurrency`: Optional number of concurrent connections Cube can open to your database for this connection. See [Connection Concurrency](#connection-concurrency) below.
- `credentials`: A javascript object containing the credentials expected by the driver:
  - The necessary credentials for each database are available [here](./credentials).
  - Credentials are securely encrypted.
  - We strongly suggest creating a **read-only** database user for each connection.

## Connection Concurrency

Each connection has a `concurrency` setting: the number of connections Cube is allowed to open to your database at once for that connection.

- **Why it matters:** the higher the concurrency, the more queries Cube can run against your database in parallel, instead of queuing them up one after another. This directly impacts how fast your dashboards load, especially when many end-users (or many components on the same dashboard) request data at the same time.
- **Recommendation:** we recommend setting `concurrency` to somewhere between **20 and 100**, depending on how many connections your database can support. Databases with generous connection limits and available resources (e.g. Snowflake, BigQuery, Redshift) can typically support higher values; row-based databases like Postgres with low default connection limits should be checked against your own database's `max_connections` before going too high.
- **Default:** if you omit `concurrency`, Cube falls back to its own default, which is low and generally not enough to serve concurrent dashboard traffic well. We recommend setting it explicitly for any production connection.

You can set `concurrency` when creating a connection (`POST /api/v1/connections`, shown above), or add/update it on an existing connection (`PUT /api/v1/connections/<connectionName>`) — see [CRUD Operations](#crud-operations) below.

> Leave enough headroom in your database's own max connection limit for other
> applications connecting to the same database. Setting `concurrency` higher
> than your database can support may cause connection errors instead of faster
> queries.

## CRUD Operations

Refer to the specific scripts in [your repo](https://github.com/embeddable-hq/remarkable-pro-boilerplate/tree/main/src/embeddable.com/scripts) to easily manage connections:

- **Create a connection:** `connection-create.cjs`
- **List connections:** `connection-list.cjs`
- **Read a specific connection:** `connection-read.cjs`
- **Update a connection:** `connection-update.cjs`
- **Delete a connection:** `connection-delete.cjs`

To run any script, use the following command:

```bash
node src/embeddable.com/scripts/<script>.cjs
```

Or use tools like [Postman](https://www.postman.com/) or Bruno (link below).

API collection: https://github.com/embeddable-hq/embeddable-public-apis

## Testing Your Connection

To confirm your connection works, use the `/test` endpoint, which is also available as a [script](https://github.com/embeddable-hq/remarkable-pro-boilerplate/tree/main/src/embeddable.com/scripts) in your repo.

```javascript
const apiKey = "<your API Key>";
const connectionName = "<some unique name>";
const BASE_URL = "https://api.us.embeddable.com"; // US API
// const BASE_URL = 'https://api.eu.embeddable.com'; // EU API

fetch(`${BASE_URL}/api/v1/connections/${connectionName}/test`, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${apiKey}`,
  },
});
```

## Using Your Connection

Reference your database connection by name in your [data models](https://docs.embeddable.com/data-modeling/writing-data-models/defining-models):

```yaml
cubes:
  - name: customers
    data_source: my-postgres-db # Points to the database connection
```

- Omitting the `data_source` field defaults to a demo connection named `default`.

## Multiple databases

Embeddable supports connecting to **multiple databases** out-of-the-box.

To do so, simply:

- create a separate **connection** for each database (see "Connections API" above)
- then point each of your **models** at the appropriate **connection** (see "Using your connection" above).

Sometimes, however, you'll want to point a model at different databases, e.g:

- to run against prod vs qa vs test databases.
- to connect to different databases depending on the customer.

In those cases you'll need use what we call **Environments**. Learn more [here](./environments).

## IP Whitelisting

You can restrict database access by allowing only Embeddable’s IPs ([learn more](https://docs.embeddable.com/deployment/deployment-regions#ips-to-whitelist)).

## SSH Connections (Bastion Host)

> Please contact us for help with this.

Embeddable also supports connecting to your database via SSH connection. The process works as follows:

### Whitelist Embeddable's SSH-specific IP's:

Details [here](https://docs.embeddable.com/deployment/deployment-regions#ips-to-whitelist).

### Add public key to bastion user

This will be used for establishing the SSH tunnel.

- **US :**

```bash
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMMDhwuLvNxkgh7armnFQ94MjFyA5EnAAYol9uXcj0Xi jump-server-us@embeddable.com
```

- **EU :**

```bash
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJFIqQgU/5Cdbe8sTPZ9S3Zy6iTyBRNP7cjZmD5LtlX0 jump-server-eu@embeddable.com
```

### You'll need the following information

- Bastion user
- Bastion host
- Host and port of the database (must be accessible from Bastion host)
- Database credentials needed by Cube

You can send us these details using [Embeddable's secure transfer service](https://privatebin.eu.embeddable.com/) — to avoid sending them by email or chat. Just paste the details, set an expiry, and share the link with us.

Note: SSH tunnels are set up per database server. If you need to connect to two different database servers, you’ll need two separate SSH tunnels. However, the same SSH user can be reused if both databases are accessible through the same bastion host.

## Amazon PrivateLink

Embeddable supports connecting to your database via Amazon PrivateLink. This allows you to connect to your database **without exposing it to the public internet**. Your database must be in the same region as Embeddable's deployment (US or EU).

This is an alternative to SSH connections. It is more secure and easier to set up, but it is **only available** for databases hosted on AWS.

### What it means

- No public internet exposure — traffic stays entirely within AWS’s private network
- Available in eu-central-1 and us-east-1 - database must be in the same region as Embeddable's deployment
- Works with any database that supports PrivateLink (e.g. RDS, Redshift, etc)

### How it works

- You set up a VPC Endpoint Service (NLB → your DB). For more information, see the [AWS Documentation](https://docs.aws.amazon.com/vpc/latest/privatelink/create-endpoint-service.html)
- You whitelist our Principal ARN in your Endpoint Service
- You share the service name with us using [our transfer service](https://privatebin.eu.embeddable.com/)
- We connect to your service
- You accept the connection
- You can now make connections via the [Connections API](#connections-api) using the provided hostname and port

### Whitelisting Embeddable's Principal ARN

In order to work with Amazon PrivateLink, you need to whitelist our Principal ARN in your VPC Endpoint Service. This allows us to connect to your database securely without exposing it to the public internet.

For EU and US, the ARN is the same: `arn:aws:iam::891377273898:role/EmbeddablePrivateLinkConsumer`.

## Connect to External Data Platforms

Embeddable works seamlessly with [Cube Cloud](https://cube.dev/product/cube-cloud), allowing you to use your existing Cube models alongside Embeddable.

Follow the setup instructions [here](https://docs.embeddable.com/data/cube-cloud) to connect your Cube Cloud instance and start using your data.

---
Source: https://docs.embeddable.com/data/connect-your-database
