SQL SERVER – FIX: Msg 5123, Level 16 – CREATE FILE Encountered Operating System Error 5

This blog is about another common error reported by many DBA to me. He faced error related to create file in OS.

SQL SERVER - FIX: Msg 5123, Level 16 - CREATE FILE Encountered Operating System Error 5 attach-err-01

Here is the text of the message.

CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file ‘C:\Program Files\Microsoft SQL Server\MSSQL14.SQL2017\MSSQL\DATA\DetachTest.mdf’. (Microsoft SQL Server, Error: 5123)

Important piece in above error message “operating system error 5(Access is denied.)”. From that it is clear that its due to permission issue with the file.

Why this happens?

Whenever a database is detached, the permission of the database files is modified. If any SQL Login has detached the database, then permissions are given to SQL Server Service account. If any Windows account has detached the database, then the permission is given to the account who has detached itself.

To explain the real cause, I decided to reproduce the error. I created a database called DetachTest.  A SQL Service account was set to CONTOSO\SQLService. Now, I logged into the server via SSMS with login CONTOSO\syadmin.

Then I detached the database and here are the permissions. You can view them by going to the file > properties and security tab.

SQL SERVER - FIX: Msg 5123, Level 16 - CREATE FILE Encountered Operating System Error 5 attach-err-02

Now, I logged in as “sa” account, which is a SQL Login. As I explained earlier, SQL Server would try to use a service account to read the file. As you can expect, the file is not readable by any account other than CONTOSO\sysadmin.


By reading the blog so far, you must have got a clue about what needs to be done to avoid “Access is denied” error. If not, then here are the steps.

  • Right-click on the file which is mentioned in the error message and go to properties.
  • In the Properties dialog box, go to Security tab.
  • In the Select Group or user names box (which has sysadmin in above image). Click the Edit button and enter the name you used to login to SQL Server. If its windows account, then use that account else use service account.
  • Click Check Names and verify that the name you are specifying does exist as SQL Server login.
  • Click OK.
  • In the Group or User Names box, select the user name you selected in the above step and give full control permission to the user. Click OK.

Now you are all set to attach database files by using the SQL Server authentication or Windows authentication.

Reference: Pinal Dave (https://blog.sqlauthority.com)


SQL Error Messages, SQL Server, Windows
Previous Post
SQL SERVER – Login Failed – Error: 18456, Severity: 14, State: 38 – Reason: Failed to Open the Explicitly Specified Database
Next Post
SQL SERVER – The Operating System Returned the Error 21(The Device is Not Ready.) While Attempting ‘GetDiskFreeSpace’ on ‘A:\’

Related Posts

3 Comments. Leave new

  • it seems no way to change permission for Database files.

  • Just go to the OS path e.g C:\Users
    While in this path right lick the open space,click give access to> advanced sharing….> security>click everyone>edit(to change permission) for everyone or user or group you want.apply,OK and OK. Go back and execute command

  • Umasankar Godi
    April 21, 2020 9:48 pm

    thank you .It helped me.


Leave a Reply