Skip to main content

Switching the current database

Below are examples of how to determine the current/active database and schema and switch between different databases and schemas:

-- check your current database
SELECT current_database();
dbname


-- list all tables in the current database
SHOW TABLES;
table1
table2


-- list all databases
SHOW DATABASES;
dbname
dbname2

-- switch to database named 'dbname2'
USE dbname2;

-- verify that you've successfully switched databases
SELECT current_database();
dbname2

-- check your current schema
SELECT current_schema();
main

-- list all schemas across all databases
SELECT * FROM duckdb_schemas();
oiddatabase_namedatabase_oidschema_nameinternalsql
986my_db989information_schematrueNULL
974my_db989mainfalseNULL
972my_db989my_schemafalseNULL
987my_db989pg_catalogtrueNULL
1508system0information_schematrueNULL
0system0maintrueNULL
1509system0pg_catalogtrueNULL
1510temp1453information_schematrueNULL
1454temp1453maintrueNULL
1511temp1453pg_catalogtrueNULL
-- switch to schema my_schema within the same database
USE my_schema;

-- verify that you've successfully switched schemas
SELECT current_schema();
my_schema

-- switch to database my_db and schema main
USE my_db.my_schema

-- verify that both the database and schema have been changed
SELECT current_database(), current_schema();
current_database()current_schema()
my_dbmain