- Error number
- 4060
- Severity
- 11 (user-correctable error)
- State
- 1
- Product
- Microsoft SQL Server Database Engine
- Versions
- All supported SQL Server versions (2016 to 2025), Azure SQL Database, Azure SQL Managed Instance
- Category
- Logins & Permissions
- Last reviewed
- 23 October 2026
On this page
What it means
Error 4060 means the login was accepted but the database named in the connection (Database or Initial Catalog) could not be opened, so the whole connection was refused.
A connection that names a database does two things: it authenticates the login, then it opens that database. Error 4060 comes from the second step. The password was fine; the database in quotes in the message was missing, not online, or not open to this login. The message ends with "The login failed" because SQL Server then rejects the whole login: in our tests both sqlcmd and .NET received error 18456 in the same response.
Severity 11 means the request named something that cannot be used, not that the server is damaged. The client message does not say why the database could not be opened. The SQL Server error log does: it records 18456 with state 38 and the reason Failed to open the explicitly specified database. In our test a database that does not exist and a database set OFFLINE produced exactly the same client output, so check the database itself before changing permissions.
Common causes
- The database name in the connection string is misspelled, points at the wrong server, or differs only in case on a server with a case-sensitive collation.
- The database exists but is not available: OFFLINE, RESTORING, RECOVERY_PENDING or SUSPECT.
- The database is in SINGLE_USER mode and another session holds the only connection, or in RESTRICTED_USER mode and the login is not a member of db_owner, dbcreator or sysadmin.
- The login has no user in that database (for example after a restore from another server), or its user was denied CONNECT.
- The database was dropped, renamed or detached after the connection string was written.
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.
Read the reason from the error log
Run as a member of securityadmin or sysadmin. Search for the database name that appears in the client message.
EXEC sp_readerrorlog 0, 1, N'Failed to open the explicitly specified database'; A line such as Login failed for user 'CONTOSO\appsvc'. Reason: Failed to open the explicitly specified database 'AppDb'. just after Error: 18456, Severity: 14, State: 38. The login name and client address tell you which application is affected.
Check that the database exists and is open for connections
SELECT name, state_desc, user_access_desc, collation_name
FROM sys.databases
WHERE name = N'AppDb'; No row: the name is wrong or the database is gone. state_desc other than ONLINE, or user_access_desc of SINGLE_USER or RESTRICTED_USER, explains the failure without any permission problem.
Check that the login has a user in the database
Run in the target database. Replace the login name with the one in the error log.
SELECT dp.name AS user_name, dp.type_desc, sp.name AS login_name,
perm.state_desc AS connect_permission
FROM sys.database_principals AS dp
LEFT JOIN sys.server_principals AS sp ON sp.sid = dp.sid
LEFT JOIN sys.database_permissions AS perm
ON perm.grantee_principal_id = dp.principal_id
AND perm.permission_name = N'CONNECT'
WHERE sp.name = N'CONTOSO\appsvc'; No row means the login has no user here (unless it reaches the database through a Windows group or the guest user). connect_permission = DENY blocks it even when the user exists.
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.
Correct the database name in the connection string
Client or network sideCompare the name in the message with sys.databases, character by character. Fix Database or Initial Catalog in the application settings, and confirm the connection targets the intended server.
-- Wrong: the catalog does not exist on this server
-- Server=sql01;Database=AppDb;Integrated Security=true;
-- Right:
-- Server=sql01;Database=ApplicationDb;Integrated Security=true;
Bring the database back online or open it to users again
Changes database stateIf the database was taken offline or left in single-user or restricted mode after maintenance, return it to normal. Find out why it was changed first; someone may still be working on it.
ALTER DATABASE AppDb SET ONLINE;
ALTER DATABASE AppDb SET MULTI_USER;
Create a user for the login and grant only what it needs
Changes permissionsIf the database is online but the login has no user in it (common after restoring a database from another server), create the user and add it to the roles the application needs. For a user that exists but was orphaned by a restore, remap it instead with ALTER USER ... WITH LOGIN.
USE AppDb;
CREATE USER [CONTOSO\appsvc] FOR LOGIN [CONTOSO\appsvc];
ALTER ROLE db_datareader ADD MEMBER [CONTOSO\appsvc];
-- Orphaned SQL user after a restore: remap it to the login
-- ALTER USER [app_user] WITH LOGIN = [app_user];
Examples
Connecting to a database that does not exist
Reproduced on the lab instance with Windows authentication. The command line is shown as a comment; the client output and the matching error log lines follow.
-- From a command prompt:
-- sqlcmd -S . -E -C -d AppDb -Q "SELECT 1"
-- Then, as an administrator, read the error log:
EXEC sp_readerrorlog 0, 1, N'AppDb';
Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : Login failed for user 'CONTOSO\appsvc'.. Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : Cannot open database "AppDb" requested by the login. The login failed.. Error log: Error: 18456, Severity: 14, State: 38. Login failed for user 'CONTOSO\appsvc'. Reason: Failed to open the explicitly specified database 'AppDb'. [CLIENT: <local machine>]
The same error when the database is OFFLINE
Reproduced with a scratch database set OFFLINE. A .NET application receives both errors in one SqlException: Number 4060 (Class 11, State 1), then 18456 (Class 14, State 1). The error log again shows state 38, so the client cannot tell "missing" from "offline".
ALTER DATABASE AppDb SET OFFLINE;
-- A new connection with Database=AppDb now fails.
SELECT name, state_desc FROM sys.databases WHERE name = N'AppDb'; -- OFFLINE
Cannot open database "AppDb" requested by the login. The login failed. Login failed for user 'CONTOSO\appsvc'.
How to prevent it
Keep connection strings in configuration that is checked per environment, and test the connection as part of each deployment so a wrong catalog name fails in the pipeline rather than in production. When you restore a database to another server, run the user check above and remap orphaned users before the application starts.
Treat taking a database offline, or into single-user mode, as a change with a plan: list the logins and applications that connect to it, and tell their owners. A monitoring rule on 18456 state 38 in the error log shows quickly which clients still ask for a database that is not available.
Common questions
Why does error 4060 say the login failed when my password is correct?
Authentication succeeded; only opening the database failed. SQL Server then closes the connection and reports error 18456 as well. The error log entry for the same moment (state 38) confirms the password was not the problem.
What is the difference between error 4060 and error 4064?
Error 4060 is about the database named in the connection string. Error 4064 is about the login default database, used when the connection does not name one. Fix 4060 in the connection or the database; fix 4064 by naming another database or changing the default.
Does error 4060 always mean the database does not exist?
No. An offline, restoring, single-user or restricted database, or a login with no user in the database, produces the same client message. Check sys.databases first, then the login user mapping.
Learn more on CodeWithSQL
Official documentation
- MSSQLSERVER_18456 (Microsoft Learn)
- MSSQLSERVER_4064 (Microsoft Learn)
- Database states (Microsoft Learn)
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.