CREATE FILE Encountered Operating System Error (Error 5123)

🚨Part of the SQL Server Errors series, the exact messages and what actually causes them.

Msg 5123  ·  Level 16  ·  State 1
CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file ‘C:\t5123.mdf’.
The denial is aimed at the SQL Server service account, not at you. Your login had every SQL permission it needed, then Windows refused the engine’s attempt to create or open the file. Grant the service’s account rights on the target folder, or put the file somewhere SQL Server already owns, and the same statement succeeds.

This is the error that makes people check their own permissions, and their own permissions are fine. The statement cleared every check inside SQL Server; the operating system then refused the file operation, and the account it refused is the one the SQL Server service runs as. Whoever you are in the session, the file is created by the engine.

It also never travels alone. Msg 1802, CREATE DATABASE failed, follows it as the summary line, and which error precedes that summary tells you exactly how far the engine got. Both variants below were produced deliberately on a real instance for this post.


Three Messages, One Story

What came backWhat it means
5133 directory lookup failedThe service account cannot even read the folder. It got refused at the door.
5123 CREATE FILE encountered operating system errorThe folder was visible, and creating or opening the file inside it was refused. One level deeper, same underlying cause.
1802 CREATE DATABASE failedThe wrapper that follows either of the above. It carries no cause of its own; the line before it does.

The operating system error number inside the message is the precise complaint: error 5 is access denied, error 3 is path not found. Between the SQL error and the OS error you know which folder to look at and what kind of refusal happened.


The Fix: Give the Service Account the Folder

First, confirm what account the engine actually runs as, because that is who needs the rights:

SELECT servicename, service_account
FROM   sys.dm_server_services;

A default installation runs as the per-service account NT SERVICE\MSSQLSERVER. Grant it Modify on the folder you want the files in, from an elevated prompt:

icacls "D:\SQLData" /grant "NT SERVICE\MSSQLSERVER:(OI)(CI)M"

Or sidestep the question entirely: leave the FILENAME clause off and let the database land in the instance’s default data directory, which the service already owns. That is the correct answer more often than people expect, because a folder chosen ad hoc for one database is exactly the kind that nobody granted rights on.

CREATE DATABASE Reporting;

What Not To Do

  • Do not run the SQL Server service as an administrator to make this stop. The service account’s narrow rights are a feature; widening the account to fix one folder trades a five-minute ACL for a permanent enlargement of what a compromised instance can reach.
  • Do not grant Everyone on the data folder. The engine needs the rights, not the world. Grant the specific service account.
  • Do not confuse this with a full disk. A full disk during file creation speaks with 1105 and its relatives once the database exists. Error 5 inside a 5123 is a permissions conversation.

Common Questions

I am an administrator on the machine. Why am I denied?
Because the file is not created under your token. Your session asked SQL Server, and SQL Server’s service account asked Windows. It is the service account that was refused, and it is the service account that needs the grant.
It works in the default data directory but fails on my D: folder. Why?
Setup grants the service account full rights on the default directories at install time. Any folder you create afterwards starts with whatever it inherited, which usually does not include the SQL Server service. The icacls grant above is the missing step.
Does the same thing happen when attaching a database?
Yes, the message says “open or create” for exactly that reason. Files copied in from another machine or restored by backup tooling often arrive owned by whoever copied them, and the engine cannot open what it cannot read. Same account, same grant, same fix.
What does operating system error 3 mean instead of 5?
Path not found: the folder in the FILENAME does not exist from the service’s point of view. Check for typos first, then for the difference between your drive mappings and what the service session can see, because a mapped drive letter that exists for you frequently does not exist for a service.

Related Scripts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *