SQL Error – Unable to open the physical file. Operating system error 5: “5(Access is denied.)”
Today I Detached a Database from SQL Server and moved the mdf/ldf files to an another drive, but while Attaching those files from the new location I got error below by using following script:
USE [master] GO CREATE DATABASE [NewDatabase] ON ( FILENAME = N'E:\NewDB\NewDatabase.mdf' ), ( FILENAME = N'E:\NewDB\NewDatabase_log.ldf' ) FOR ATTACH GO
Msg 5120, Level 16, State 101, Line 4
Unable to open the physical file “C:\NewDB\NewDatabase.mdf”. Operating system error 5: “5(Access is denied.)”.
Msg 1802, Level 16, State 7, Line 4
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Well the error clearly mentions that the error is due to the limited access of Service Account by which SQL Services are running, it doesn’t have access to the new folder location where the database mdf/ldf files are moved.
So, you just need to go to the new folder location, check the folder Properties and go to Securities tab, select the Service account and click on Edit button.
On the new Security popup window click under the Allow checkbox for Full Control permission.
Now execute the above Attach script and the Database will be attached without any issues.