Skip to content
Microsoft SQL Server

SQL Server Error 945

Database cannot be opened due to inaccessible files or insufficient memory or disk space

Applies to: All supported SQL Server versions (2016 to 2025), Azure SQL Managed Instance
Last technically reviewed 23 October 2026 on SQL Server 2025 (17.0.1145.4)
The full message
Msg 945, Level 14, State 2, Line 1
Database 'AppDb' cannot be opened due to inaccessible files or insufficient memory or disk space.  See the SQL Server errorlog for details.
Other forms of this message
What ALTER DATABASE ... SET ONLINE reports when a file is missing (reproduced on SQL Server 2025)
Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file "D:\SqlData\AppDb.mdf". Operating system error 2: "2(The system cannot find the file specified.)".
Msg 5181, Level 16, State 5, Line 1
Could not restart database "AppDb". Reverting to the previous status.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.
Error log form
Error: 945, Severity: 14, State: 2.
Database 'AppDb' cannot be opened due to inaccessible files or insufficient memory or disk space.  See the SQL Server errorlog for details.
Error number
945
Severity
14 (user-correctable error)
State
2
Product
Microsoft SQL Server Database Engine
Versions
All supported SQL Server versions (2016 to 2025), Azure SQL Managed Instance
Last reviewed
23 October 2026

What it means

Error 945 means SQL Server knows the database but could not start it, usually because one of its data or log files is missing, moved, locked or not readable by the service account, so the database sits in the RECOVERY_PENDING state.

When SQL Server starts a database (at service startup, after ALTER DATABASE ... SET ONLINE, or after an attach) it must open every file listed for it in sys.master_files. If a file cannot be opened, or there is not enough memory or disk space to start recovery, the database is left in RECOVERY_PENDING, and every later attempt to use it (USE, a query, a login whose default database it is) fails with 945 at severity 14.

The message itself never names the file or the cause. The real reason was logged when the database tried to start: look in the SQL Server error log just before the first 945 for errors such as 5120 or 17204 with an operating system error (2 means the file is not there, 3 the folder is not there, 5 access is denied, 32 the file is locked by another process). That earlier message decides the fix.

Common causes

  • A data or log file was moved, renamed or deleted while the service was stopped or the database was offline, so the path in the catalogue no longer exists (OS error 2 or 3).
  • The SQL Server service account lost permission on the file or folder, for example after files were copied in from elsewhere or the service account was changed outside SQL Server Configuration Manager (OS error 5).
  • The drive or SAN volume holding the files was not available when SQL Server started, for example a disk that comes online after the service.
  • Another process, such as a backup agent, antivirus scanner or a second instance, held the file open (OS error 32).
  • The disk was full or the server was short of memory when recovery had to run.

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 databases are not online?

SELECT name, state_desc, user_access_desc
FROM sys.databases
WHERE state_desc <> 'ONLINE';
What to look for

The affected database shows RECOVERY_PENDING (files could not be opened) or SUSPECT (recovery started but failed: see error 926).

Where does SQL Server expect the files to be?

SELECT file_id, type_desc, name AS logical_name, physical_name, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'AppDb');
What to look for

The exact paths SQL Server tries to open. Check on the server that each file exists at that path, on a drive that is online.

Find the original cause in the error log

EXEC sp_readerrorlog 0, 1, N'AppDb';
EXEC sp_readerrorlog 0, 1, N'Operating system error';
What to look for

Messages such as 5120 or 17204 with the file name and an operating system error, logged when the database tried to start. If the service has restarted since, read the previous logs (first argument 1, 2 and so on).

Which account needs access to the files?

SELECT servicename, service_account
FROM sys.dm_server_services;
What to look for

The SQL Server service account (often NT Service\MSSQLSERVER) must have read and write access to every data and log file of the database.

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.

Make the file available again, then bring the database online

Changes database state

Fix what the error log reported: reconnect the drive, put the file back at the path in sys.master_files, restore the service account permission, or stop the process that locked it. Then ask SQL Server to start the database again. If it starts, recovery runs and the database returns to ONLINE.

ALTER DATABASE AppDb SET ONLINE;

Point the catalogue at the file's real location

Changes database state

If the files were moved on purpose, tell SQL Server the new path (one MODIFY FILE per moved file, using the logical name from the diagnostic), then bring the database online. The path change takes effect the next time the database starts.

Caution: Point each logical file only at its own file from the same database. Never attach or point at a copy taken while the database was running, or at a log file from a different point in time: the database can come up inconsistent.

