2 min read

Distributing DuckDB From The CLI

Share local DuckDB files over the internet!
Distributing DuckDB From The CLI

During a recent conversation with a friend, we discussed my sharing the results of some analysis I did with him. I used DuckDB to crunch some numbers and created a small database to hold all my transformations – just a few tables & views.

However, I was looking for an easy way to allow my friend to access my work without having to send him the entire file every time I made an update.

Fortunately, I remembered one of my favorite tools, ngrok, that allowed me to share my work over the network! So naturally, I wrote a small re-usable script that allows me to instantly share a local DuckDB file through a publicly (or password-protected) accessible link.

Using a small script, I was able to share my local db with one line:

./duckdb-share.sh dani.db

And my friend was able to instantly use it from their terminal:

D attach 'https://d3a0-209-35-224-123.ngrok-free.app/dani.db';
D select * from dani.analysis_results

Here's the full script, save it as duckdb-share.sh and make it executable.

set -e


ngrok http file://$DB_FILE_PARENT_FOLDER --log=stdout >/dev/null &

sleep 1

PUBLIC_URL=$(curl -sS http://localhost:4040/api/tunnels | jq -r '.tunnels[0].public_url')

echo "Database file $DB_FILE_NAME is now being shared, to connect in DuckDB execute:"
echo "> load httpfs; attach '$NGROK_URL';"

echo "To kill the share, execute:"
echo "> kill -9 $(ps aux | grep ngrok | grep -v grep | awk '{print $2}')"

To show how it works, if you run this, expect the following output, containing a copy-pastable snippet to be run on the DuckDB consumer side.

./duckdb-share.sh path/to/duck.db

Database file duck.db is now being shared, to connect in DuckDB execute:
> load httpfs; attach 'https://d3a0-209-35-224-123.ngrok-free.app/duck.db';

To kill the share, execute:
> kill -9 42114

The consumer on the other side just has to attach to the remote database:

attach 'https://d3a0-209-35-224-123.ngrok-free.app/duck.db';
Don't forget to load the httpfs extension on the consumer side first by running load httpfs; first!

As a gist:

GitHub Gist: instantly share code, notes, and snippets.