How to Specify the Location of Data Files and Log Files when Creating a Database in SQL Server

The simplest way to create a database in SQL Server is to use CREATE DATABASE my_database without specifying anything else. When you do this, data files and log files are created in the default location (see how to find the default location).

However, sometimes you might want the data files and log files to reside in a different location. If that’s the case, use the following code example to explicitly state your own location for the database’s data files and log files.

USE master;
GO
CREATE DATABASE Solutions
ON
( NAME = Solutions_dat,  
    FILENAME = 'D:\mssql\data\Solutionsdat.mdf',
    SIZE = 10MB,
    MAXSIZE = 50MB,
    FILEGROWTH = 5MB )  
LOG ON
( NAME = Solutions_log,  
    FILENAME = 'D:\mssql\data\Solutionslog.ldf',
    SIZE = 5MB,
    MAXSIZE = 25MB,
    FILEGROWTH = 5MB );
GO

That example uses Windows path conventions (starts with a drive letter and uses a backslash).

Below is an example for Linux and Mac systems:

USE master;
GO
CREATE DATABASE Solutions
ON
( NAME = Solutions_dat,  
    FILENAME = '/var/opt/mssql/data/Solutionsdat.mdf',
    SIZE = 10MB,
    MAXSIZE = 50MB,
    FILEGROWTH = 5MB )  
LOG ON
( NAME = Solutions_log,  
    FILENAME = '/var/opt/mssql/data/Solutionslog.ldf',
    SIZE = 5MB,
    MAXSIZE = 25MB,
    FILEGROWTH = 5MB );
GO

The .mdf file is the data file and the .ldf file is the transaction log file. We specify the size of each file, as well as its maximum size and its file growth. FILEGROWTH specifies the automatic growth increment of the file (the amount of space added to the file every time new space is required).

  • FILESIZE can be specified in KB, MB, GB, or TB.
  • MAXSIZE can be specified in KB, MB, GB, TB, or UNLIMITED.
  • FILEGROWTH can be specified in KB, MB, GB, TB, or %.

The default is KB (for kilobytes).