Troubleshooting Guide: SQL Server Error 5016 — Cannot Change the Name of the System Database
Why This Error Happens
Error 5016 occurs because SQL Server explicitly blocks rename operations on system databases (master, model, msdb, tempdb) to protect core engine functionality and internal references that depend on fixed system database names. This is a hard-coded restriction in the SQL Server engine, not a permissions or configuration issue, so no amount of elevated access will bypass it. The error typically surfaces when an administrator attempts to run ALTER DATABASE ... MODIFY NAME or sp_renamedb against one of these protected databases, often during a scripted migration or naming-standardization task.
Step-by-Step Resolution Guide
Step 1: Confirm Which Database Triggered the Error
- Review the exact error message text, which includes the database name in place of
%.*ls(e.g., “Cannot change the name of the system database ‘master'”). - Run the following in SQL Server Management Studio (SSMS) or
sqlcmdto confirm its system status:
SELECT name, database_id, is_distributor
FROM sys.databases
WHERE name = 'YourDatabaseName';
- System databases always have
database_idvalues of 1 through 4 (master,tempdb,model,msdb).
Step 2: Abandon the Rename Attempt on System Databases
- Do not attempt to force the rename using
sp_renamedb, direct metadata edits, or single-user mode tricks — this can corrupt the instance. - Cancel any deployment scripts or DevOps pipelines (e.g., Azure DevOps, Flyway, Liquibase) that are programmatically trying to rename
master,model,msdb, ortempdb. - Check the script or tool logs located typically at
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOGfor the triggering statement.
Step 3: Redirect the Intended Change to a User Database
- If the goal was to rename a custom/user database that was mistakenly flagged, verify you are connected to the correct SQL Server instance and not accidentally targeting
masterdue to a connection string default. - Run the rename against the correct user database instead:
ALTER DATABASE [OldUserDbName] MODIFY NAME = [NewUserDbName];
- Ensure no active connections exist on the target database before renaming by setting it to single-user mode first:
ALTER DATABASE [OldUserDbName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
Step 4: If tempdb Logical File Names Need Changing (Common Workaround)
- Since
tempdbitself cannot be renamed, but its physical/logical file names sometimes need adjustment, useALTER DATABASEon the files instead:
ALTER DATABASE tempdb MODIFY FILE (NAME = tempdev, FILENAME = 'D:\SQLData\tempdb.mdf');
ALTER DATABASE tempdb MODIFY FILE (NAME = templog, FILENAME = 'D:\SQLData\templog.ldf');
- Restart the SQL Server service via
services.mscorRestart-Service MSSQLSERVERin PowerShell for the file changes to take effect.
Alternative or Verification Steps
- Verify success by querying
sys.databasesagain to confirm the new name is applied only to the intended user database and thatmaster,model,msdb, andtempdbremain unchanged. - If a true “fresh” system database is required (e.g., for disaster recovery rebuild), use the
setup.exe /ACTION=REBUILDDATABASEcommand-line rebuild process instead of attempting an in-place rename.