Skip to main content

Specify MotherDuck database

When you connect to MotherDuck you can specify a database name or omit the database name and connect to the default database.

  • If you use md: without a database name, you connect to a default MotherDuck database called my_db.
  • If you use md:<database name>, you connect to the <database name> database.

After you establish the connection, either the default database or the one you specify becomes the current database.

You can run the USE command to switch the current database, as shown in the following example.

#list the current database
con.sql("SELECT current_database()").show()
# ('database1')

#switch the current database to database2
con.sql("USE database2")

To query a table in the current database, you can specify just the table name. To query a table in a different database, you can include the database name when you specify the table. You don't need to switch the current database. The following examples demonstrate each method.

#querying a table in the current database
con.sql("SELECT count(*) FROM mytable").show()

#querying a table in another database
con.sql("SELECT count(*) FROM another_db.another_table").show()