To get a SQL database out of standby mode, you first need to identify which kind of standby you are dealing with, because the term describes several different mechanisms with completely different recovery paths. If you are using Microsoft SQL Server, a database that is "in standby" is almost always a log-shipping secondary or an Availability Group secondary in a read-only state, and the fix is either to fail over or to stop the log restore and bring the database online with RESTORE DATABASE ... WITH RECOVERY. If you are using PostgreSQL, you are probably looking at a hot standby server in continuous recovery, and you cannot simply "turn it off" — you must either promote it to primary or shut it down and restart it as a normal writable server. The single most common mistake is assuming there is one generic "disable standby" command; there isn't. The correct procedure depends on how the standby was created, because that determines what state the data files are actually in.
This article separates the three situations readers most often mean, explains what is really happening underneath, and gives the concrete commands and ordering constraints for each. It also covers the things that go wrong — a database stuck "in recovery," a restore that refuses to accept WITH RECOVERY, a promoted replica that still behaves read-only — because those edge cases are usually the real reason someone is searching.
First, figure out which "standby" you have
"Standby mode" is not a formal, universal database state. It is shorthand for at least three distinct arrangements, and the recovery steps differ sharply.
SQL Server log shipping in standby. A secondary database is kept continuously restoring transaction log backups using RESTORE LOG ... WITH STANDBY = 'path\undo.ldf'. The STANDBY option keeps the database readable between restores and writes the information needed to undo uncommitted transactions to an undo file. This is the classic "database in standby mode" in SQL Server documentation.
SQL Server secondary under Always On or a read-only restore. An Availability Group secondary is readable but read-only by AG design; you do not "exit" standby on it — you fail over or remove it from the AG. Separately, a database left mid-restore in RESTORING state is not standby at all, just incomplete recovery.
PostgreSQL hot standby. A server configured with standby.signal and primary_conninfo is in continuous recovery (pg_is_in_recovery() returns true). It is read-only by construction. To make it writable you promote it, or you rebuild it as a standalone server.
The distinction matters because the data-integrity rules are different. In SQL Server log shipping, the undo file tells the engine exactly what to roll back; in PostgreSQL, the system knows what to do only because it was replaying WAL and can finish the replay on promotion. Applying SQL Server recovery steps to PostgreSQL will do nothing, and vice versa.
The direct answer for SQL Server log shipping
If you have a log-shipping secondary and you want it to become a normal, writable database, the operation is: stop applying logs, then recover the database.
The critical point is that you must run RESTORE DATABASE with WITH RECOVERY, not WITH STANDBY, on the database. The syntax is:
If you issued WITH STANDBY on the last restore, the database is left in a state where the engine still expects more log to arrive. RESTORE DATABASE ... WITH RECOVERY finalizes recovery, rolls forward everything committed, rolls back everything uncommitted, drops the standby undo file's role, and makes the database writable. After that, the database can accept connections and writes, and it is no longer a log-shipping secondary.
Before you run it, do three things. First, disable the log-shipping restore job and the copy job on the secondary; otherwise a job may try to restore over the top of your recovered database and fail confusingly. Second, confirm you actually intend to break the log-shipping pair — the primary should be treated as no longer able to resynchronize cleanly without reinitializing. Third, capture the current backup chain if you may need point-in-time recovery later, because once you recover the secondary, it cannot simply continue as a log-shipping target without a fresh initialization.
The WITH RECOVERY call is the key. People often try ALTER DATABASE ... SET ONLINE on a standby database and are surprised it fails. It fails because recovery has not completed; SET ONLINE does not finalize recovery, and the standby state is not the same as a simple OFFLINE state.
Why the undo file matters, and why you sometimes can't recover
When SQL Server restores a log WITH STANDBY, it must leave the database readable while still being able to accept more logs. That requires it to undo uncommitted transactions so readers see a consistent view, but remember what it undid so a later restore can redo them. That memory lives in the undo file.
This produces the specific failure mode people hit: you try RESTORE DATABASE ... WITH RECOVERY and get an error because the standby undo file is missing, inaccessible, or the last restore left the database in STANDBY without a valid undo state. If the undo file was deleted or the service account lost access to it, the database can be stranded. The recovery strategy is to restore the backup chain again from the last full backup and each subsequent log, this time ending with WITH RECOVERY. There is no supported way to finalize recovery from a standby state whose undo information is gone, because the engine cannot know which uncommitted transactions it had rolled back. This is exactly the kind of situation where trying to force the database online by editing files or detaching/attaching is more likely to corrupt it than fix it.
A related error appears when someone runs RESTORE DATABASE ... WITH RECOVERY on a database that is not actually in a restore state at all. SQL Server will tell you the database is already online or that no restore is in progress. That is a signal to step back and check sys.databases.state_desc and sys.databases.user_access, not to keep hammering the command.
Finalizing a database that is stuck in "Restoring"
A very common misdiagnosis is calling a database "in standby" when it is actually stuck with state_desc = 'RESTORING'. This happens when a restore was interrupted, when a tail-log restore is pending, or when a full restore was done without WITH RECOVERY. In this state the database is not readable and refuses connections.
The fix is again RESTORE DATABASE ... WITH RECOVERY. If SQL Server reports that the log or backup set is required, you may need WITH NORECOVERY for the preceding steps and WITH RECOVERY only on the final one. The ordering rule is simple: every restore except the last uses NORECOVERY; the last one uses RECOVERY. Mixing this up is the single most frequent cause of a database sitting in RESTORING indefinitely. If the chain is incomplete, you cannot complete recovery — you must restore from the last full backup and roll forward through each log in sequence. RESTORE HEADERONLY and RESTORE FILELISTONLY let you inspect what you actually have before committing.
If a tail-log backup was never taken from the source and you are recovering to the same instance, a point-in-time restore may not be possible; be honest with yourself about the recovery point objective before choosing WITH RECOVERY.
Always On secondary: you don't "exit standby," you fail over or remove
If the database is a secondary replica in an Availability Group, it shows as read-only and "not synchronizing" or "synchronized" depending on health. It is not in a standby mode that a single command toggles off. Your options are:
Fail over (planned manual failover for synchronous replicas, forced failover for asynchronous ones when the primary is unavailable). After failover the former secondary becomes the primary and is fully writable. The former primary, if it comes back, becomes a secondary and is read-only. Remove the database from the AG on that replica if you want a standalone writable copy. This breaks the AG relationship for that database; re-adding requires seeding or a restore, so plan for the data movement.
You cannot make an AG secondary writable in place while remaining a secondary. The read-only property is enforced by the replication contract, not by a session-level setting you can override. If someone has told you to run ALTER DATABASE ... SET READ_WRITE on an AG secondary, that will fail, and it should — allowing writes would break the log stream.
PostgreSQL: promotion, not a mode switch
For PostgreSQL, a hot standby is a server in recovery that streams or replays WAL. pg_is_in_recovery() returns true, and every write is rejected with "cannot execute ... in a read-only transaction." There is no ALTER SYSTEM SET standby = off.
The supported ways out are:
Promote the standby with pg_ctl promote (or SELECT pg_promote(); on modern versions). This tells the server to finish replaying available WAL, exit recovery, and open for writes. If the primary is still alive, you now have two primaries writing independently — a split-brain risk. Before promoting, ensure the old primary is stopped or fenced, or that you are intentionally doing a controlled switchover with the old primary demoted. Promotion is not reversible by flipping a flag; demoting a promoted server back into standby generally requires rebuilding it from a base backup of the new primary.
Rebuild it as a standalone server if you do not want a new primary: stop the server, remove the standby.signal file, remove primary_conninfo and any restore_command from postgresql.conf (or postgresql.auto.conf), and start it. With no standby signal and no recovery configuration, PostgreSQL starts in normal read-write mode. The catch is that if the WAL it already replayed came from a primary that is still running, your "standalone" copy is a point-in-time fork of that primary and will diverge. That is fine for a test copy; it is dangerous if anyone treats it as the production system of record.
Recovery-in-progress nuance. If a standby is replaying and you just want it writable quickly, promotion is the designed path. If it is stuck in recovery because the WAL source is gone and it has not reached a consistent point, promotion may still work if it has replayed enough to be consistent; otherwise it will not open. Check the server log for the last replayed LSN and whether a consistent recovery point was reached. PostgreSQL will not silently open a server that has not reached consistency, which is a safety property, not a bug.
What "back from standby" usually means in managed cloud services
Readers often arrive here because a managed service shows a replica or standby and they want to use it directly. In Amazon RDS, Azure SQL, and Google Cloud SQL, a standby or read replica is managed; you generally promote it through the console or API, not by running recovery commands on it. The same conceptual rule applies — promotion makes it independently writable and breaks replication — but the operational commands differ and the provider may enforce a final synchronization step. If you run raw RESTORE ... WITH RECOVERY against a managed SQL Server instance you may lack the permissions, and the platform may consider the operation unsupported. Check the provider's documented promotion procedure.
Common pitfalls that make recovery fail
A few recurring problems account for most failed attempts.
The restore chain is incomplete. Ending with WITH RECOVERY on a database that has not had every intervening log restored will fail or will silently produce a database that is missing transactions. Verify the LSN chain with RESTORE HEADERONLY before finalizing.
The database is in SINGLE_USER or RESTRICTED_USER. Recovery can proceed, but afterwards the database may reject normal logins. Reset with ALTER DATABASE ... SET MULTI_USER once recovery completes.
File permissions on the undo file. In log shipping, the SQL Server service account needs read/write access to the standby undo file path. A permission change after a service account rotation is a classic cause of a stuck standby restore. Fix the ACL before retrying; do not delete the undo file to "clean up."
Believing OFFLINE and STANDBY are the same. They are not. Bringing an OFFLINE database online is ALTER DATABASE ... SET ONLINE. A standby database needs RESTORE ... WITH RECOVERY. Using the wrong one wastes time and can create confusion about whether the database is actually recovered.
Promoting a PostgreSQL standby while the primary is alive. This produces two writable servers with divergent histories. Decide deliberately: switchover (demote the old primary) or promotion during a real failure (old primary must be fenced or rebuilt).
Assuming a failed restore can always be resumed. Once a restore is interrupted and the backup media or log chain is unavailable, the only reliable path is a fresh restore from full backup forward. Inventory your backups before starting, not after.
Verification after recovery
Once you have recovered a SQL Server database, confirm sys.databases.state_desc is ONLINE and user_access is MULTI_USER, then run DBCC CHECKDB on a copy or in a maintenance window before trusting it for production. For a promoted PostgreSQL server, confirm pg_is_in_recovery() returns false, check that pg_stat_replication no longer has the old upstream listed as a source, and verify your connection strings and failover tooling point at the right host. For both systems, re-point application connection strings, monitoring, and backup jobs that were configured for the standby role — a database that is technically online but still pointed at by a stale replica configuration is a different kind of outage.
The honest summary is that "standby mode" is not a switch you flip. It is a role conferred by an ongoing recovery or replication arrangement. The way out is to complete recovery (SQL Server: WITH RECOVERY; PostgreSQL: promote or rebuild), or to change the replication role deliberately, and to accept that breaking the standby relationship has consequences for the primary and for future backups. Choose the path that matches how the standby was created, verify the state before and after, and do not attempt to force a database online when the recovery information it needs is genuinely missing — re-restoring is slower but safer than a corrupted database.