DLH Logo

SQL Server – How to set a database to read-only; Setting to Read/Write

By Staff Expert WriterTechnical

Last updated: October 29, 2025

SQL Server – How to set a database to read-only; Setting to Read/Write

<p>Sometimes the need arises to migrate a databases for historical purposes and posterity to a lesser used data tier while still keeping the data accessible. Other times there is just the need for a read-only backup or sharable database for development purposes. Whatever the need, the requirement of read-only databases happens more often that one might think.</p>

<p>Sometimes the need arises to migrate a databases for historical purposes and posterity to a lesser used data tier while still keeping the data accessible. Other times there is just the need for a read-only backup or sharable database for development purposes. Whatever the need, the requirement of read-only databases happens more often that one might think.</p> <p>Usually the process involves conducting a full backup and restoration of one database to another SQL Server instance or on another environment.</p> <p>Let&#8217;s start with the idea that you know how to export (backup) and import (restore) a SQL Server database. Then in order to make the database read-only or read/write we can use scripting seen in each respective section below.</p> <h2 class="wp-block-heading">Make a Database Read-Only</h2> <pre class="wp-block-code"><code>USE &#91;master] GO ALTER DATABASE &#91;TESTDB] SET READ_ONLY WITH NO_WAIT GO</code></pre> <h2 class="wp-block-heading">Make a Database Read/Write</h2> <pre class="wp-block-code"><code>USE &#91;master] GO ALTER DATABASE &#91;TESTDB] SET READ_WRITE WITH NO_WAIT GO</code></pre> <p>Based on your need the scripts above should do the trick. Sometimes you get a error message stating the database is already in use. You can stop the database and restart it, then reapply the above selected script, or alternative you can update (ALTER) the database to run in single user mode which would use the following script logic:</p> <pre class="wp-block-code"><code>ALTER DATABASE &#91;TESTDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;</code></pre> <p>In the case of single user mode, this query will rollback and transactions which may have been running on the database and puts the database into a single user mode configuration.</p>

Staff Expert Writer

Staff Expert Writer

databaseSQL Server

Related Articles

Automate Your Data Pipeline Today

Join operators of restaurants, retail, and hospitality brands who use DLH.io to centralize POS, payroll, and operational data.