MSSQLMSSQL 4064

MSSQL 4064: Cannot Open Database Requested by Login

SQL Server error 4064 means the login succeeded but the default database for that login does not exist, is offline, or the user has no access to it.

Example Query

-- Connection string points to a database that was dropped:
Server=myserver;Database=OldAppDB;User=app;Password=secret

Common Causes

  1. 1Database was dropped or renamed after the login's default was set
  2. 2Database is offline or in single-user mode
  3. 3Login's default database was never changed from master after migration

How to Fix It

Connect to master first, then change the default: ALTER LOGIN app WITH DEFAULT_DATABASE = NewAppDB. Bring the database online with ALTER DATABASE OldAppDB SET ONLINE. Check database status with SELECT name, state_desc FROM sys.databases.

Need a reliable database?

Try Supabase — free PostgreSQL with a generous free tier. No credit card required.

Get started free →

Got a query causing this error?

Paste it into SQLbuddy and get an instant plain-English explanation with optimization tips.

Analyze My Query →