Published on

July 12, 2025

How to Move SQL Server Database Files to a New Location

As a SQL Server Database Administrator, you may often encounter the need to move user database files to a new location. This could be due to various reasons such as separating the location of log and data files, moving files to a new disk to address storage shortage, or distributing files between multiple disks to improve performance. In this article, we will discuss the process of moving a SQL Server user database’s files to a new location within the same instance.

Let’s assume we have a SQL Server database called TestDB and we want to move its data and log files to a new location while keeping the database in the same instance. We can start by creating a sample database:

USE master
GO

CREATE DATABASE TestDB
GO

USE TestDB
GO

CREATE TABLE TestTable
(
ID INT,
Val CHAR (1)
)
INSERT INTO TestTable(ID, Val)
VALUES (1,'A'), (2,'B'),(3, 'C')

By default, the database’s data and log files are created in the default location. To view the current file locations, we can right-click on the database name in SQL Server Management Studio (SSMS), choose “Properties,” and then navigate to the “Files” section. Alternatively, we can run the following query:

SELECT name AS FileLogicalName, physical_name AS FileLocation
FROM sys.master_files
WHERE database_id = DB_ID(N'TestDB')

Suppose we have specific folders for our data and log files, such as “C:\MSSQL\UserDBData” and “C:\MSSQL\UserDBLog” respectively, and we need to move these files to the corresponding locations without affecting other databases in the instance. Here is the step-by-step process:

  1. Take the database offline:
  2. USE master
    GO
    
    ALTER DATABASE TestDB SET OFFLINE
    
  3. Physically move the data and log files to the new locations.
  4. Alter the database to use the new paths of its files:
  5. USE master
    GO
    
    ALTER DATABASE TestDB
    MODIFY FILE (NAME = TestDB, FILENAME = 'C:\MSSQL\UserDBData\TestDB.mdf')
    
    ALTER DATABASE TestDB
    MODIFY FILE (NAME = TestDB_log, FILENAME = 'C:\MSSQL\UserDBLog\TestDB_log.ldf')
    

    In the above code, “NAME” refers to the logical name of the file, and “FILENAME” represents the new path of the file. Repeat this step for all the files you want to relocate.

  6. Bring the database back online:
  7. USE master
    GO
    
    ALTER DATABASE TestDB SET ONLINE
    

    It is important to note that the necessary permissions to the new folders are required to bring the database online.

    After completing these steps, you can verify that the database is online and the files’ paths have been updated in SQL Server Management Studio.

    By following this process, you can successfully move a user database’s data and log files to a new location within the same instance. Remember to keep the database offline during the entire process of physically copying the files to the new location.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.