I have received following question nearly 10 times in last week though emails. Many users have received following error while connecting to the database. This error happens when database is dropped for which is default for some of the database user. When user try to login and their default database is dropped following error shows up.
Cannot open user default database. Login failed.
Login failed for user ‘UserName’. (Microsoft SQL Server, Error: 4064)

The fix for this problem is very simple.
Fix/Workaround/Solution:
First click on Option>> Button of “Connect to Server” Prompt.

Now change the connect to database to any existing database on your server like master or msdb.

Now click on connect and it will successfully log you in. Once you login in the system run following statement for your username and it should fix your problem. Make sure to replace [test] with your username and master with yourdatabase name.
ALTER LOGIN [test] WITH DEFAULT_DATABASE = master
GO

Reference : Pinal Dave (http://www.SQLAuthority.com)










As for me I set everyone to the TEMP database by default. This database always exists and if they run anything in the database by mistake, it disappears on the next reboot. Also, they can’t mess up my master database.
I tried your fix, but I still get the login failure
Thanks for the tip. It worked great!!!!!!
Thanks for this. I locked myself out of my local test account and this fixed it.
In trying to delete a database, and getting “in-use” errors, I found on the web that people were saying to just run
ALTER DATABASE Database SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Well, I did that, and still couldn’t delete the database, because I was the only one logged into it! (Setting it to a single-user mode doesn’t do anything if you are the one user.)
So when trying to right-click the database and get its Properties, I got the 4064 error about log-in failure (since I was already logged into it, somehow, I guess…). Tried this solution and it will work as long as you then re-log into SQL Server fresh. Then you can right-click the database to pull up its properties and then you can reset the database to “MULTI_USER” at the bottom of the “Options” section, under “Restrict Access”.
Still haven’t figured out how to programmatically drop the database, though.
Thanks,
Tom
Thank you!!!!
hi, thanks for the solution.
its really help to sort out things. accidently detached the database. run into trouble due to unable to login with windows authentication.
thank for help
We have a UserID and login that is used by an application. It has been around since mid-2004. It was working as of last week; today I get the 4064 error message. I can log in as myself, using Windows Authentication, to the database server machine; i can see the program Userid’s default database, and can in fact run SELECT queries against various tables. I ran the ALTER statement that you printed above; I also right-clicked the database and made sure that the program UserID had at least SELECT access to the database (it did). I also set the program UserID’s default database to Master. (The database desired is listed in the program’s connection string.)
When I close and restart SQL Server 2005 and attempt to login as the program UserID, I still get the 4064 error message.
I’m stumped.
Thanks, worked. And much simpler than Microsoft’s KB. Used it in SqlExpress2008
Thanks so much. You were really more than helpful….
Dev
This is the best solution i fount the google.
Thanks
Hey,
i got a problem which i didn’t realize last time. I’m using windows authentication, and my server name is for example “home-pc\server”, So how do i set the default database in this case ?
I have tried using the method you give above and replacing the user name with the server name and i get the message below
[
Msg 15151, Level 16, State 1, Line 1
Cannot alter the login 'HOME-PC\KEEM', because it does not exist or you do not have permission.
]
Thanks! The top search hit I tried (MSKB 307864) didn’t work; the SQLCMD syntax generated a “Sqlcmd: ‘-E’: Unknown Option” error. Your solution is more intuitive–and, most importantly, actually works! Thanks again.
i agree… spot on ! chris
So simple !! thanks that worked a treat !
thank u for ur help
I bashed my head against the same rock that Keem did. the solution is to put the qualified user name in brackets like so
ALTER LOGIN [HOME-PC\User] WITH DEFAULT_DATABASE=master
Thx Ross. You were a great help coz I had gotten stuck for all this time on that same position.
Can anyone help me on how to configure my ‘ sql server authentication’ login ?
Thanks Pinal Dave — I had this problem, and am in an environment where I am remote to the box with limited privileges. However, the solution works for me.
Some people have had a problem with their username, and in general I use double quotes for any .NET strings (though the square brackets also work):
alter Login “domain\user” with default_database = master
go
<<>>
alter Login [domain\user] with default_database = master
go
Hi. This did not work for me but this did:
Database properties -> Files
Change owner to relevant user.
Many thanks….Easy, clean and simple solution!!!!
This worked for my login on the master database but I cannot get it to work for the actual database I want the login to refer to.
@Kirk
Ask your SysAdmin to give you access to all other databases. Every login should be mapped to a user in a database, only then a login can enter into that database.
Exception to above comment would be, if you enable Guest A/c in all databases, then you can access all databases with out actually having a user in any of the database, Which ofcourse is very dangerous, Guest A/c should be disabled all the time in all databases ( Except Master and Tempdb ).
~ IM.
Thanks heaps for this, thought i’d stuffed it!
thanks,
it worked :)
Saved my skin there mate. Cheers!
Thanks…
Work!
Thank you, It helped me a lot, made me to recover from panic ………
Thank you Ross, I was about ready to commit seppuku there for a bit.
This helped me! Thanks
Muchas gracias!!!
I have error 4064, according to your instructions its doesn’t solve the problem.
Following error showing while i wanted to open new connection.
‘Failed to open a new connection’
Failed to initialize MSDB database for turning (exit code: -1)(DTAClient)
Please advise.
Thanks
D
Many thanks for the valuable and its really a very good post
I used the same solution, and now I can login. But like many others, I get the same error when I try to actually do anything.
Thank you !!! I stuck with this problem for five days.
This did not work for me, I ran into the issue after detaching my default database. I ended up just creating a new admin user and re-attaching the database in question. Problem solved.
I had same problem but the solution Curvian Vynes mentioned did: Right click on database > Properties > Files > set owner equal to the login of the user.
thanxa
it work’s
i love u
This solution worked great and saved me loads of time. Big Thanks. However all was working fine before so I wonder what changed to cause the problem?
Anyway … Thanks.
Thank you so much! It worked very much.
Thanks a lot.
Fantastic solution !!
Thank so lots…..^_^
Many thanks. It worked great!!!
Hi,
v have always solved my problems. Thanks a ton
thank u for ur help
Thnx,it solved my problem…many thnx
Thank you so much..
Thanks, the tip was very useful.
Worked great!
Hi
Thanks first for getting me a step forward to start my SQL 2008 server
Now I am able to connect with master but not able to change the default user for the instance as it is offline
When I am trying to bring it on line it is showing the same error 4064 login failed
VERY THANKS MY DEAR FRIEND!
nice. thanks my Database experts was unable to help me. thanks. :)
Working Great thnx..!!
Thanks Pinal Dave.
Error 4064 for connecting to sql server 2005 resolved with the help of your simple process. :-)
Thanks a lot. :-)
Manaa
Thanks Pinal Dave.
It worked :)
Thx champ. It works as described.
I was a bit worried as I had Detached the DB for my login!
nice.
Muchas Gracias Broeshhh.. Pero mi problema tenia algo mas… para aquellos que no les salio quizas es porque hicieron algo antes de que el problema surge por ejemplo en mi caso borre una bd que era usado como database x default sin darme cuenta por eso luego al no reconcoer esta database no me permitia, lo que hcie fue entrar con master como me mencioana qui y luego en vez de hacer el “ALTER” crear la base de datos que tenai antes por medio de Scripts.
ENGLISH: For people who still have the problem, maybe it comes becuase before having the issue, you move or configure something wron, in my case, i delete a database, db which was the default db so the solution was accesing by master and then creating again the old db by scripts….
Options allowed me to connect.
Then ran :
ALTER LOGIN [domain\userGroup] WITH DEFAULT_DATABASE = master
GO
Thanks
Thanks for the help. It worked, OK !!!!!!
sir, i have deleted my default user sqlserver 2008. Now unable to login.
Suggest any way out. Reinstall is not possible.
Thanks it worked.
Thank you !
Thanks a lot…saved my a**
I can see this is not new but this articles saved my critical time today. I was doing file group maintenance on a server and brought the database online. I did not know that the database for default for that server. I could not bring it online. I searched google and get to your article. I reconnected to server in SSMS using option, I selected Master and I was in and was able to bring my database online. Thanks once again. Keep up the good work.
thanks for that , it really worked well !
Thanks, worked for me !
mohit,
hi, i did the same as u told but unable to take backup offline
solved my problem.. it work fine
lot of thank Pinal
nice post
Thanks again
Abdul Jabbar Patel
worked like a charm. quick and easy.
thanks pinal
Thanks a lot.
It’s really a great post.
Sunil Sharma
Thanks!
Hmm,
I have the same problem but I cannot fix it with adding the “testdb” name in the “Connect to database” field.
If I fill in “master” it works. But why dows it not work with “testdb” which is assigned to my “testuser”?