Restore and Point-in-Time Recovery

Restore puts the backup files back in place, and recover applies logs on top of them to roll forward to the desired point in time. All three databases apply "full backup -> (incremental/differential) -> logs" in that order, and you specify the stopping point as a time or a log number. Point-in-time recovery discards every change after that point, so it is safer to restore to another server or under another name and move over only the data you need than to do it directly on the production database.

Oracle

Required privileges: SYSDBA or SYSBACKUP (12c+) for RMAN. For Flashback Query, SELECT and FLASHBACK on the target table (or FLASHBACK ANY TABLE).

Pre-restore checks

RESTORE DATABASE PREVIEW;     -- which backup pieces and archive logs will be used
RESTORE DATABASE VALIDATE;    -- actually reads those pieces to check for corruption

Full restore and recovery

SHUTDOWN IMMEDIATE;
STARTUP MOUNT;
RESTORE DATABASE;
RECOVER DATABASE;
ALTER DATABASE OPEN;

If you lost only one data file, keep the database open, take just that file offline, run RESTORE DATAFILE 7; RECOVER DATAFILE 7;, then bring it back online.

Point-in-time recovery (RESTORE/RECOVER UNTIL TIME)

SHUTDOWN IMMEDIATE;
STARTUP MOUNT;
RUN {
  SET UNTIL TIME "TO_DATE('2026-11-25 14:30:00','YYYY-MM-DD HH24:MI:SS')";
  RESTORE DATABASE;
  RECOVER DATABASE;
}
ALTER DATABASE OPEN RESETLOGS;
  • You can also stop with UNTIL SCN 123456789 or UNTIL SEQUENCE 1500 THREAD 1.
  • Opening with RESETLOGS starts a new incarnation. Take a full backup right after opening.

WARNING This rolls the entire database back to a past point, so every later change is lost. If only one table is affected, consider Flashback or RECOVER TABLE below first.

Point-in-time recovery of a single table (RECOVER TABLE, 12c+)

RMAN temporarily creates an auxiliary instance and extracts the table as of that point.

RECOVER TABLE hr.employees
  UNTIL TIME "TO_DATE('2026-11-25 14:30:00','YYYY-MM-DD HH24:MI:SS')"
  AUXILIARY DESTINATION '/u01/aux'
  REMAP TABLE hr.employees:employees_1430;

With REMAP TABLE, the table is created under a new name without touching the original. You need disk space for the auxiliary instance.

View past data with Flashback Query

Reads past data as far back as the undo still holds. It is the fastest way to bring back rows changed by mistake without a restore.

SELECT * FROM hr.employees
AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '15' MINUTE)
WHERE department_id = 50;

-- When and how a row changed
SELECT versions_starttime, versions_operation, employee_id, salary
FROM hr.employees
VERSIONS BETWEEN TIMESTAMP SYSTIMESTAMP - INTERVAL '1' HOUR AND SYSTIMESTAMP
WHERE employee_id = 120;
  • How far back you can go depends on undo_retention and the size of the undo tablespace. If the undo has been overwritten, you get ORA-01555.
  • To bring the data back, move it with INSERT INTO ... SELECT ... AS OF TIMESTAMP ....

Flashback Table

ALTER TABLE hr.employees ENABLE ROW MOVEMENT;
FLASHBACK TABLE hr.employees TO TIMESTAMP (SYSTIMESTAMP - INTERVAL '15' MINUTE);

-- Recover a dropped table from the recycle bin
FLASHBACK TABLE hr.employees TO BEFORE DROP RENAME TO employees_restored;
  • This is an Enterprise Edition feature. It relies on undo, so it has the same limits as Flashback Query.
  • TO BEFORE DROP works only if recyclebin = on and DROP ... PURGE was not used (SELECT * FROM dba_recyclebin;).

WARNING FLASHBACK TABLE reverts all changes to that table since that point. Valid data that arrived in the meantime is lost as well.

FLASHBACK DATABASE

