How to Fix SQL Server Error 4064 (Msg 4064)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get sql server error 4064 when a login cannot open its assigned default database. You can fix it by setting an online database or fixing the user mapping. The server returns message 4064 with severity 11 and stops the login process right away. It says it cannot open the user default database and that the login failed. Your password is fine, so the problem is the target database state or user permissions. Root causes of sql server error 4064 in the database engine When someone logs in wit

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You get sql server error 4064 when a login cannot open its assigned default database. You can fix it by setting an online database or fixing the user mapping.

The server returns message 4064 with severity 11 and stops the login process right away. It says it cannot open the user default database and that the login failed. Your password is fine, so the problem is the target database state or user permissions.

Root causes of sql server error 4064 in the database engine

When someone logs in without naming a target database, the server uses their default database. If the engine cannot open that database, it blocks the session and throws error 4064. A database user is the identity of a login when it connects to a database. For access to many databases on an instance, a login in master is very common.

Several database changes can trigger this login failure during daily work. Someone might drop, rename, or detach the default database during a server cleanup. The database might also sit offline, in a restoring state, or in suspect mode. Sometimes an admin sets the database to single-user mode for maintenance, blocking all other logins.

Broken user mappings also cause this error after a database restore to another server. The login exists in master, but its link to the database user is now broken. The login also fails if someone removes the database user or revokes its connect rights.

How to diagnose and fix the failure in production

To find the cause, log in with an admin account that uses the master database. Check the server error log to see which login and database failed. Next, check the state of that target database on the server instance. If someone dropped the database, change the login default database to master or an active database.

If the database is online, check the user mapping inside that specific database. Make sure a valid database user exists for the login and has connect rights. All fixed database roles, including the public role, have connect permission by default.

Kubernetes API Server Access + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Do not fix this error by giving the broken login full owner rights. Members of the owner role can do all maintenance tasks and can drop the database. Also, you cannot change the permissions assigned to fixed database roles.

Grant only the exact read or write role that the person or app needs. Members of the data reader role can read all data from user tables and views. Members of the data writer role can add, delete, or change data in user tables. If a login only needs one database, you can use a contained database user instead. A contained user has a password and is not tied to a login in master. An admin must first enable contained databases on the server and on the target database.

How to prevent default database login failures

You can stop error 4064 from coming back by changing how people connect. Set the default database for human logins to master unless they need another one. Master is always online when the server is running, so drops will not break logins.

Ask your app teams to name the target database in their connection settings. When the app names an online database, it skips the broken default database setting. Before you drop or rename any database, check which logins use it as their default. Update those logins first so your users and apps stay working during maintenance.

Govern SQL Server queries and responses in flight

Table grants decide which SQL Server objects a caller may touch. They do not check query intent or rewrite sensitive values in results across every client and agent. By design, hoop.dev sits inline in the TDS wire protocol next to SQL Server as a sidecar for your SQL Server. On every production batch from an engineer or AI agent, hoop.dev decides to pass, mask, block, or hold it for approval.

With hoop.dev guardrails and hoop.dev data masking, banned statements are refused or held for review, matched sensitive fields in supported result columns are masked in memory, and each decision is logged. Check out the code on GitHub.

Frequently asked questions

What does severity 11 mean for error 4064?

Severity 11 means the requested database object or entity is missing or not available. In this case, the server cannot open the default database tied to your login.

Can I log in if my default database is offline without an admin?

Yes, you can specify another online database in your client connection settings before logging in. If your login has user access to that second database, the session will start.

Why did error 4064 start right after a database restore?

Restoring a database from another server often breaks the link between logins and database users. An admin must relink the login in master to the user inside the restored database.

The query runs. The emails stay hidden.