# Managed ClickHouse

Run ClickHouse, the column-oriented database for real-time analytics, without a server to look after. We take care of provisioning and patch updates, and storage is included in the flat plan price.

## Create a ClickHouse database

1. Go to **Databases** and click **Create database**.
2. Select **ClickHouse**.
3. Enter a **Name**. It must be unique in your organization.
4. Select the **Data center** your servers run in. See [Regions](https://betterstack.com/docs/cloud/servers/regions/#choose-a-region).
5. Select a **Plan**.
6. Turn on **Allow public access** if clients outside your private networks connect to the database.
7. Click **Create database**.

ClickHouse runs on a single node, so there's no replica to pick. The database is ready when its status reads **Running**.

![Create database form with ClickHouse selected](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/fdc85974-34bd-4c19-0a3d-b7f8c64ad900/md2x =2180x2156)

## Plans

| Plan | vCPU and RAM | Storage | Concurrent queries |
|------|--------------|---------|--------------------|
| CH1 | 4 vCPU, 4 GB | 64 GB | 40 |
| CH2 | 8 vCPU, 8 GB | 128 GB | 80 |
| CH3 | 8 vCPU, 32 GB | 192 GB | 160 |
| CH4 | 16 vCPU, 64 GB | 256 GB | 320 |
| CH5 | 32 vCPU, 120 GB | 512 GB | 640 |
| CH6 | 48 vCPU, 160 GB | 768 GB | 960 |
| CH7 | 64 vCPU, 192 GB | 1,024 GB | 1,280 |
| CH8 | 128 vCPU, 480 GB | 1,536 GB | 2,560 |

ClickHouse compresses data heavily, so the storage holds far more raw data than its size suggests. Concurrent queries are the capacity limit, because each analytical query uses every core. A query over the limit fails right away with `Too many simultaneous queries`, so queue queries in your client, for example with a bounded worker pool. Connections are cheap, and you can open up to 1,000 at once.

By default, each query can use up to half the RAM, and big aggregations and sorts spill to disk instead of failing. Traffic over private networks is free. Outbound traffic from table functions such as `url()` and `s3()`, and from the Kafka engine, counts as internet egress. See [Pricing philosophy](https://betterstack.com/docs/cloud/pricing/#network-and-ip-address-prices) for its price.

## Connect to your database

Open the database and find the hostname, the `defaultdb` database, the `admin` user and its password under **Connection**. **Copy database URL** gives you a ready-made connection string for your app. To connect from a terminal, set the hostname as `CH_HOST` and the password as `CH_PASSWORD`:

[code-tabs]
```bash
[label Private network]
clickhouse-client --host "$CH_HOST" --user admin --password
```
```bash
[label Public access]
curl --cacert betterstack-db-ca.pem "https://$CH_PUBLIC_HOST:8443/?query=SELECT+1" \
  -H "X-ClickHouse-User: admin" -H "X-ClickHouse-Key: $CH_PASSWORD"
```
[/code-tabs]

A new database is reachable only from the [private networks](https://betterstack.com/docs/cloud/networks/private-networks/) attached to it. There, it serves the native protocol on port **9000**, or **9440** with TLS, and HTTP on port **8123**, or **8443** with HTTPS.

**Allow public access** opens only the TLS ports, 9440 and 8443, on a separate public hostname. Certificates are issued by the Better Stack Managed CA, which isn't in system trust stores, so pass the CA certificate to your client. `betterstack clickhouse credentials <name> --availability-zone us-west-2a` returns the public hostname in `public_uri` and the CA certificate in `ca_certificate`. Save the certificate as `betterstack-db-ca.pem` and set the public hostname as `CH_PUBLIC_HOST`. Restrict who can connect with a [firewall](https://betterstack.com/docs/cloud/networks/firewalls/).

[info]
#### Connection hangs from outside Cloud?
The hostname resolves to a private address, so it works only from your private networks. From the internet, turn on public access and connect to the public hostname.
[/info]

## Rotate credentials

To replace the `admin` password:

1. Click **Rotate credentials** on the database page.
2. Click **Rotate credentials** to confirm.

The previous password keeps working for 24 hours, so roll your clients within that window. Users you create yourself aren't affected.

## Users and databases

`admin` manages your data with plain SQL. `CREATE DATABASE`, `CREATE USER`, `CREATE ROLE`, row policies and quotas work as they would on your own server:

```sql
[label Create a database and a user]
CREATE DATABASE analytics;
CREATE USER app IDENTIFIED BY 'use-a-strong-password';
GRANT SELECT, INSERT ON analytics.* TO app;
```

Session settings, such as `max_memory_usage` and `max_threads`, can be changed with `SET`. `SYSTEM FLUSH LOGS`, `SYSTEM DROP CACHE`, `SYSTEM RELOAD DICTIONARY` and `KILL QUERY` work. Server file access and commands that control the server, such as `SYSTEM SHUTDOWN`, aren't available.

The built-in `default` user is locked. Anything that connects back to the database, such as a dictionary with a ClickHouse source, must name `USER 'admin'` and its password, or another user you created.

## Single node and recovery

ClickHouse runs without replicas, so there's no failover. If the node's machine fails, we recover it with its data. If the data itself is lost, your latest snapshot is what you get back, so take snapshots often.

Everything that runs on a single node works, such as the MergeTree family, materialized views, projections, TTLs, dictionaries, the Kafka engine and async inserts. Replicated table engines, the `Replicated` database engine, `ON CLUSTER` DDL, `KeeperMap` and `S3Queue` aren't available.

## Snapshots

Click **Take a snapshot** on the database page. The database keeps serving while the snapshot is taken, and every acknowledged synchronous insert is in it. Data in `Buffer` tables, and async inserts acknowledged before they were written, can be missing. Snapshots outlive the database. To restore one, see [Database snapshots](https://betterstack.com/docs/cloud/databases/snapshots/#roll-a-database-back).

[info]
#### Restored database lists detached parts?
A snapshot taken under write load restores with parts that a merge was still writing. ClickHouse lists them in `system.detached_parts` with reason `broken-on-start`, and your data is all in the active parts. Once you've checked it, remove each part with `ALTER TABLE <table> DROP DETACHED PART '<name>'` to free the disk space.
[/info]

## Rescale and disk usage

To change the plan:

1. Click **Rescale** on the database page.
2. Select a **Plan** and click **Rescale database**.

The node stops, resizes and starts again, so the database is unavailable for a few minutes. You can move to a smaller plan only if your data fits within 80% of its storage.

When storage fills up, you get warnings first:

| Storage used | What happens |
|--------------|--------------|
| 80% | We send a disk usage warning to your [webhooks](https://betterstack.com/docs/cloud/integrations/webhooks/). |
| 90% | The database turns **Degraded** as a warning. Reads, writes and schema changes still work. |

The disk never fills up completely. ClickHouse keeps a free-space floor and refuses a write that doesn't fit with `NOT_ENOUGH_SPACE`, while smaller writes and all reads keep working.

To free space, drop or truncate old partitions, add a `TTL … DELETE` clause to large tables, or rescale to a bigger plan. `TRUNCATE` frees space right away.

After a merge, the old parts stay on disk for about 8 minutes, so a heavy bulk load can briefly use more space than your data. Insert in large batches and leave headroom, or rescale up for the load and back down afterwards.

## Updates

We apply ClickHouse patch releases automatically. Each one restarts the node, which takes from a few seconds to a minute.

## Need help?

Please let us know at hello@betterstack.com.  
We're happy to help! 🙏
