# Connect ClickHouse to Contextflo

Connect ClickHouse to Claude or ChatGPT: create a read-only ClickHouse user and add your database to Contextflo, with each step and the SQL to run.

Source: https://contextflo.com/docs/data-sources/clickhouse-setup

Connect your ClickHouse database to Contextflo with a dedicated read-only user.

## Prerequisites

- Admin access to your ClickHouse server
- ClickHouse HTTP interface enabled (port 8123 or 8443 for HTTPS)

## Setup Script

Run this in your ClickHouse client. **Update the variables** before running.

```sql
-- =====================================================
-- Contextflo ClickHouse Setup Script
-- =====================================================
-- UPDATE THESE before running:

-- 1. Create a read-only user
CREATE USER IF NOT EXISTS contextflo_user
  IDENTIFIED WITH sha256_password BY 'your-secure-password';

-- 2. Grant SELECT on your database
GRANT SELECT ON your_database.* TO contextflo_user;

-- 3. (Optional) Grant access to additional databases
-- GRANT SELECT ON another_database.* TO contextflo_user;
```

## What This Script Does

1. **Creates a dedicated user** - `contextflo_user` with password authentication
2. **Grants read-only access** - SELECT permissions on your database

## Connection Details for Contextflo

After running the script, use these details to connect:

- **Host**: Your ClickHouse server hostname or IP
- **Port**: `8123` (HTTP) or `8443` (HTTPS)
- **Protocol**: HTTP or HTTPS depending on your setup
- **Database**: Your database name (defaults to `default`)
- **Username**: `contextflo_user`
- **Password**: The password you set in the script

## ClickHouse Cloud

If you're using ClickHouse Cloud:

1. Find your hostname in the Cloud console (ends in `.clickhouse.cloud`)
2. Use **HTTPS** protocol with port **8443**
3. Use the credentials from your Cloud service

## Verify Permissions

To check what permissions were granted:

```sql
SHOW GRANTS FOR contextflo_user;
```

To test as the new user:

```sql
SELECT * FROM your_database.your_table LIMIT 10;
```

## Security Notes

- **Read-only access**: This setup only grants SELECT permissions. Contextflo cannot modify or delete your data.
- **Password security**: Use a secure password with at least 16 characters.
- **Network access**: Ensure your ClickHouse server allows connections from Contextflo's IP addresses.

## Troubleshooting

### Connection Refused

**Possible causes:**

- HTTP interface not enabled
- Firewall blocking port 8123/8443
- Incorrect host or port

**Solutions:**

1. Check that `http_port` or `https_port` is configured in your ClickHouse server config
2. Verify firewall rules allow inbound traffic on the port
3. Test connectivity: `curl http://your-host:8123/ping`

### Authentication Failed

**Possible causes:**

- Wrong username or password
- User doesn't exist

**Solutions:**

1. Double-check credentials
2. Verify user exists: `SELECT name FROM system.users`

### Permission Denied

**Possible causes:**

- User lacks SELECT permissions on the database

**Solutions:**

```sql
GRANT SELECT ON your_database.* TO contextflo_user;
```

## Next Steps

- [Go back to Connecting Data Sources](https://contextflo.com/docs/data-sources/connecting.md)
