Skip to main content

Go driver

The go-duckdb driver supports MotherDuck out of the box!

To connect, you need a dependency on the driver in your go.mod file:

github.com/duckdb/duckdb-go/v2 v2.5.1

Your code can then open a connection using the standard database/sql package, or any other mechanisms supported by go-duckdb:

db, err := sql.Open("duckdb", "md:my_db?motherduck_token=<token>")

Go Gotchas

Use "motherduck_" prefixed configuration in the connection string

Because duckdb-go parses all arguments out into a configuration dictionary, the shorthand properties such as attach_mode will not work. Use the fully qualified properties such as motherduck_attach_mode for the MotherDuck-specific properties:

db, err := sql.Open("duckdb", "md:my_db?motherduck_attach_mode=single")

Connecting to multiple accounts from the same process

Because duckdb-go parses all arguments out into a configuration dictionary, trying to connect with multiple MotherDuck accounts (different motherduck_token values) from the same Go process will fail with Can't open a connection to same database file with a different configuration. If connecting to different accounts is a requirement, work around this by connecting to an in-memory DuckDB database first:

c, err := duckdb.NewConnector(":memory:?custom_user_agent=INTEGRATION_NAME/v1.2.3", func(execer driver.ExecerContext) error {
bootQueries := []string{
`INSTALL motherduck`,
`LOAD motherduck`,
fmt.Sprintf("SET motherduck_token='%s'", token),
`SET motherduck_session_hint='user123'`,
`ATTACH 'md:my_db'`,
}
for _, query := range bootQueries {
_, err := execer.ExecContext(context.Background(), query, nil)
if err != nil {
return err
}
}
return nil
})
if err != nil {
// handle the error
}
defer c.Close()
db := sql.OpenDB(c)
defer db.Close()