Learn R Programming

motherduck (version 0.2.0)

create_database: Create (If Not Exists) and Switch to a Database

Description

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)

Value

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

Arguments

.con

A valid DBI connection (DuckDB / MotherDuck).

database_name

Name of the database to create/ensure and switch to

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

Run this code
if (FALSE) {
con_md <- connect_to_motherduck()
create_database(con_md, "analytics")
}

Run the code above in your browser using DataLab