Error 81: UNKNOWN_DATABASE
This error occurs when you attempt to access a database that doesn't exist, hasn't been created yet, or that you don't have permission to access. This can happen due to typos, missing database creation steps, permission restrictions, or issues in distributed cluster configurations.
Most common causes
-
Database doesn't exist
- Typo in database name
- Database not created yet (missing
CREATE DATABASEstep) - Database was dropped or deleted
- Wrong database name in connection string or queries
-
Permission and access issues
- User lacks permissions to access the database
- Database exists but user's
GRANTSdon't include it - Row-level security or access policies restricting visibility
- Cloud organization or service-level access restrictions
-
Case sensitivity and naming issues
- Database name case mismatch (especially in distributed setups)
- Special characters or reserved words in database names
- Unquoted database names with spaces or special chars
- Unicode or non-ASCII characters in names
-
Distributed and cluster issues
- Database doesn't exist on all cluster nodes
- Shard-specific database missing on some replicas
- Cross-cluster query referencing database on other cluster
- Materialized views or dictionaries referencing missing databases
-
Connection and context issues
- Connected to wrong ClickHouse server or instance
- Default database not set in connection
- Database specified in connection string doesn't exist
- Using wrong credentials or connection profile
-
Schema migration and timing issues
- Scripts running before database creation completes
- Race conditions in parallel migrations
- Database dropped and recreated causing timing gaps
- Incomplete rollback leaving references to deleted databases
Common solutions
1. Verify database exists and create if missing
2. List available databases
3. Fix database name typos
4. Use qualified table names
5. Check and grant permissions
6. Handle case-sensitive database names
7. Create database on all cluster nodes
8. Fix materialized view references
9. Handle database in connection strings
10. Verify database in migrations
11. Handle special characters in database names
12. Check database engine and access
13. Handle dropped database scenarios
Prevention tips
- Always use
IF NOT EXISTSin database creation: IncludeCREATE DATABASE IF NOT EXISTSin all migration scripts and initialization code to prevent errors when database already exists - Use qualified table names: Always prefix table names with database names (
database.table) to avoid ambiguity and make queries more portable across different contexts - Verify database existence before operations: In scripts and applications, check database existence using
SHOW DATABASESor querysystem.databasesbefore performing operations - Use consistent naming conventions: Adopt lowercase naming without special characters for databases to avoid case sensitivity and quoting issues across different environments
- Create databases
ON CLUSTER: In clustered environments, always useON CLUSTERclause when creating databases to ensure consistency across all nodes - Document database dependencies: Maintain clear documentation of which databases are required by your tables, views, and applications, especially for materialized views and dictionaries
- Implement proper error handling: In application code, catch
UNKNOWN_DATABASEerrors and provide clear messages to users, potentially with automatic database creation logic - Test migrations in staging: Always test database creation and migration scripts in staging environments that mirror production to catch missing database issues early
- Use configuration management: Store database creation scripts in version control and use infrastructure-as-code tools to ensure databases exist before deploying dependent resources
- Monitor database permissions: Regularly audit user permissions to databases using
SHOW GRANTSto ensure users have appropriate access and identify permission-related issues early
Related error codes
- UNKNOWN_TABLE (60) - Table doesn't exist in database
- UNKNOWN_IDENTIFIER (47) - Column or identifier not found