// SQL Server error reference

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
Error message
Cannot open database "%.*ls" requested by the login. The login failed.

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

  1. Check that the database exists and is online.

    SELECT name, state_desc, user_access_desc FROM sys.databases WHERE name = N'SalesDb';
  2. If it is offline, bring it online.

    ALTER DATABASE SalesDb SET ONLINE;
  3. 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;
  4. Correct the database name in the connection string, or connect to master first 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

More connection and login errors

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.

// fewer errors, faster fixes

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.