dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Home/Error Library/sqlserver-4064-cannot-open-user-default-database
SQL Server
SQL Server 2016–2022Connection & User Management
7 min read
Msg 4064, Level 11, State 1: Cannot open user default database. Login failed.

SQL Server Msg 4064 — Cannot Login, Which Database Is Unavailable?

Diagnosing Msg 4064: inspecting sys.server_principals default_database_name, distinguishing from Msg 4060, DAC recovery, and the AUTO_CLOSE trap.

Reviewed by:Principal Database Reliability Architect
Tested Versions: SQL Server 2022 CU15 & 2019 CU28 (Windows Server 2022 / Docker 29.7)

Operational Verification & Reliability Metadata

Last Technical Review: 30 Ekim 2026
Tested VersionsSQL Server 2022 CU15 & 2019 CU28 (Windows Server 2022, Docker 29.7)
Required PrivilegesALTER ANY LOGIN or sysadmin (to alter default database); Dedicated Admin Connection (DAC) access
Risk Assessment
LowUsing tempdb as permanent default database creates hidden initialization dependencies
Expected OutputInstant login recovery via ALTER LOGIN ... WITH DEFAULT_DATABASE = master and disabling AUTO_CLOSE
Rollback Procedure:Temporarily point default database to master, restore target database to ONLINE, and disable AUTO_CLOSE
Microsoft Docs: Troubleshoot Error 4064 Default Database
First 60 Seconds Emergency Protocol:

The error string is a symptom, not the root cause. Do not restart the server blindly. Run the diagnostic queries below to pinpoint the actual failure mode.

▸Symptom

plaintext
Msg 4064, Level 11, State 1, Line 1 Cannot open user default database. Login failed. Msg 18456, Level 14, State 1, Line 1 Login failed for user 'app_user'.

When both lines appear together, they confirm a pivotal diagnostic fact: The user credentials are correct, and authentication succeeded! However, during session initialization, SQL Server attempted to bind the connection context to the principal's assigned default database and failed, summarily terminating the session.


▸Distinguishing Msg 4064 from Msg 4060

  • •Msg 4060 (`Cannot open database requested by login`): The application connection string explicitly designated a database (Initial Catalog=app) that is offline or unavailable (H-15).
  • •Msg 4064 (`Cannot open user default database`): The connection string omitted the catalog name (or specified an invalid context), falling back to the principal's configured default database, which cannot be accessed.

▸Diagnostic Workflow: Resolving the Chicken-and-Egg Impasse

1. Connect via DAC (Dedicated Administrator Connection)

The DAC connects directly to master, bypassing individual user default database restrictions:

bash
sqlcmd -S server_name -A -E

2. Query Default Database Configurations

sql
SELECT name, default_database_name, is_disabled, type_desc FROM sys.server_principals WHERE name = 'app_user';

Examine the catalog status:

sql
SELECT name, state_desc, user_access_desc, is_auto_close_on FROM sys.databases WHERE name = 'app';

Four root causes:

  1. 1Target database was dropped or renamed.
  2. 2Target database is in an unready state (OFFLINE, RESTORING, RECOVERY_PENDING, SUSPECT).
  3. 3Orphaned user mapping: The database exists, but contains no USER mapped to this server login.
  4. 4The AUTO_CLOSE trap: Database shuts down between connections, stalling fresh logins.

▸Step-by-Step Resolution

1. Repoint the Login's Default Database to master

Instantly restores connectivity:

sql
ALTER LOGIN app_user WITH DEFAULT_DATABASE = master;

2. Bring Target Database Online

sql
ALTER DATABASE app SET ONLINE;

3. Disable AUTO_CLOSE

sql
ALTER DATABASE app SET AUTO_CLOSE OFF;

4. Re-link Orphaned Database Users

sql
USE app; ALTER USER app_user WITH LOGIN = app_user;

▸Related Error Guides & Hub Links

Other Database Error Triage Playbooks

Other battle-tested diagnostic guides for high-severity database errors.

All Errors (50)