SQL Server error 4060: Cannot open database requested
The login is valid, but the database named in the connection doesn't exist, is offline or restoring, or the login has no user in it.
- Error number
4060- Severity
- 11
- Category
- Connection and login
What it means
SQL Server accepted the login but couldn't open the database the connection asked for (the Database or Initial Catalog setting). Because the connection can't continue without a database, the login fails as a whole. Error 18456 with state 38 is usually logged alongside it.
Common causes
- The database name is misspelled, or the connection points to a server where that database doesn't exist. On a case-sensitive server collation the name's case matters.
- The database is offline, restoring, in recovery, suspect, or in single-user mode with someone else connected.
- The login has no database user mapped in that database, for example after restoring the database onto another server.
- In Azure SQL Database, the database was dropped, renamed, or is paused (serverless).
How to fix it
-
Check that the database exists and is online.
SELECT name, state_desc, user_access_desc FROM sys.databases WHERE name = N'SalesDb';
-
If it is offline, bring it online.
ALTER DATABASE SalesDb SET ONLINE;
-
If the login has no user in the database, create one (or remap an orphaned user).
USE SalesDb; CREATE USER app_user FOR LOGIN app_user; ALTER ROLE db_datareader ADD MEMBER app_user;
-
Correct the database name in the connection string, or connect to
masterfirst to see which databases you can open.
Questions
What is the difference between 4060 and 4064?
4060 means the database named in the connection couldn't be opened. 4064 means no database was named and the login's default database couldn't be opened.
Related errors
18456Login failed for user4064Cannot open user default database916Login can't access the database3D000PostgreSQL: database does not exist1049MySQL: Unknown database1044MySQL: Access denied for user to database
More connection and login errors
53Network-related or instance-specific18452Login is from an untrusted domain18470Login failed, account disabled18486Login failed, account locked out18487Login failed, password expired18488Login failed, password must be changed18461Login failed, single user mode
The code and message text are from the Microsoft Database Engine events and errors reference. Browse the full SQL Server error list or search every database error.
Work on SQL Server in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQL Server and the other databases you use.