How to Fix ‘Operating system error 5: “5(Access is denied.)” in MS SQL Server?
Summary: The error “Unable to open the physical file” in SQL Server usually occurs due to lack of permissions to create the database. In this post, we will mention the solutions to fix this SQL Server error. In addition, we will mention a SQL repair software that can help recover database in case it gets corrupt or damaged.
When trying to create a database in SQL Server, you may encounter an error, saying “Unable to open the physical file.” This prevents you from creating the database.
The complete error message looks like:
Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file “R:\productions\stellar_database.mdf”. Operating system error 5: “5(Access is denied.)”.
Msg 1802, Level 16, State 7, Line 1
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
The access denied error means that the user creating the database does not have permissions to create files in the folder.
Solutions to Fix the Error “Operating system error 5: “5(Access is denied.)” in SQL Server
You can try the following solutions to resolve this SQL Server error.
1. Check and Grant the Required Permissions
You need to first check that the user has the required permissions.
For this, go to Windows Explorer, right-click the production folder, and select Properties.
Verify if the user executing the T-SQL statement to create the database has permissions to the folder.
If not, press the Edit button. If the user is not in the list, press the Add button to add the user.
You can enter the object name. You can use the Check Name option to verify if you entered it correctly.
If you do not know the name, press the Advanced button to search for the user’s name.
Write your query with the start name or description. In Object Types, you can filter to look at users only. In Locations, you can select a computer or an Active Directory domain.
Once added, make sure to provide Modify permission.
Note: If you do not have permission to grant permissions for the folder, you can ask the system administrator to grant the required permissions.
2. Run as Administrator
Another common problem is that you do not have enough permissions because you are not running the SQL Server Management Studio (SSMS) as an administrator. In this case, the solution is very simple.
In the Windows menu, look for the SSMS, right-click on it, and select the option Run as administrator.
What to do if your database file is damaged or corrupted?
Your database file may get damaged or corrupted due to hardware problem, natural disaster, virus/malware attack, or any other problem. In such a case, you can restore the database from the backup.
If you do not have a current backup, then you can install specialized software to repair the corrupt database. One such SQL repair software is Stellar Repair for MS SQL. This software is created exclusively to fix corrupt or damaged SQL databases.
Using the software is also simple. You just need to provide the name of the database file(s) and the software will fix it for you.
Let’s see how it works.
Before proceeding, bring the database offline. Once it is offline, create a copy and work with the copy. This way you will have a backup, in case something goes wrong.
Now, launch the software and click Browse to select the database file.
Optionally, you can use the Find button to find the database file.
After selecting the database file, press the Repair button to repair your database.
Once repaired, you can save the data. Press the Save icon. You can create a new database or replace the existing one.
In addition, you can save the tables’ data and views in other files, like Excel, CSV, and HTML.
If everything goes fine, you will be able to recover your data.
Conclusion
In this post, we learned how to fix the error “Operating system error 5: “5(Access is denied.)” in SQL Server. The main reason for this error is the lack of permissions. In this case, you need an administrator or a user with privileges to provide permissions to modify files in the folder. In addition, we learned how to fix a damaged database using Stellar Repair for MS SQL.