Rewinds the entire database to the past without a restore. It works only on Enterprise Edition, and only if flashback logging was enabled beforehand or a guaranteed restore point was created.

SELECT flashback_on FROM v$database;
SELECT oldest_flashback_scn, oldest_flashback_time FROM v$flashback_database_log;

-- Enable (requires FRA). Retention in minutes is db_flashback_retention_target
ALTER DATABASE FLASHBACK ON;

-- Guaranteed restore point before the work
CREATE RESTORE POINT before_release GUARANTEE FLASHBACK DATABASE;
SELECT name, scn, time, guarantee_flashback_database FROM v$restore_point;
SHUTDOWN IMMEDIATE;
STARTUP MOUNT;
FLASHBACK DATABASE TO RESTORE POINT before_release;
-- or FLASHBACK DATABASE TO TIMESTAMP TO_TIMESTAMP('2026-11-25 14:30:00','YYYY-MM-DD HH24:MI:SS');
ALTER DATABASE OPEN READ ONLY;      -- check the data first
-- After checking: SHUTDOWN IMMEDIATE; STARTUP MOUNT; ALTER DATABASE OPEN RESETLOGS;

WARNING Opening with RESETLOGS discards changes after the rewind point. A guaranteed restore point keeps flashback logs in the FRA until it is dropped, so when you are done, drop it with DROP RESTORE POINT before_release;.

PostgreSQL

Required privileges: permission to create objects in the target database for pg_restore. Physical restore and PITR require the server's OS account (access to the data directory).

View dump contents and restore (pg_restore)

pg_restore -l sales.dump | head -50                       # dump table of contents
createdb -h db02 -U postgres sales_restore
pg_restore -h db02 -U postgres -d sales_restore -j 4 --no-owner sales.dump
pg_restore -h db02 -U postgres -d sales_restore -t orders sales.dump   # a single table
psql -h db02 -U postgres -f globals.sql postgres          # roles first (pg_dumpall --globals-only)
  • -j works only with the custom (-Fc) and directory (-Fd) formats. Load a plain SQL dump with psql -f.
  • --clean --if-exists drops objects with the same name before restoring.

WARNING Restoring into an existing database with --clean drops and recreates the objects in the dump. Data added after the dump is lost. Where possible, restore into a new database and move over only what you need.

Point-in-time recovery (PITR, PostgreSQL 12+)

You need a base backup (pg_basebackup) and the archived WAL that follows it.

# 1. Stop the server and move the existing data directory aside
pg_ctl -D /pgdata stop -m fast
mv /pgdata /pgdata.old

# 2. Unpack the base backup (if it was in tar format)
mkdir /pgdata && chmod 700 /pgdata
tar xzf /backup/base_20261126/base.tar.gz -C /pgdata
tar xzf /backup/base_20261126/pg_wal.tar.gz -C /pgdata/pg_wal

# 3. Set recovery parameters, then create recovery.signal
cat >> /pgdata/postgresql.conf <<'EOF'
restore_command = 'cp /archive/%f %p'
recovery_target_time = '2026-11-25 14:30:00+09'
recovery_target_action = 'pause'
EOF
touch /pgdata/recovery.signal

# 4. Start
pg_ctl -D /pgdata start
  • Since PostgreSQL 12, recovery.conf is gone. Put the settings in postgresql.conf (or postgresql.auto.conf), and the server starts in recovery mode if a recovery.signal file exists. On 11 and earlier, use recovery.conf.
  • recovery_target_action: pause (default; stops at the target and waits read-only), promote (immediately writable), shutdown.
  • Whether to include the transaction at the target time is set by recovery_target_inclusive (default on). You can also stop at a name (recovery_target_name, a point created with pg_create_restore_point()), an LSN (recovery_target_lsn), or an XID.

Check recovery progress and finish

SELECT pg_is_in_recovery(), pg_last_wal_replay_lsn(), pg_last_xact_replay_timestamp();
SELECT pg_get_wal_replay_pause_state();   -- PostgreSQL 14+: not paused / pause requested / paused

