# SQL Server Error 4060: Cannot open database requested by the login. The login failed

**Code:** `4060` · **Category:** SQL · **Seen on:** SQL Server, SQL

The login authenticated, but the database named in the connection string doesn't exist or the login can't access it.

## Meaning

Check `Initial Catalog` spelling, that the database is online, and that the login is mapped to a user in that database (`CREATE USER ... FOR LOGIN`). Also check the default database of the login (error 4064).

## Common causes

- Database name typo or wrong server
- Database offline, restoring or dropped
- Login has no user in that database
- Default database missing
- Connection to the wrong instance

## Quick fix

1. Verify the database name and instance
2. Map the login: `USE db; CREATE USER app FOR LOGIN app;`
3. Check `sys.databases` state

## Detailed fix by platform

### SQL

1. 

```sql
SELECT name, state_desc FROM sys.databases;
USE MyDb; CREATE USER app FOR LOGIN app; ALTER ROLE db_datareader ADD MEMBER app;
```


## How to diagnose

1. **Database** — Online?
2. **User mapped** — Yes?
3. **Instance** — Correct?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/sql/mssql-cannot-open-database-requested-by-login-4060)
