The "DB::Exception: Unknown database" error means ClickHouse cannot find the database you referenced in your query or DDL statement. Identified by the error code UNKNOWN_DATABASE, it fires whenever you try to query a table, create a table, or perform any operation that specifies a database name that does not exist on the server.
Impact
Any query or statement referencing the missing database will fail. This blocks table creation, data ingestion, and reads alike. In a deployment pipeline, a missing database can cascade into a full outage of dependent services if the application assumes the database is already present.
Common Causes
- The database was never created -- a migration step that creates the database was skipped or failed silently.
- Typo in the database name --
CREATE TABLE my_databse.eventsinstead ofmy_database.events. - Case sensitivity -- ClickHouse database names are case-sensitive by default.
MyDatabaseandmydatabaseare different. - Wrong server or cluster node -- connecting to a node where the database has not been replicated or created yet.
- Database was dropped -- someone or an automated process removed the database.
- Using a fully qualified name without the database existing in the current context -- referencing
production.eventswhen onlydefaultdatabase exists.
Troubleshooting and Resolution Steps
List all databases on the server:
SHOW DATABASES;Verify that the database name you are referencing appears in the list.
Check for typos and casing. Compare the database name in your query against what
SHOW DATABASESreturns. Remember that names are case-sensitive.Create the database if it does not exist:
CREATE DATABASE IF NOT EXISTS my_database;For clustered environments, create the database on all nodes:
CREATE DATABASE IF NOT EXISTS my_database ON CLUSTER my_cluster;Verify you are connected to the correct server. Check your connection string or client configuration:
clickhouse-client --host your-host --port 9000 --query "SHOW DATABASES"Check if the database was recently dropped. Look in the ClickHouse server logs or query the
system.query_log:SELECT query, event_time FROM system.query_log WHERE query LIKE '%DROP DATABASE%my_database%' ORDER BY event_time DESC LIMIT 5;If the database metadata is corrupt, check the filesystem. Database metadata lives in the ClickHouse data directory (usually
/var/lib/clickhouse/metadata/). Verify that a.sqlfile for the database exists.
Best Practices
- Always use
CREATE DATABASE IF NOT EXISTSin deployment and migration scripts. - Include database creation as the first step in your schema migration pipeline, before any table creation.
- In multi-node clusters, use
ON CLUSTERfor database creation to ensure consistency. - Monitor your ClickHouse instances for unexpected DDL operations using
system.query_log. - Use connection string defaults to set the correct database context so that unqualified table names resolve correctly.
Frequently Asked Questions
Q: Are database names case-sensitive in ClickHouse?
A: Yes. MyDatabase and mydatabase are treated as two separate databases. Always verify the exact casing.
Q: How do I set a default database for my session?
A: Use the USE statement: USE my_database;. In clickhouse-client, you can also pass --database my_database on the command line.
Q: I created the database on one replica but queries on another replica fail. Why?
A: Database creation is not automatically replicated. Use CREATE DATABASE ... ON CLUSTER to propagate the DDL to all nodes, or create the database individually on each node.
Q: Can a database disappear on its own?
A: Not under normal circumstances. Databases are only removed by explicit DROP DATABASE commands. Check your system.query_log for unexpected drops, and verify that no automated cleanup scripts are targeting your database.