About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.
A user or an application that has just worked for months suddenly cannot connect, and the message is "Cannot open user default database. Login failed." That is SQL Server error 4064. The login itself was recognised. What failed is the second step: opening the database that the login is set to land in. The database may have been moved, restored, set offline or removed, or the login may have lost its user in that database.
Below are the causes Microsoft lists, read-only queries to prove which one you have, and the fix for each, with a way back for every change.
What the error means
Microsoft's page for error 4064 gives the message text as "Cannot open user default database. Login failed." and explains it this way: "The SQL Server login was unable to connect to SQL Server, either due to permission issues with a database user associated with the login in the default database, or a problem with its default database."
Every login has a default database, the database a new connection opens unless the connection asks for another. If that database cannot be used by this login at that moment, the connection fails with 4064 even when the password was right.
Microsoft's page also says that when an application such as SQL Server Management Studio (SSMS) shows error 4064 to the user, the SQL Server error log holds an 18456 entry, and gives the wording of its example: "Login failed for user" followed by "Reason: Failed to open the database" and the database name "specified in the login properties", logged with State: 40. The page states that this behaviour is by design and that fixing the default database "automatically addresses the 18456 error", so a login failure in the log with that reason is a pointer to the default database, not to the password.
Likely causes
Microsoft splits the causes into two groups. The first is permission problems in the default database:
- The login has no mapped user in its default database. The page says: "The login doesn't have a corresponding mapped user in its default database."
- A default database was assigned to the login but no user mapping was created in it.
- The mapped user has been denied access, for example by being added to the db_denydatareader fixed database role by mistake.
The second group is a default database that is unavailable when the login connects. The page lists these states: suspect, no longer exists, wrong name, single-user with the only connection already in use, detached, RESTRICTED_USER, offline, EMERGENCY, and part of a database mirror. It adds that a login that belongs to several groups can fail because the default database of one of those groups is unavailable.
| What you see | Cause on Microsoft's list |
|---|---|
| Typical right after a database is moved to another instance | No mapped user in the default database (the page calls these "orphaned users" and says the problem "typically occurs when databases are moved between two server instances") |
| SSMS shows the database as (Recovery Pending) | Suspect mode |
| Database not in the list at all | Removed, detached, or the name is wrong |
user_access_desc is SINGLE_USER or RESTRICTED_USER |
Single-user or restricted state |
| Status OFFLINE or EMERGENCY | Offline or emergency state |
Prove the cause before changing anything
Everything in this section only reads. Connect with an account that can still get in. Microsoft's workaround for the affected user is to open the Connect to Server dialog in SSMS, select Options, and under Connection Properties type another database in "Connect to database". A sysadmin login can enter master. A login that is not a sysadmin can enter a database it knows it can reach, and master may work unless "your system administrator has explicitly denied permissions to a guest user in the master database."
- Find the login's default database, using Microsoft's query (replace the placeholder with the login name):
SELECT name, default_database_name
FROM sys.server_principals
WHERE type = 'S' AND name = '<sql-login>';
- Check the state of that database. Microsoft gives two queries:
SELECT DATABASEPROPERTYEX (N'<dbname>', N'STATUS') AS N'Database Status';
SELECT name, user_access_desc FROM sys.databases WHERE name = '<dbname>';
- If the database is missing, check for other logins pointed at the same name, using Microsoft's query:
SELECT name AS Login_Name FROM sys.server_principals where default_database_name = '<removed-dbname>';
- If the database is online and multi-user, the cause is a permission or mapping problem. Open the database's Security, Users node and check whether the login has a user, and whether that user is a member of db_denydatareader on the Membership page. Microsoft also points to
sys.fn_my_permissionsfor effective permissions.
Example (illustrative): at contoso.com.au the finance application's SQL login finance_app fails after the database FinanceDB was restored onto a new server. Query 1 shows the default database is FinanceDB. Query 2 shows STATUS is ONLINE and the user access is MULTI_USER. The database is available, so the mapping is the suspect: the restore brought the user across but the login on the new server is a different identity.
Fix
Microsoft's page has a row for each cause. The fixes that change the server are below. Work out which row applies from the previous section; apply only that one.
Change the login's default database
This is the safe fix when the database is meant to be gone or unavailable, or when you only need the login to connect. Microsoft says you need ALTER ANY LOGIN permission, and CONTROL SERVER as well if the login is a sysadmin or has CONTROL SERVER.
Save first. Run this query and write down the login name and its current default_database_name.
SELECT name, default_database_name
FROM sys.server_principals
WHERE type = 'S' AND name = '<sql-login>';
Risk: The next statement changes which database one login opens on every future connection. Any tool or script that relied on connecting without naming a database will land somewhere else. Only that login changes.
ALTER LOGIN [<LoginName>] WITH DEFAULT_DATABASE = <AvailableDatabaseName>;
Microsoft's examples use master as the target (ALTER LOGIN [SQLLogin] WITH DEFAULT_DATABASE = master;) or a named user database. The same statement can be run from SSMS, from sqlcmd after connecting with sqlcmd -S <InstanceName> -d master and a trusted connection, or from the login's General page.
To undo it: run the same statement with the login name and the default database you wrote down, for example ALTER LOGIN [<LoginName>] WITH DEFAULT_DATABASE = <PreviousDatabaseName>;.
Bring the database back to normal access
Microsoft's rows for single-user and RESTRICTED_USER databases say to return the database to multi-user mode when maintenance is finished, and for OFFLINE and EMERGENCY databases to bring them online. Microsoft adds that where you still need to restrict or keep the database offline, you change the affected logins' default database instead, as above.
Save first. Run these two queries for this one database and note the values, because they are the state you may need to go back to.
SELECT DATABASEPROPERTYEX (N'<dbname>', N'STATUS') AS N'Database Status';
SELECT name, user_access_desc FROM sys.databases WHERE name = '<dbname>';
Disruptive step: The next statement sets one database to multi-user access, which allows every login that has a user in it to connect again. If someone set the database to single-user or restricted mode deliberately for maintenance, that protection ends. Confirm with whoever did it that the maintenance is finished.
ALTER DATABASE <dbname> SET MULTI_USER;
To undo it: set the database back to the value you recorded. The options are SINGLE_USER, RESTRICTED_USER and MULTI_USER on Microsoft's ALTER DATABASE SET options page. For example ALTER DATABASE <dbname> SET RESTRICTED_USER;.
For an offline or emergency database the statement on Microsoft's page is ALTER database <dnname> SET ONLINE;. Microsoft's page spells the database placeholder <dnname>; it means the database name.
Disruptive step: The next statement brings one offline or emergency database online, so users and applications can open it. A database put into EMERGENCY by an administrator for troubleshooting is read-only, and the same ALTER DATABASE page says only sysadmin members can set that state. Bring it online only if that work is complete.
ALTER DATABASE <dbname> SET ONLINE;
To undo it: the same page lists ONLINE | OFFLINE | EMERGENCY, so set the state you recorded, for example ALTER DATABASE <dbname> SET OFFLINE;. Make sure nobody needs the database before you do.
The other rows
- Suspect: Microsoft says a database becomes suspect for reasons including denial of access to a database resource by the operating system and "the unavailability or corruption of one or more database files", and that "You might have to recover the database from its backup to resolve this situation." This is a recovery job, not a login fix.
- No user mapping or orphaned user: follow Microsoft's page "Troubleshoot orphaned users (SQL Server)".
- Detached: Microsoft says to attach the database, by SSMS or
sp_attach_db. - Database mirror: Microsoft states: "You can't connect to a mirror database on the mirror server, and this behavior is by design."
Confirm the fix
- Connect as the affected login without naming a database and confirm the connection opens.
- Re-run the default database query and the state queries and compare with what you recorded.
- Look in the SQL Server error log: the 18456 entry with its "Failed to open the database" reason should stop appearing.
What to watch afterwards
- Other logins that share the same default database. The query for default database names finds them before they fail.
- After any database move or restore, check that logins and users line up. Error 4064 is one of the common results of orphaned users, according to Microsoft.
- Maintenance that sets single-user or restricted mode: reset it as the last step of the job.
Read next
Related articles you may want next.
- TroubleshootingSQL Server error 18456 'Login failed for user': read the state number in the error logLogin failed for user, error 18456: find the state number and reason in the SQL Server error log, match it to the cause and fix it safely.Read article
- TroubleshootingThe Database Restored, but the Application Cannot Log InTrace SQL logins, database users and SID mappings after a restore, and validate application access without broadening application privileges.Read article
- Backup & RecoverySQL Server database stuck in 'Restoring...': finish the restore or recover it safelyWhy a SQL Server database sits in Restoring, how to tell a live restore from NORECOVERY or a standby copy, and how to recover without losing logs.Read article
Risks and limits
These statements have not been run in a lab. Microsoft's page gives no version range, so test on a non-production instance first where you can. Changing a login's default database cannot repair a suspect or detached database; it only lets the login connect elsewhere. The page does not describe any damage from the changes above. It also does not give a command to remove a user from db_denydatareader, only where to look for the membership.
Sources
- MSSQLSERVER_4064 — Microsoft Learn, page dated 2026-03-18, accessed 1 December 2026
- ALTER DATABASE SET options (Transact-SQL) — Microsoft Learn, page dated 2026-07-21, accessed 1 December 2026
This article was originally published on .