Unable to Open the Physical File (Error 5120)

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

Msg 5120  ·  Level 16  ·  State 101
Unable to open the physical file “D:\SQLData\SalesDB.mdf”. Operating system error 5: “5(Access is denied.)”.
“Access is denied” does not always mean permissions. Reproduced on SQL Server 2025: a file already open by SQL Server itself and a file the service account genuinely cannot read produce the identical message. Rule out “something already has it” before you start granting rights.

The number after Operating system error is the part worth reading. It is a Windows error code, not a SQL Server one, and it is the only thing in the message that narrows anything down.


Read the Operating System Error, Not the SQL One

Three attach attempts on the same instance, each with a different underlying problem:

OS errorTextWhat was actually wrong
2The system cannot find the file specified.The path is wrong, or the file was never copied. Usually a typo or a missing step.
5Access is denied.Either the file is already open by something (including SQL Server itself) or the service account has no permission on it.
32The process cannot access the file.Another process is holding it. Antivirus and backup agents are the usual suspects.

Errors 2 and 32 are self-explanatory. Error 5 is the one that wastes time, because it has two completely different causes and the message treats them the same.


The Trap: Access Denied Without a Permissions Problem

Verified directly. A database was created and left online, then a second attach was attempted against its own data file:

-- SalesDB is attached and running
CREATE DATABASE SalesDB_Copy
    ON (FILENAME = N'D:\SQLData\SalesDB.mdf')
    FOR ATTACH;
Msg 5120  ·  Level 16  ·  State 101
Unable to open the physical file “D:\SQLData\SalesDB.mdf”. Operating system error 5: “5(Access is denied.)”.

Nothing is wrong with the permissions. SQL Server already has that file open and is refusing to open it twice, and Windows reports that as access denied. Grant every right you like and it will not change.

So the first question for an error 5 is not “who needs permission” but “is this file already in use”. Check before touching a single ACL:

-- is SQL Server already using this file?
SELECT  DB_NAME(database_id) AS database_name,
        name AS logical_name,
        physical_name,
        state_desc
FROM    sys.master_files
WHERE   physical_name LIKE '%SalesDB%';

If a row comes back, the file belongs to a database this instance already knows about, and the answer is to detach it, restore under a different name, or stop trying to attach a copy of something that is already here.


When It Really Is Permissions

The genuine case looks identical and has a different fix. It usually happens after a human copies a file: the copy inherits the copier’s permissions, and the SQL Server service account is not the copier.

Find out who the service actually runs as, because it is often not what people assume:

SELECT  servicename,
        service_account,
        startup_type_desc,
        status_desc
FROM    sys.dm_server_services;

On the instance used for this post that returned NT Service\MSSQLSERVER, a virtual account. That is the default on a modern install, and it is exactly why a freshly copied file is unreadable: the account is local to the machine, it is not a domain user, and nobody thinks to grant it anything.

Grant the service account read and write on the folder, not on the individual file. New files created later inherit the folder permissions, so folder-level is the fix that lasts:

# from an elevated prompt, adjust the account and path
icacls "D:\SQLData" /grant "NT Service\MSSQLSERVER":(OI)(CI)F

It Never Arrives Alone

On an attach or a create, 5120 is followed by a second error:

Msg 1802  ·  Level 16  ·  State 7
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

And this is the part that matters for anything scripted: tested with all three failures inside TRY/CATCH, ERROR_NUMBER() returned 1802 every time, never 5120. Your error handler sees the useless message, not the useful one.

“Check related errors” is the documentation telling you the detail lives elsewhere. So when a job logs a bare 1802, do not go looking for a filename in it. Read the SQL Server error log, where both messages are recorded in order:

EXEC xp_readerrorlog 0, 1, N'Unable to open the physical file';

Microsoft’s reference covers Database detach and attach and sys.dm_server_services in full.


Common Questions

I granted Full Control and it still says access is denied.
Then it is almost certainly not a permissions problem. The most common cause is that the file is already open, very often by this same SQL Server instance. Check sys.master_files for the physical path before changing anything else.
Which account do I actually grant to?
Whatever sys.dm_server_services reports, which on a default modern install is a virtual account like NT Service\MSSQLSERVER rather than a domain user. Grant on the folder with inheritance, so files added later are covered too.
Does this happen on restore as well as attach?
Yes, and on startup. Anything that needs SQL Server to open a file on disk can raise it: attaching, restoring to a path it cannot write, or a database failing to come online after a volume changed. The operating system error still tells you which flavour you have.
Why does my error handler only show 1802?
Because 1802 is the terminating error for the statement and 5120 is a related error raised alongside it. Verified on SQL Server 2025: ERROR_NUMBER() returned 1802 for a missing file, a file in use, and a file with no permission. Read the error log for the 5120.
The file is on a network share.
That brings its own version of this. A virtual account has no identity off the machine, so a UNC path usually needs a domain service account and the share permissions to match. Local storage is the simpler answer where you have the choice.
Could antivirus be holding the file?
Yes, and it usually shows as operating system error 32 rather than 5. Database files are a classic antivirus exclusion for this reason: the scanner opens the file, SQL Server cannot, and nothing in the SQL error message points at the scanner.

Related Scripts

Comments

Leave a Reply

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