How to Find the Location of Data Files and Log Files in SQL Server

If you ever need to know where your database files are located, run the following T-SQL code:

USE master;
SELECT 
  name 'Logical Name', 
  physical_name 'File Location'
FROM sys.master_files;

This will return a list of all data files and log files for the SQL Server instance.

Here’s what it looks like when I run that code in SQL Operations Management Studio on a Mac:

Returning the data file and log file location in SQL Server using T-SQL

If you use Windows, your path will look like a Windows path. Something like this:

C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\Music.mdf

Although, if you specified your own location, it could just as easily look like this:

D:\data\Music.mdf

What are These Files?

When you create a database in SQL Server, data files and transaction log files are automatically created.

  • The data files hold the data and objects such as tables, indexes, stored procedures, and views.
  • The transaction log files record all transactions and the database modifications made by each transaction. Transaction log files contain the information that is required to recover all transactions in the database.

If you don’t explicitly specify a location and name for these files, SQL Server will put them in the default location (the default location is specified at the server level). Here’s how to find the default location for data files and log files in SQL Server.

Find Files for a Specific Database

If you only need to find the location of the files for a particular database, you can query the sys.database_files system catalog view instead. Simply switch to the applicable database and run the query.

Here’s an example:

USE Solutions;
SELECT
  name 'Logical Name', 
  physical_name 'File Location'
FROM sys.database_files;

This will return only the data and log files for the Solutions database.