-- If it stopped at the target and the data is correct, switch to writable
SELECT pg_promote();                      -- PostgreSQL 12+ (in the paused state, pg_wal_replay_resume() also leads to promotion)
  • While you verify the data, leave it in the pause state and query read-only. If you need an earlier point, you must start over from the base backup (you cannot rewind).
  • Promotion starts a new timeline. When finished, remove recovery_target_* from postgresql.conf and take a new base backup right away.

WARNING Running PITR on the production data directory discards changes after the target time. Do not delete the existing directory; move it aside, and where possible restore on another server or another port.

SQL Server

Required privileges: CREATE DATABASE if the database does not exist; if it does, sysadmin or dbcreator, or db_owner on that database.

Inspect a backup file

RESTORE HEADERONLY   FROM DISK = N'D:\backup\sales_full.bak';   -- backup type, LSN, time
RESTORE FILELISTONLY FROM DISK = N'D:\backup\sales_full.bak';   -- logical file names, paths
RESTORE VERIFYONLY   FROM DISK = N'D:\backup\sales_full.bak' WITH CHECKSUM;
  • VERIFYONLY only checks that the backup file is complete and readable. It does not guarantee the data structures, so the real verification is to periodically restore onto another server and run DBCC CHECKDB.

Tail-log backup

Right after corruption or a mistake, first back up the log written since the last log backup.

BACKUP LOG sales TO DISK = N'D:\backup\sales_tail.trn' WITH NORECOVERY;
-- If the data files are damaged and the database will not open, use WITH NO_TRUNCATE

WARNING Taking a tail-log backup WITH NORECOVERY puts the database in the RESTORING state, and no one can write to it. Use it only when you will continue straight into a restore.

Point-in-time recovery (NORECOVERY, STOPAT)

RESTORE DATABASE sales FROM DISK = N'D:\backup\sales_full.bak'
  WITH NORECOVERY, REPLACE, STATS = 10;
RESTORE DATABASE sales FROM DISK = N'D:\backup\sales_diff.bak'
  WITH NORECOVERY;
RESTORE LOG sales FROM DISK = N'D:\backup\sales_log_202611261300.trn'
  WITH NORECOVERY;
RESTORE LOG sales FROM DISK = N'D:\backup\sales_log_202611261400.trn'
  WITH STOPAT = '2026-11-26T13:45:00', RECOVERY;
  • Every step before the last uses NORECOVERY. Once you finish with RECOVERY, no more logs can be applied.
  • To inspect the data midway, give STANDBY = N'D:\backup\sales_undo.tuf' instead of RECOVERY, and the database opens read-only.
  • Log backups must form an unbroken chain. Check the chain using first_lsn/last_lsn in the history (msdb.dbo.backupset).

WARNING REPLACE overwrites an existing database of the same name. You must also disconnect its connections first (see SINGLE_USER WITH ROLLBACK IMMEDIATE in the sessions document).

Restore under a different name

Leave the production database as is, create a copy as of a past point, and move over only the data you need.

RESTORE DATABASE sales_1345 FROM DISK = N'D:\backup\sales_full.bak'
  WITH MOVE N'sales'     TO N'D:\data\sales_1345.mdf',
       MOVE N'sales_log' TO N'L:\log\sales_1345.ldf',
       NORECOVERY, STATS = 10;
RESTORE LOG sales_1345 FROM DISK = N'D:\backup\sales_log_202611261400.trn'
  WITH STOPAT = '2026-11-26T13:45:00', RECOVERY;

The logical names used in MOVE are the LogicalName values from the RESTORE FILELISTONLY output.

Progress and restore history

SELECT session_id, command, percent_complete,
       DATEADD(SECOND, estimated_completion_time / 1000, GETDATE()) AS eta
FROM sys.dm_exec_requests
WHERE command IN ('RESTORE DATABASE', 'RESTORE LOG', 'BACKUP DATABASE', 'BACKUP LOG');

SELECT destination_database_name, restore_date, restore_type, user_name, stop_at
FROM msdb.dbo.restorehistory
ORDER BY restore_date DESC;