# Connect using a token stored in your .Renviron
con <- connect_to_motherduck()
# Connect and specify a persistent database file
con <- connect_to_motherduck(db_path = "~/my_database.duckdb")Create connection to motherduck
Source: R/connection_fns.R
Establishes a connection to a MotherDuck account using DuckDB and the MotherDuck extension. The function handles token validation, database file creation, extension loading, and executes PRAGMA MD_CONNECT to authenticate the connection.
Usage
connect_to_motherduck(db_path = NULL, config)Arguments
-
db_path -
Character, optional. Path to a DuckDB database file or directory to use. If
NULL, a temporary file is used. -
config -
List, optional. A list of DuckDB configuration options to be passed to
duckdb::duckdb().
Value
A DBIConnection object connected to your MotherDuck account.
Details
This function provides a convenient interface for connecting to MotherDuck. It:
-
Uses the token from the
MOTHERDUCK_TOKENenvironment variable. -
Optionally specifies a persistent DuckDB database file or directory via
db_path. -
Optionally provides custom DuckDB configuration options via
config. -
Automatically loads the MotherDuck extension if not already loaded.
You must set the MOTHERDUCK_TOKEN environment variable in your .Renviron file before calling this function. Use usethis::edit_r_environ() to edit your .Renviron file.
If db_path is not supplied, a temporary DuckDB database file will be created in the session’s temporary directory. Use config to pass any DuckDB-specific options (e.g., memory limits or extensions).
MotherDuck currently supports DuckDB versions 1.3.0 through 1.4.4. This function will warn if you’re using an untested version.