- Error number
- 5123
- Severity
- 16 (user-correctable error)
- State
- 1
- Product
- Microsoft SQL Server Database Engine
- Versions
- All supported SQL Server versions (2016 to 2025), Azure SQL Managed Instance
- Category
- Database State & Recovery
- Last reviewed
- 23 October 2026
On this page
What it means
Error 5123 means SQL Server tried to create a new database file (for CREATE DATABASE, ALTER DATABASE ADD FILE or a restore) and Windows refused, almost always because the SQL Server service account cannot write to that folder.
New data and log files are created on the server by the SQL Server service account. When the folder exists but Windows rejects the create, SQL Server reports 5123 with the operating system error and the full path; CREATE DATABASE then stops with 1802. Nothing is created, so you can fix the cause and run the same statement again.
The operating system error tells you why. Error 5 (access denied) is by far the most common: the folder exists but the service account has no write permission, typical for user profile folders, the root of a system drive or a folder created by another administrator. If the folder does not exist at all, SQL Server 2025 raises 5133 (directory lookup failed) instead, and if a file with that name is already there it raises 5170.
Common causes
- The service account has no write permission on the target folder, for example a user Documents folder, C:\Users, or a new data drive whose permissions were never set (OS error 5).
- The path was typed for your workstation rather than the server, and on the server it points at a protected location.
- The folder is on a network share or a drive that the service account cannot write to.
- A restore or ADD FILE uses the original server's paths (MOVE was not specified) and those folders are protected on this server.
Diagnose it
Read-only These queries only read metadata and dynamic management views. They do not change data, permissions or configuration, so they are safe to run first.
Which account creates the files?
SELECT servicename, service_account
FROM sys.dm_server_services; The SQL Server service account. It needs Modify (or Full control) permission on the folder you are creating files in.
Where are files created by default?
SELECT SERVERPROPERTY('InstanceDefaultDataPath') AS default_data_path,
SERVERPROPERTY('InstanceDefaultLogPath') AS default_log_path; Folders that already have the right permissions. If you leave out FILENAME, CREATE DATABASE uses them.
Which folders do existing databases use?
SELECT DB_NAME(database_id) AS database_name, type_desc, physical_name
FROM sys.master_files
ORDER BY database_name, file_id; Paths that work on this server. A new path on a different drive or folder is the likely place the permission is missing.
How to fix it
Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.
Create the files in the default folders
Changes schemaThe simplest fix: let SQL Server choose the instance default data and log folders, which the setup program already gave the service account access to.
CREATE DATABASE AppDb; -- files go to the instance default data and log folders
Use a folder prepared for SQL Server
Changes permissionsIf the files must live elsewhere, create a dedicated folder (for example E:\SqlData) and grant the service account from the diagnostic Full control on it, the same as on the default data folder. Then run CREATE DATABASE with that path.
Caution: Grant access to the SQL Server service account only. Do not give Everyone or Users write access to folders that hold database files.
CREATE DATABASE AppDb
ON (NAME = AppDb_data, FILENAME = N'E:\SqlData\AppDb.mdf')
LOG ON (NAME = AppDb_log, FILENAME = N'E:\SqlLogs\AppDb_log.ldf');
Map files to valid folders when restoring
Changes database stateA restore recreates the files at the paths stored in the backup. On a different server, move them to folders that exist and are writable with WITH MOVE, using the logical names from RESTORE FILELISTONLY.
RESTORE DATABASE AppDb
FROM DISK = N'D:\Backups\AppDb_full.bak'
WITH MOVE N'AppDb' TO N'E:\SqlData\AppDb.mdf',
MOVE N'AppDb_log' TO N'E:\SqlLogs\AppDb_log.ldf';
Examples
Creating a database in a folder the service cannot write to
-- C:\Users exists, but the SQL Server service account cannot create files in it
CREATE DATABASE AppDbArchive
ON (NAME = AppDbArchive_data, FILENAME = N'C:\Users\AppDbArchive.mdf');
Msg 5123, Level 16, State 1, Line 2 CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Users\AppDbArchive.mdf'. Msg 1802, Level 16, State 4, Line 2 CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.
How to prevent it
Prepare data and log folders as part of server setup, with permissions for the service account, and set them as the instance defaults so CREATE DATABASE and restores use them without explicit paths.
In deployment and restore scripts, take paths from SERVERPROPERTY('InstanceDefaultDataPath') and InstanceDefaultLogPath instead of hard-coding folders from another machine.
Common questions
I am a Windows administrator. Why is access denied?
The file is created by the SQL Server service account, not by your login, so your own administrator rights do not apply. Grant the service account permission on the folder.
Is anything left behind after error 5123?
No. CREATE DATABASE fails as a whole (error 1802) and no database is created, so you can correct the path or permission and run it again.
Learn more on CodeWithSQL
Official documentation
- CREATE DATABASE (Transact-SQL)
- Database files and filegroups
- Configure Windows service accounts and permissions
- Database engine errors 5000 to 5999
Last technically reviewed 23 October 2026; examples and diagnostics run on SQL Server 2025 (17.0.1145.4). Message text checked against the engine's sys.messages catalogue.