

# Connect to Athena with ATTACH

`ATTACH` mounts an Athena workgroup as a catalog in your local DuckDB session, so you can query Athena tables as though they were local. Each attachment adds a catalog that you reference by its alias in later queries.

Use the service ARN with explicit Region and workgroup options, or use an `athena://` path. Complete [Install the extension](connect-with-duckdb-extension-download-and-install.md) first. The following example selects the `athena_creds` secret created by that procedure.

```sql
ATTACH 'arn:aws:athena' AS arn_example
    (TYPE athena, Region '{{us-west-2}}',
     WorkGroup '{{my-duckdb-workgroup}}', Secret 'athena_creds');
```

A full workgroup ARN can name the Region and workgroup. The extension uses the workgroup name in the signing principal's account; it does not use the ARN account field to select another account. Data catalog ARNs are refused. Select a data catalog with the `Catalog` option instead.

The extension uses the query result location configured on the workgroup, including managed query results. It refuses the `OutputLocation` and `S3OutputLocation` options on `ATTACH`. Configure result storage on the workgroup instead.

```sql
ATTACH 'arn:aws:athena:{{us-west-2}}:111122223333:workgroup/{{my-duckdb-workgroup}}' AS workgroup_arn
    (TYPE athena, Secret 'athena_creds');
```

A catalog attached with `READ_ONLY` refuses writes. For write requirements, see [Write requirements and limitations](connect-with-duckdb-extension-write.md#connect-with-duckdb-extension-write-restrictions).

```sql
-- Use the secret from the installation procedure.
ATTACH 'arn:aws:athena' AS read_only_ath
    (TYPE athena, Region '{{us-west-2}}',
     WorkGroup '{{my-duckdb-workgroup}}',
     Secret 'athena_creds', READ_ONLY);
-- Read an existing table.
SELECT * FROM read_only_ath.{{my_database}}.{{my_table}} LIMIT 10;
```

In the following examples, replace {{us-west-2}}, {{my-duckdb-workgroup}}, {{my\_database}}, and {{my\_table}} with your Region, workgroup, database, and table. `Schema` is an alias for `Database`; `Region` and `WorkGroup` can also supply values in place of the path.

```sql
ATTACH 'athena://{{us-west-2}}' AS ath_db
    (TYPE athena, WorkGroup '{{my-duckdb-workgroup}}',
     Database '{{my_database}}', Secret 'athena_creds');
SELECT * FROM ath_db.{{my_table}} LIMIT 10;
```

To attach an S3 table bucket as the catalog, set `Catalog` to `s3tablescatalog/` followed by the bucket name and `Database` to its namespace. In the following example, replace {{amzn-s3-demo-table-bucket}}, {{retail}}, and {{widget\_sales}} with your table bucket, namespace, and table.

```sql
ATTACH 'athena://{{us-west-2}}/{{my-duckdb-workgroup}}' AS sales
    (TYPE athena, Catalog 's3tablescatalog/{{amzn-s3-demo-table-bucket}}',
     Database '{{retail}}', Secret 'athena_creds');
SELECT * FROM sales.{{widget_sales}} LIMIT 10;
```

The following example adds S3 Tables alongside Glue databases in one attached catalog. Set `S3Tables` to your table bucket name or `*` for all table buckets. A namespace is exposed as a schema named `s3tablescatalog/<table-bucket>.<namespace>`. Use the same replacement values as in the previous example.

```sql
ATTACH 'athena://{{us-west-2}}/{{my-duckdb-workgroup}}' AS lake
    (TYPE athena, S3Tables '{{amzn-s3-demo-table-bucket}}', Secret 'athena_creds');
SELECT * FROM lake."s3tablescatalog/{{amzn-s3-demo-table-bucket}}.{{retail}}".{{widget_sales}} LIMIT 10;
```

`WorkGroup`  
Your workgroup name when it is not supplied in the path. If neither an option, a path, nor `athena_workgroup` supplies a name, the default is `primary`. For this guide, select your DuckDB workgroup explicitly.

`Database`  
The default database or table-bucket namespace for the attached catalog.

`Catalog`  
The catalog to attach. The default is `AwsDataCatalog`. For an S3 table bucket, use `s3tablescatalog/` followed by your bucket name.

`S3Tables`  
Adds named table buckets alongside Glue databases. Supply one bucket name, a comma-separated list of bucket names, or `*` for all table buckets.

`Secret`  
The name of the Amazon secret created in your local session.

`ath_db`, `sales`, and `lake`  
Local catalog aliases created by the examples. Keep them when following later examples.

You can use the service ARN as the path and supply the Region and workgroup as options. The following example uses the `athena_creds` secret from the installation procedure.

```sql
ATTACH 'arn:aws:athena' AS by_arn
    (TYPE athena, Region '{{us-west-2}}',
     WorkGroup '{{my-duckdb-workgroup}}', Secret 'athena_creds');
```

To use a FIPS endpoint, set `athena_endpoint` before `ATTACH`. The endpoint must be in the Region being attached.

```sql
SET athena_endpoint = 'https://athena-fips.{{us-west-2}}.amazonaws.com';
ATTACH 'athena://{{us-west-2}}/{{my-duckdb-workgroup}}' AS fips
    (TYPE athena, Secret 'athena_creds');
FROM athena_query('fips', 'SELECT 1 AS via_fips');
RESET athena_endpoint;
```



| Option | Use | 
| --- | --- | 
| Region | The Region when not supplied in the path. A path and option must agree. | 
| Schema | An alias for Database. | 
| EngineVersion | Leave unset for the workgroup-based setup in this guide. | 
| EncryptionOption | SSE\_S3 or SSE\_KMS for S3 result files when the workgroup does not enforce its configuration. For managed query results or an enforcing workgroup, configure encryption on the workgroup and omit EncryptionOption and KmsKey from ATTACH. | 
| KmsKey | Required with an accepted SSE\_KMS option; omitted with SSE\_S3. | 

## Detach a catalog
<a name="connect-with-duckdb-extension-detach"></a>

When you no longer need an attached catalog, remove it from your local DuckDB session with `DETACH`. Use the alias that you gave the catalog in `ATTACH`.

```sql
DETACH {{catalog_alias}};
```