Database with its label being replaced, representing renaming a SQL Server database

How to Rename a SQL Server Database Safely (and Why You Can’t Access It Afterwards)

How to rename a SQL Server database with ALTER DATABASE MODIFY NAME, fix Msg 5030 and login error 4064, rename the files and find everything that still points to the old name.

March 31, 2023

Renaming a database is a one-line command. But the name lives in many places outside the database: application connection strings, logins’ default databases, Agent jobs, linked server queries, views and procedures written with three-part names. The “I can’t access the database” complaints after the command usually come from these places.

This article covers how to rename a database safely, the common errors, and what to check after the rename.

SQL Server Consulting

Changing a database name or structure?

Aryasoft maps every dependency a change affects in advance and plans the switch without downtime.

Get Support

The Rename Command

sp_renamedb is deprecated; the current method is ALTER DATABASE ... MODIFY NAME. A rename needs exclusive access to the database; if connections are open, you have to end them first:

USE master;

-- Close open connections and take exclusive access (open transactions are rolled back)
ALTER DATABASE SalesDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

ALTER DATABASE SalesDB MODIFY NAME = SalesDB_Archive;

ALTER DATABASE SalesDB_Archive SET MULTI_USER;

If exclusive access can’t be obtained, you’ll see this error:

▤ Messages

Msg 5030, Level 16, State 2 The database could not be exclusively locked to perform the operation.

Warning

WITH ROLLBACK IMMEDIATE rolls back every open transaction in the database and disconnects users. On a production database, do this step in a maintenance window, coordinated with the application teams. Run the commands back to back in the same session, so no other session (an application or an Agent job, for example) grabs the single connection in SINGLE_USER mode before you do.

File Names Don’t Change

Even though the database name changes, the logical file names (SalesDB_Data) and the physical files on disk (SalesDB.mdf) keep their old names. For consistency, you can change them too:

-- Logical names (can be done while online)
ALTER DATABASE SalesDB_Archive MODIFY FILE (NAME = N'SalesDB_Data', NEWNAME = N'SalesDB_Archive_Data');
ALTER DATABASE SalesDB_Archive MODIFY FILE (NAME = N'SalesDB_Log',  NEWNAME = N'SalesDB_Archive_Log');

-- Physical files: define the new paths first, then take it offline and rename the files
ALTER DATABASE SalesDB_Archive MODIFY FILE (NAME = N'SalesDB_Archive_Data', FILENAME = N'E:\Data\SalesDB_Archive.mdf');
ALTER DATABASE SalesDB_Archive MODIFY FILE (NAME = N'SalesDB_Archive_Log',  FILENAME = N'L:\Log\SalesDB_Archive_log.ldf');
ALTER DATABASE SalesDB_Archive SET OFFLINE WITH ROLLBACK IMMEDIATE;
-- Rename the files in Windows to match the new names, then:
ALTER DATABASE SalesDB_Archive SET ONLINE;

When renaming the files, make sure the SQL Server service account keeps its permissions on them.

“I Can’t Get In” After the Rename

The login’s default database

Logins whose default database is the old name fail to log in:

▤ Messages

Cannot open user default database. Login failed. Login failed for user ‘AppUser’. (Microsoft SQL Server, Error: 4064)

-- Logins whose default database no longer exists
SELECT name, default_database_name
FROM sys.server_principals
WHERE default_database_name IS NOT NULL
  AND DB_ID(default_database_name) IS NULL;

ALTER LOGIN AppUser WITH DEFAULT_DATABASE = SalesDB_Archive;

Other places to check

  • Application connection strings: the Initial Catalog or Database values.
  • Agent jobs: the database job steps run in, and names inside commands.
  • Three-part names: references like SalesDB.dbo.Table in views, procedures and synonyms in other databases. Use the queries in our dependency guide to find them.
  • Linked server queries: four-part name references to this database from other servers.
  • Backup and maintenance jobs: scripts and file names that hard-code the database name.
  • SSIS, SSRS and ETL connections.

Note

A database that’s part of an Always On Availability Group or database mirroring can’t be renamed directly. Remove it from the group first, rename it and add it back; the database has to be reinitialized on the secondary replicas (seeding or restore). In databases using replication, log shipping or CDC, a rename also requires reconfiguring those features.

A Less Disruptive Path: Synonyms or a Transition Period

When the name has to change but you can’t update every dependency at once, update the code that references the old name gradually. Using synonyms for references from other databases means a future rename only needs an update in one place.

Checklist

  • The change happens in a maintenance window, coordinated with application teams.
  • ALTER DATABASE … MODIFY NAME was used, not sp_renamedb.
  • Login default databases were updated.
  • Connection strings, Agent jobs, and three- and four-part names were checked.
  • AG, mirroring, replication and log shipping were planned separately.
  • Logical and physical file names were updated if needed.
With ALTER DATABASE oldname MODIFY NAME = newname. If connections are open, take exclusive access first with SET SINGLE_USER WITH ROLLBACK IMMEDIATE.
Because an exclusive lock can’t be taken while connections are open on the database. Close the connections or switch to SINGLE_USER mode.
If a login’s default database is still the old name, error 4064 occurs. Fix it with ALTER LOGIN … WITH DEFAULT_DATABASE.
No. Logical names are changed with MODIFY FILE … NEWNAME; physical files are renamed while the database is offline and updated with MODIFY FILE … FILENAME.

SQL Server Consulting

Plan a rename together with its dependencies

Aryasoft’s senior DBA team maps the applications, jobs, logins and code a change affects in advance, and plans the switch to be uninterrupted and reversible.

4.7/5