# MotherDuck

## Setup

The following credentials keys are accepted:

* `database` **(required)** -> The motherduck database name
* `motherduck_token` **(required)** -> The service token. See [here](https://motherduck.com/docs/authenticating-to-motherduck#authentication-using-a-service-token) for instructions on getting it.
* `schema` (optional) -> The default schema to use.
* `duckdb_version` (optional) -> The CLI version of DuckDB to use. You can also specify the env. variable `DUCKDB_VERSION`.
* `read_only` (optional) -> Whether to open the connection in `readonly` mode. Accepts `true` or `false`. Default is `false`.
* `interactive` (optional) -> Whether to communicate to the DuckDB CLI via interactive mode instead of reopening the connection each time. Accepts `true` or `false`. Default is `true`.
* `motherduck_attach_mode` (optional v1.4.24+) -> Whether to set [MotherDuck Attach Mode](https://motherduck.com/docs/key-tasks/authenticating-and-connecting-to-motherduck/attach-modes/). Accepts `workspace` or `single`.
* `copy_method` (optional) -> the method data is copied from sling into DuckDB. Acceptable values: `csv_files`, `csv_http`, `arrow_http`, `named_pipes`. Default is `csv_http`.
* `max_buffer_size` (optional *v1.5*) -> the max buffer size to use when piping data from DuckDB CLI. Specify this if you have extremely large one-line text values in your dataset. Default is `10485760` (10MB).

### Using `sling conns`

Here are examples of setting a connection named `MOTHERDUCK`. We must provide the `type=motherduck` property:

{% code overflow="wrap" %}

```bash
$ sling conns set MOTHERDUCK type=motherduck database=my_db motherduck_token=xxxxxxxxxxxx

# Or use url
$ sling conns set MOTHERDUCK url="motherduck://my_db?motherduck_token=xxxxxxxxxxxx"
```

{% endcode %}

### Environment Variable

See [here](https://docs.slingdata.io/sling-cli/environment#dot-env-file-.env.sling) to learn more about the `.env.sling` file.

{% code overflow="wrap" %}

```bash
export MOTHERDUCK='motherduck://my_db?motherduck_token=xxxxxxxxxxxx'
export MOTHERDUCK='{ type: motherduck, database: "my_db", motherduck_token: "xxxxxxxxxxxx" }'
```

{% endcode %}

### Sling Env File YAML

See [here](https://docs.slingdata.io/sling-cli/environment#sling-env-file-env.yaml) to learn more about the sling `env.yaml` file.

```yaml
connections:
  MOTHERDUCK:
    type: motherduck
    database: <database>
    motherduck_token: <motherduck_token>
    schema: <schema>
    duckdb_version: '<duckdb_version>'
    copy_method: arrow_http
```

### Specifying a DuckDB version

If you would like to use a specific DuckDB version, you can specify this way:

```bash
export DUCKDB_VERSION='0.7.0'
```

If you are facing issues connecting, please reach out to us at <support@slingdata.io>, on [discord](https://discord.gg/q5xtaSNDvp) or open a Github Issue [here](https://github.com/slingdata-io/sling-cli/issues).

### Potential Issue

If you have a `.duckdbrc` file, which runs commands whenever the DuckDB CLI is invoked, this may interfere with normal Sling operation. If you are facing weird issues and have this file, try again after deleting it.
