Transparent Data Encryption is the easiest SQL Server feature to enable and the easiest to lose a database to. Encrypted data is only as recoverable as the certificate protecting the database encryption key, and that certificate is only usable if the instance’s Service Master Key can decrypt it. Restore in the wrong order, or into a fresh instance without the right material, and the database attaches, reports “cannot be opened”, and no number of RESTORE attempts helps.
This covers a standalone SQL Server instance on a Windows VPS — no Always On, no Azure Key Vault: the most common small-application deployment, and the one where recovery material most often exists only on the failed machine.
What TDE actually protects and what it does not
TDE encrypts data and log files at rest, plus backups. It does not encrypt the network channel or restrict who can query tables. The threat model is a stolen disk, a lifted VHDX snapshot, or a backup left on an open share — real risks on a rented server, but not a substitute for column encryption of sensitive fields or a firewall on the listener.
The key hierarchy in one diagram
Four objects sit between a password and a row of data. Understanding the chain is the whole task; every restore failure traces to a broken link in it.
| Layer | Object | Stored in | Loss means |
|---|---|---|---|
| 1 | Service Master Key (SMK) | master DB, protected by DPAPI | Cannot decrypt the TDE certificate |
| 2 | TDE certificate (with private key) | master DB | Database is unrecoverable, permanently |
| 3 | Database Encryption Key (DEK) | Inside the user database | Recreated only by re-encrypting from a valid cert |
| 4 | Data and log pages | MDF/LDF files | Unreadable without 1→2→3 |
The SMK is created at install and protected by the Windows DPAPI machine key, which does not survive a new machine. A fresh instance has a different DPAPI key and therefore a different SMK — which is why a certificate backup alone is not enough.
Step 1: confirm TDE is available, and back up the Service Master Key
TDE required Enterprise edition up to SQL Server 2016; from 2019 it is also in Standard and Web. Check first — a database encrypted under one edition cannot be restored into an edition that will not open it.
The SMK already exists; a backup does not. Create one before touching encryption, and store it off the server:
SELECT SERVERPROPERTY('Edition') AS Edition, SERVERPROPERTY('ProductVersion') AS Version;
GO
USE master;
GO
-- Back up the SMK; the password protects the file, not the DPAPI layer
BACKUP SERVICE MASTER KEY
TO FILE = 'D:\Backup\SMK_prod.bak'
WITH PASSWORD = 'LongRandom-PassPhrase-2026!';
GO
-- If restoring into this instance later:
-- RESTORE SERVICE MASTER KEY
-- FROM FILE = 'D:\Backup\SMK_prod.bak'
-- WITH PASSWORD = 'LongRandom-PassPhrase-2026!';
-- Note: restoring an SMK with FORCE overwrites the existing one and can
-- break other encryption on the target instance. Always try without FORCE first.
Step 2: create the certificate and back it up with its private key
The certificate must be created in master. Backing it up without the private key produces a file that looks fine and is useless — the restore later fails with Msg 15581 or a missing-private-key error.
USE master;
GO
CREATE CERTIFICATE TDECert_Prod
WITH SUBJECT = 'TDE certificate for AppDB',
EXPIRY_DATE = '20360101';
GO
-- The critical step: WITH PRIVATE KEY
BACKUP CERTIFICATE TDECert_Prod
TO FILE = 'D:\Backup\TDECert_Prod.cer'
WITH PRIVATE KEY (
FILE = 'D:\Backup\TDECert_Prod.pvk',
ENCRYPTION BY PASSWORD = 'Another-Long-Random-PassPhrase!'
);
GO
-- Verify both files and the private key presence
SELECT name, pvt_key_encryption_type_desc, expiry_date
FROM sys.certificates
WHERE name = 'TDECert_Prod';
Two files come out: .cer (public) and .pvk (private key). Keep both and the password off the VPS — a certificate backup on the same disk as the database is not a backup.
Step 3: create the DEK and enable encryption
The DEK lives inside each user database, protected by the server certificate. Enabling encryption starts a background scan that encrypts pages in place and grows the log.
USE AppDB;
GO
CREATE DATABASE ENCRYPTION KEY
WITH ALGORITHM = AES_256
ENCRYPTION BY SERVER CERTIFICATE TDECert_Prod;
GO
ALTER DATABASE AppDB SET ENCRYPTION ON;
GO
-- Monitor progress: encryption_state 3 = encrypted, 2 = in progress
SELECT DB_NAME(database_id) AS db, encryption_state, percent_complete, key_algorithm
FROM sys.dm_database_encryption_keys;
Take a full backup and ensure the log has room first. Encrypting a 50 GB database can generate tens of gigabytes of log; a log capped at 2 GB produces error 9002 and stalls the scan mid-way, leaving the database in state 2. Grow the log — do not cancel the scan.
The restore order that actually works
This is the part that gets skipped and causes the outage. Two cases, differing only in whether master travels with the backup set.
- Same instance, restored
master: restoremaster, thenRESTORE SERVICE MASTER KEY ... WITH PASSWORD(try withoutFORCEfirst), then the user database. The certificate arrives insidemaster, so no separate import is needed. - Fresh instance, no
masterbackup: do not restore the old SMK — it conflicts with the new DPAPI key. Create the certificate from the backup withWITH PRIVATE KEYinstead, then restore the database. This is the common path on a rebuilt VPS.
USE master;
GO
-- Step 1 of the restore: recreate the certificate with its private key
CREATE CERTIFICATE TDECert_Prod
FROM FILE = 'D:\Backup\TDECert_Prod.cer'
WITH PRIVATE KEY (
FILE = 'D:\Backup\TDECert_Prod.pvk',
DECRYPTION BY PASSWORD = 'Another-Long-Random-PassPhrase!'
);
GO
-- Step 2 of the restore: restore the encrypted database
RESTORE DATABASE AppDB
FROM DISK = 'D:\Backup\AppDB_full.bak'
WITH MOVE 'AppDB' TO 'D:\Data\AppDB.mdf',
MOVE 'AppDB_log' TO 'D:\Data\AppDB_log.ldf',
RECOVERY, STATS = 10;
GO
-- Verify it is online and encrypted
SELECT name, state_desc, is_encrypted
FROM sys.databases WHERE name = 'AppDB';
If the restore fails with Msg 33111 and Msg 3013, the certificate is missing, named differently, or its private key was not imported. Compare the thumbprint in the error against SELECT thumbprint FROM sys.certificates on the source instance — they must match exactly.
What breaks when the certificate is gone
A database whose certificate no longer exists cannot be opened, decrypted, or repaired — there is no supported recovery. What you will see:
| Message | Meaning | Action |
|---|---|---|
| Msg 33111 — cannot find server certificate with thumbprint | Certificate not present in this instance’s master | Import the .cer + .pvk |
| Msg 15581 — create a master key or open it in session | Certificate present but private key not usable | Re-import with WITH PRIVATE KEY |
Database stuck in RECOVERY_PENDING | DEK cannot be decrypted | Certificate import, then ALTER DATABASE ... SET ONLINE |
| Msg 945 — database cannot be opened, encryption key missing | No usable DEK in the file | Only fixable with the original certificate |
The last row is the irrecoverable one: if the certificate and its private key backup are both gone, the data is gone. This is why the first thing to check on a Windows VPS running SQL Server is whether D:\Backup sits on a different device or object store than the data files.
Permissions, ownership, and rotation
The certificate is a securable in master; a common post-restore failure is one that imported correctly but that a non-sysadmin service account cannot use. Grant narrowly, and rotate on a schedule:
GRANT ALTER ANY CERTIFICATE TO [AppServiceAccount];
-- or, tighter, grant CONTROL on the specific certificate:
GRANT CONTROL ON CERTIFICATE::TDECert_Prod TO [AppServiceAccount];
-- Rotate: create a new cert, re-encrypt the DEK, drop the old one
CREATE CERTIFICATE TDECert_Prod_2027 WITH SUBJECT = 'TDE cert rotation 2027';
GO
USE AppDB;
ALTER DATABASE ENCRYPTION KEY
ENCRYPTION BY SERVER CERTIFICATE TDECert_Prod_2027;
GO
-- Back up the NEW certificate before dropping the old one
USE master;
DROP CERTIFICATE TDECert_Prod;
Back up the new certificate in the same operation that re-encrypts the DEK — a rotation where the new certificate is never backed up replaces one you had a backup for with one you do not.
Verification queries to run quarterly
Configure-once-never-check is a common audit finding. These queries cover the whole chain and belong in a scheduled job with output mailed off-server.
-- 1. Which databases are encrypted, and is the DEK backed by a cert that exists?
SELECT d.name, d.is_encrypted, k.encryption_state, c.name AS cert_name, c.expiry_date
FROM sys.databases d
LEFT JOIN sys.dm_database_encryption_keys k ON d.database_id = k.database_id
LEFT JOIN sys.certificates c ON k.encryptor_thumbprint = c.thumbprint
WHERE d.database_id > 4;
-- 2. Backups actually encrypted?
SELECT database_name, backup_finish_date, is_encrypted, key_algorithm
FROM msdb.dbo.backupset
WHERE backup_finish_date > DATEADD(day, -30, GETDATE())
ORDER BY backup_finish_date DESC;
-- 3. Certificate expiry horizon
SELECT name, expiry_date, DATEDIFF(day, GETDATE(), expiry_date) AS days_left
FROM sys.certificates WHERE pvt_key_encryption_type IS NOT NULL;
Checklist before leaving the instance
- SMK backed up, password recorded in a password manager, file copied off the VPS.
- Certificate backed up with
WITH PRIVATE KEY; both.cerand.pvkpresent and non-zero. - Restore rehearsed on a scratch instance, in the order: certificate → database.
- Backup destination separate from the data volume.
- Encryption state and certificate expiry reviewed on a schedule, with the rotation plan documented.
The restore rehearsal separates a working TDE deployment from a documented one: a certificate backup never used to restore a database is an assumption, and ten minutes on a scratch instance turns it into a fact.