ALTER DATABASE AppDb MODIFY FILE (NAME = AppDb, FILENAME = N'E:\SqlData\AppDb.mdf');
ALTER DATABASE AppDb SET ONLINE;

Give the service account access to the files

Changes permissions

For OS error 5, grant the SQL Server service account (from the diagnostic) Full control on the data and log files and their folder, the same as on the default data folder. Grant it to that account only.

Caution: Database files contain all the data. Do not open the folder to Everyone or to general user groups to make the error go away.

Restore from backup if the file is lost

Changes database state

If a data file is truly gone or damaged, restore the database from your most recent full backup and the log backups after it. Restore under the same name with REPLACE, or under a new name to compare first.

Caution: RESTORE with REPLACE overwrites the existing database. If the log file is still readable, take a tail-log backup first (BACKUP LOG ... WITH NO_TRUNCATE) so committed work since the last log backup is not lost.

RESTORE DATABASE AppDb
FROM DISK = N'D:\Backups\AppDb_full.bak'
WITH NORECOVERY, REPLACE;
-- RESTORE LOG ... WITH NORECOVERY for each log backup, then:
RESTORE DATABASE AppDb WITH RECOVERY;

Examples

A database whose data file path no longer exists

A throwaway database is taken offline and pointed at a file that does not exist, which is what happens when a file is moved or deleted. The SET ONLINE errors (5120, 5181, 5069, shown in the variants) are caught so the output shows the 945 that every later query receives. The last batch puts the real path back so the database starts again.

USE master;
-- Remember where the data file really is
SELECT name, physical_name INTO #real FROM sys.master_files
WHERE database_id = DB_ID(N'AppDb') AND type_desc = N'ROWS';

-- Simulate a moved file: take the database offline and point it at a file that does not exist
ALTER DATABASE AppDb SET OFFLINE WITH ROLLBACK IMMEDIATE;
ALTER DATABASE AppDb MODIFY FILE (NAME = AppDb, FILENAME = N'C:\Users\Public\AppDb_moved.mdf');
BEGIN TRY ALTER DATABASE AppDb SET ONLINE; END TRY BEGIN CATCH END CATCH;  -- fails: file not found
GO
USE AppDb;   -- any use of the database now fails
GO
SELECT state_desc FROM sys.databases WHERE name = N'AppDb';
GO
-- Fix: point the catalogue back at the real file and start the database
DECLARE @sql nvarchar(max) = (SELECT N'ALTER DATABASE AppDb MODIFY FILE (NAME = ' + QUOTENAME(name)
    + N', FILENAME = N''' + physical_name + N''');' FROM #real);
EXEC (@sql);
ALTER DATABASE AppDb SET ONLINE;
SELECT state_desc FROM sys.databases WHERE name = N'AppDb';
Result
Msg 945, Level 14, State 2, Line 1
Database 'AppDb' cannot be opened due to inaccessible files or insufficient memory or disk space.  See the SQL Server errorlog for details.

state_desc
RECOVERY_PENDING

The file "AppDb" has been modified in the system catalog. The new path will be used the next time the database is started.
state_desc
ONLINE

Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.

How to prevent it

Move database files only with the documented procedure: ALTER DATABASE ... MODIFY FILE first, then take the database offline, move the files, check their permissions and bring it online. Do not rename or move files under a running instance, and keep antivirus and backup agents away from live .mdf, .ndf and .ldf files.

Make sure the volumes that hold database files are available before the SQL Server service starts, and alert on databases whose state_desc is not ONLINE so a RECOVERY_PENDING database is noticed before users report it.

Common questions

What is the difference between RECOVERY_PENDING and SUSPECT?

RECOVERY_PENDING means SQL Server could not even start recovery, typically because a file is missing or inaccessible; nothing is known to be damaged yet. SUSPECT means recovery started and failed, which points to damage. Fix file access for the first; for the second, restore from backup.

Should I detach and reattach the database to fix error 945?

No. Detaching a database that cannot start can leave you unable to attach it again. Fix the file problem and use ALTER DATABASE ... SET ONLINE instead.

Why does the error mention memory and disk space?

The same message covers every reason a database could not start. In practice a missing or inaccessible file is the usual cause; the error log entry before the 945 tells you which one applies.

Learn more on CodeWithSQL

Official documentation

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.

Still stuck on a SQL Server error?

Browse the full SQL Server error library, or get hands-on help from an experienced SQL professional.