Create (If Not Exists) and Switch to a Database

Source: R/utils-db.R

Ensures a database exists and sets it as the active database. If connected to MotherDuck, the function will run ⁠CREATE DATABASE IF NOT EXISTS⁠ followed by ⁠USE <database>⁠. Prints CLI status information about the current user and database.

Usage

create_database(.con, database_name)

Arguments

.con

A valid DBI connection (DuckDB / MotherDuck).

database_name

Name of the database to create/ensure and switch to

Value

Invisibly returns NULL. Side effect: may create a database and switches to it; prints CLI status

Details

  • Connection type is checked via validate_md_connection_status() (with return_type = “arg”).

  • On MotherDuck, executes:

    • ⁠CREATE DATABASE IF NOT EXISTS <database>⁠

    • ⁠USE <database>⁠

  • Displays status and environment info with CLI messages.

See Also

Other db-manage: alter_table_schema(), copy_tables_to_new_location(), create_if_not_exists_share(), create_or_replace_share(), create_schema(), create_table(), delete_and_create_schema(), delete_database(), delete_schema(), delete_table(), describe_share(), drop_share(), list_owned_shares(), list_shared_with_me_shares(), upload_database_to_md()

Examples

con_md <- connect_to_motherduck()
create_database(con_md, "analytics")