// SQL Server error reference

SQL Server error 5120: Unable to open the physical file

SQL Server couldn't open a database file (.mdf, .ndf or .ldf), typically because the service account has no permission or the file is in use.

Error number
5120
Severity
16
Category
Backup and restore
Error message
Unable to open the physical file "%.*ls". Operating system error %d: "%ls".

What it means

When attaching a database, restoring, or bringing a database online, the SQL Server service opens its data and log files. Error 5120 reports that one couldn't be opened, with the operating system error: 5 is access denied, 32 is the file being used by another process, 2 or 3 means the path doesn't exist.

Common causes

  • Operating system error 5: the SQL Server service account lacks permission on the file, often after copying .mdf files from elsewhere.
  • Operating system error 32: another SQL Server instance or process has the file open.
  • The path is wrong or the drive isn't available to the service.
  • Attaching a read-only file or one on removable media.

How to fix it

  1. Grant the SQL Server service account full control on the files and folder (find the account in sys.dm_server_services).

  2. Make sure no other instance uses the files; detach them there first.

  3. Attach using the correct paths.

    CREATE DATABASE SalesDb ON
        (FILENAME = N'D:\Data\SalesDb.mdf'),
        (FILENAME = N'D:\Data\SalesDb_log.ldf')
    FOR ATTACH;

Related errors

More backup and restore errors

The code and message text are from the Microsoft Database Engine events and errors reference. Browse the full SQL Server error list or search every database error.

// fewer errors, faster fixes

Work on SQL Server in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQL Server and the other databases you use.