- Error number
- 4064
- Severity
- 11 (user-correctable error)
- State
- 1 to the client; the error log records 18456 state 40
- Product
- Microsoft SQL Server Database Engine
- Versions
- All supported SQL Server versions (2016 to 2025), Azure SQL Managed Instance
- Category
- Logins & Permissions
- Last reviewed
- 23 October 2026
On this page
What it means
Error 4064 means the login is valid but the connection did not name a database, and the default database configured for the login could not be opened, so SQL Server refused the connection.
Every connection starts in a database. When the connection string or the SSMS dialog does not name one, SQL Server uses the login default database (default_database_name in sys.server_principals). If that database is gone, unavailable, or closed to this login, the connection fails with 4064 and the client also sees error 18456.
The message never names the database, so start with the login: look up its default database, then check that database. The error log entry for the same moment (18456, state 40, reason Failed to open the database ... specified in the login properties) names it. Because the failure happens during login, there is no session in which to run USE master; you have to name a reachable database in the connection itself.
Common causes
- The default database was dropped, renamed or detached, and the login still points at it.
- The default database is OFFLINE, SUSPECT (shown as Recovery Pending), in EMERGENCY, or is a mirroring mirror.
- The default database is in SINGLE_USER mode with its only connection taken, or in RESTRICTED_USER mode and the login is not db_owner, dbcreator or sysadmin.
- The login has no user in its default database (an orphaned user after a restore or migration), or its user was denied access.
- A Windows login reaches SQL Server through several groups, and the default database of one of those groups is unavailable.
Diagnose it
Read-only These queries only read metadata and dynamic management views. They do not change data, permissions or configuration, so they are safe to run first.
Find the default database of the login
Run as an administrator from any working connection.
SELECT name, type_desc, default_database_name, is_disabled
FROM sys.server_principals
WHERE name = N'app_user'; The default_database_name is the database 4064 failed to open. Check it in the next query.
Check that the default database is there and open
SELECT name, state_desc, user_access_desc
FROM sys.databases
WHERE name = N'Sales'; No row means the default database no longer exists. Anything other than ONLINE and MULTI_USER explains the error without a permission problem.
List every login whose default database is missing or not online
SELECT sp.name AS login_name, sp.default_database_name,
ISNULL(d.state_desc, 'MISSING') AS database_state
FROM sys.server_principals AS sp
LEFT JOIN sys.databases AS d ON d.name = sp.default_database_name
WHERE sp.type IN ('S', 'U', 'G')
AND (d.name IS NULL OR d.state_desc <> 'ONLINE'); Each row is a login that will fail with 4064 whenever it connects without naming a database.
Read the error log entry that names the database
EXEC sp_readerrorlog 0, 1, N'specified in the login properties'; Lines with Reason: Failed to open the database 'Sales' specified in the login properties, after Error: 18456, Severity: 14, State: 40.
How to fix it
Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.
Connect by naming a database you can open
Client or network sideThis gets the user in straight away and changes nothing on the server. With sqlcmd use -d; in a connection string set Database (or Initial Catalog); in SSMS choose Options, Connection Properties, Connect to database. Use master only if the login can open it (it normally can through the guest user).
-- sqlcmd
-- sqlcmd -S sql01 -E -d master
-- Connection string
-- Server=sql01;Database=master;Integrated Security=true;
Change the login default database
Changes permissionsPoint the login at a database that is always available. This needs ALTER ANY LOGIN (and CONTROL SERVER when the login being changed is a sysadmin).
ALTER LOGIN [app_user] WITH DEFAULT_DATABASE = master;
Make the original default database usable again
Changes database stateIf the default database is correct but offline or left in single-user or restricted mode after maintenance, return it to normal once the work on it is finished.
ALTER DATABASE Sales SET ONLINE;
ALTER DATABASE Sales SET MULTI_USER;
Give the login a user in its default database
Changes permissionsIf the database is online but the login has no user there, create one. If a user with the same name exists but is orphaned after a restore, remap it to the login instead of creating a second user.
USE Sales;
CREATE USER [app_user] FOR LOGIN [app_user];
-- Orphaned user after a restore:
-- ALTER USER [app_user] WITH LOGIN = [app_user];
Examples
The default database is offline
Shown as documented by Microsoft (MSSQLSERVER_4064 and MSSQLSERVER_18456); not reproduced here, because it needs a login with this default database and creating logins is not allowed on the lab instance.
-- app_user has DEFAULT_DATABASE = Sales, and Sales is taken offline:
ALTER DATABASE Sales SET OFFLINE;
-- app_user then connects without naming a database (SSMS, or sqlcmd without -d).
Cannot open user default database. Login failed. Login failed for user 'app_user'. (Microsoft SQL Server, Error: 4064) Error log: Error: 18456, Severity: 14, State: 40. Login failed for user 'app_user'. Reason: Failed to open the database 'Sales' specified in the login properties. [CLIENT: 10.0.0.15]
The default database was dropped
Shown as documented by Microsoft; not reproduced here. The login keeps the old name in default_database_name, so every connection that does not name a database fails until the default is changed.
-- The login still points at a database that no longer exists:
SELECT name, default_database_name
FROM sys.server_principals
WHERE default_database_name = N'Sales';
Cannot open user default database. Login failed. Login failed for user 'app_user'. (Microsoft SQL Server, Error: 4064)
How to prevent it
Give administrative and service logins a default database that is always online, usually master, and have applications name their own database in the connection string. Then a database going offline affects only the clients that use it, not every login that happens to default to it.
Before you drop, rename, detach or take a database offline, run the "logins whose default database is missing" query above with that name and repoint those logins first. After restoring a database to a new server, check for orphaned users before applications connect.
Common questions
Why can I not run USE master to get past error 4064?
The login fails before a session exists, so there is nowhere to run it. Name a reachable database in the connection itself (sqlcmd -d, Database= in the connection string, or Connect to database in SSMS), then change the default with ALTER LOGIN.
How is error 4064 different from error 4060?
Error 4060 is raised for the database named in the connection string. Error 4064 is raised for the login default database, which is used only when the connection does not name a database.
Which database does error 4064 refer to?
The message does not say. Query default_database_name in sys.server_principals for the login, or read the error log entry with state 40, which names the database.
Learn more on CodeWithSQL
Official documentation
Last technically reviewed 23 October 2026; examples and diagnostics run on SQL Server 2025 (17.0.1145.4). Message text checked against the engine's sys.messages catalogue.