---
title: "Turning Your Root URL Into a DuckDB Remote Database"
date: 2024-12-01
author: Drew Breunig
description: "DuckDB sends a distinctive user agent, so a site can serve a database file from its root URL. A small trick with big uses for data publishers."
tags: ["duckdb", "data"]
url: https://www.dbreunig.com/2024/12/01/turning-your-root-url-into-a-duckdb-remote-database.html
---

On the [Overture][omf] Slack, [Jake Wasserman][jake] shared a cool trick where you can [connect to a remote DuckDB database using the `ATTACH` statement][attach], using an HTTP or S3 URL. Jake's demo was especially cool because he created a DB containing only views referencing an [Overture parquet on S3][omf data], so the hosted database file was only a couple hundred kilobytes. 

It's a nice technique that allows us to host some easy to wrangle views at a convenient URLs (Jake suggested `https://duckdb.overturemaps.org/`), which will be nice for "Getting Started" demos and tutorials or even regional extracts.

But I wondered: what is the body of the request DuckDB is sending? It would be cool if someone could use the root Overture URL as a starting point, rather than having to remember a subdomain.

I spun up a quick Rails app and started `ATTACH`-ing to localhost and interrogating the request. DuckDB fires a standard `GET` HTTP request; there's nothing special about it. However, it *does* use a fairly unique user agent: "cpp-httplib/0.14.3". This is the C++ [library][cpp-httplib] used by DuckDB. We can check a request's user agent and respond with a static database file if it matches "cpp-httplib".

Here's what a Rails controller might look like:

<script src="https://gist.github.com/dbreunig/2a571e31c2d31e188aee91b3ec3c18d7.js"></script>

As a proof of concept, I created a DuckDB database containing a sample of public lists from my app, [StepList](https://steplist.app), and made it available at the root URL. You can test out it out like so:

<script src="https://gist.github.com/dbreunig/0a22204deda9cf080b3d2460c9f5e3eb.js"></script>

One gotcha for this trick: don't forget the trailing `/` in your URL. Otherwise DuckDB will complain.

This technique would be great for any DaaS company or other data purveyor. Sample datasets in remote databases can be made available at their root URL, simplfying testing and demos. We'll certainly be checking this out at Overture, at the root URL or otherwise.

Now I'm off to ask DuckDB to customize their user agent string...

[omf]: https://overturemaps.org
[jake]: https://github.com/jwass
[attach]: https://duckdb.org/docs/sql/statements/attach#attach
[omf data]: https://docs.overturemaps.org/getting-data/duckdb/
[cpp-httplib]: https://github.com/yhirose/cpp-httplib
