How to fix Error 5018 on Windows Server 2025 / Windows 11

Why This Error Happens

This message appears after executing an ALTER DATABASE ... MODIFY FILE statement to change the physical path of a database file in sys.master_files. SQL Server 2025 updates the system catalog immediately, but the actual file move/rename on disk is not performed automatically — the engine simply records the new path and waits for the next database restart or SQL Server service restart to use it. If the physical file isn’t manually relocated to the new path before that restart, or if the service account lacks permissions on the new directory, the database will fail to come back online.

Step-by-Step Resolution Guide

Step 1: Confirm the Catalog Change Was Intentional

  • Run the following to see what SQL Server now expects as the file path:
SELECT name, physical_name FROM sys.master_files WHERE database_id = DB_ID('YourDatabaseName');
  • Compare the physical_name column against the actual location of the .mdf/.ndf/.ldf file on disk.
  • If the path shown doesn’t match reality, you know the catalog update hasn’t been matched by a physical file move yet.

Step 2: Take the Database Offline and Move the Physical File

  • In SQL Server Management Studio (SSMS) or via T-SQL, set the database offline:
ALTER DATABASE [YourDatabaseName] SET OFFLINE;
  • Manually move/copy the file (using File Explorer or Move-Item in PowerShell) from its old location to the new path exactly as recorded in sys.master_files.
  • Example PowerShell command:
Move-Item "D:\OldPath\YourDB.mdf" "E:\NewPath\YourDB.mdf"

Step 3: Grant Correct NTFS Permissions on the New Path

  • Ensure the SQL Server service account (e.g., NT Service\MSSQLSERVER or a custom domain service account) has Full Control on the new folder.
  • Use icacls to verify/grant permissions:
icacls "E:\NewPath" /grant "NT SERVICE\MSSQLSERVER:(OI)(CI)F"
  • Without this, SQL Server 2025 will throw an access-denied error when trying to reopen the file at startup.

Step 4: Bring the Database Back Online

  • Set the database online again:
ALTER DATABASE [YourDatabaseName] SET ONLINE;
  • Monitor the SQL Server Error Log (via SSMS > Management > SQL Server Logs, or xp_readerrorlog) to confirm the file was found and mounted correctly at the new path.

Step 5: Restart the SQL Server Service (If Needed)

  • If the database still won’t come online, restart the SQL Server service via SQL Server Configuration Manager or:
Restart-Service -Name 'MSSQLSERVER'
  • This forces SQL Server to fully re-read sys.master_files and re-attach using the new physical path.

Alternative or Verification Steps

  • Verify success by querying sys.master_files again and confirming the state_desc column shows ONLINE for all files tied to the database.
  • If the file move was skipped or done incorrectly, you can revert the catalog entry back to the original path (while the DB is offline) using another ALTER DATABASE ... MODIFY FILE command pointing to the old, still-valid location, then retry the move process carefully.