How to Back Up a SQL Server Certificate and Its Private Key

🔧Part of the DBA-Tools Project, copy/paste SQL Server scripts and health checks.In: Security

If a certificate protects TDE or backup encryption and you lose it, you lose the data. A TDE database cannot be restored anywhere else without the certificate and its private key, and an encrypted backup cannot be restored at all. The backup itself takes a minute. Do it straight after creating the certificate, and keep the files somewhere other than the server.

⚡The short version. In master, run BACKUP CERTIFICATE [name] TO FILE = '...cer' WITH PRIVATE KEY (FILE = '...pvk', ENCRYPTION BY PASSWORD = '...'), then copy both files off the server and keep the password with them.

This post covers finding certificates that have never been backed up, the classic .cer and .pvk pair, the single-file PFX form, the errors each one throws, and proving the backup restores. Everything below was run on SQL Server 2025.


Find Certificates That Were Never Backed Up

Server certificates used for TDE and backup encryption live in master. pvt_key_last_backup_date is NULL until the private key has been exported:

USE [master];
GO
SELECT  name,
        subject,
        expiry_date,
        pvt_key_encryption_type_desc,
        pvt_key_last_backup_date      -- NULL = never exported to .pvk
FROM    sys.certificates
WHERE   name NOT LIKE '##%'           -- skip the internal certificates
ORDER BY name;
sys.certificates in master: seven certificates, every pvt_key_last_backup_date NULL because none has had its private key exported

One catch, tested on SQL Server 2025: only the .pvk export stamps that column. A PFX export leaves it exactly as it was, so a certificate backed up only as PFX still shows NULL. Treat a NULL as “check”, not as proof of no backup.

To see which certificate protects which database, and which ones are close to expiry, use Get Certificates, Keys, and TDE Status.


Back Up to .cer and .pvk

This form works on every supported version. The certificate goes to the .cer file, the private key to the .pvk file, encrypted with a new password that you choose now:

USE [master];
GO
BACKUP CERTIFICATE [TDE_Cert]
TO FILE = N'D:\CertBackup\TDE_Cert_20261028.cer'
WITH PRIVATE KEY
(
    FILE = N'D:\CertBackup\TDE_Cert_20261028.pvk',
    ENCRYPTION BY PASSWORD = N'<long generated password>'
);

Without the WITH PRIVATE KEY block you get the public certificate only, which is no use for restoring TDE or encrypted backups.

The files are written by the SQL Server service account, which needs write access to the folder, and they are never overwritten. Run the same backup twice and the second one fails:

Msg 15240  ·  Level 16  ·  State 1
Cannot write into file 'D:\CertBackup\TDE_Cert_20261028.pvk'. Verify that you have write permissions, that the file path is valid, and that the file does not already exist.

A date in the file name avoids it. If the private key is protected by its own password rather than by the database master key, the backup also needs that password:

Msg 15334  ·  Level 16  ·  State 4
The certificate has a private key that is protected by a user defined password. That password needs to be provided to enable the use of the private key.

Add DECRYPTION BY PASSWORD = N'<current key password>' inside the PRIVATE KEY ( ) block and it runs. pvt_key_encryption_type_desc in the query above tells you which kind you have: ENCRYPTED_BY_MASTER_KEY or ENCRYPTED_BY_PASSWORD.


Back Up to a Single PFX File

SQL Server 2022 added PKCS#12 export, the certificate and private key together in one .pfx file, which is easier to store and to hand to a vault:

USE [master];
GO
BACKUP CERTIFICATE [TDE_Cert]
TO FILE = N'D:\CertBackup\TDE_Cert_20261028.pfx'
WITH FORMAT = 'PFX',
PRIVATE KEY
(
    ENCRYPTION BY PASSWORD = N'<long generated password>',
    ALGORITHM = 'AES_256'
);

ALGORITHM is not optional here. Leave it out and the export fails:

Msg 33594  ·  Level 16  ·  State 2
ALGORITHM must be specified when exporting a certificate using the PFX format. The supported algorithms are 'AES_256', 'TRIPLE_DES_3KEY'.

And as above, a PFX export does not update pvt_key_last_backup_date. If your monitoring alerts on that column, take the .pvk form as well, or the alert never clears. Both exports in a row show it:

-- The .pvk export stamps pvt_key_last_backup_date; the .pfx export does not
USE [master];
BACKUP CERTIFICATE [TDE_Cert]
TO FILE = N'D:\CertBackup\TDE_Cert_20261028.cer'
WITH PRIVATE KEY (FILE = N'D:\CertBackup\TDE_Cert_20261028.pvk',
                  ENCRYPTION BY PASSWORD = N'<long generated password>');
SELECT name, pvt_key_last_backup_date FROM sys.certificates WHERE name = N'TDE_Cert';

WAITFOR DELAY '00:00:02';

BACKUP CERTIFICATE [TDE_Cert]
TO FILE = N'D:\CertBackup\TDE_Cert_20261028.pfx'
WITH FORMAT = 'PFX',
PRIVATE KEY (ENCRYPTION BY PASSWORD = N'<long generated password>', ALGORITHM = 'AES_256');
SELECT name, pvt_key_last_backup_date FROM sys.certificates WHERE name = N'TDE_Cert';
BACKUP CERTIFICATE to .pvk then to PFX: pvt_key_last_backup_date reads the same time after both, the PFX export did not update it

SSMS IntelliSense underlines the PFX options in red and counts two errors; the batch runs fine.

Microsoft Learn: BACKUP CERTIFICATE · CREATE CERTIFICATE


Get the Files Off the Server

A certificate backup on the same disk as the database protects you from nothing. Copy the files to secure storage, and keep the password somewhere else again: a password manager or vault, never a text file next to the backup. If you use backup encryption, the backup sets are useless without these files, so they deserve the same care as the backups.

The files belong to the service account, and an ordinary login cannot read them: opening the .pvk as a non-administrator returned Access is denied. Copy them as an administrator:

# Run as an administrator
Copy-Item -Path 'D:\CertBackup\TDE_Cert_20261028.*' -Destination '\\securestore\sql-certs\SQL01\'

Prove the Backup Restores

A certificate backup that has never been restored is a hope. On a test instance, create the certificate from the files and compare thumbprints with the source. The target database needs a database master key first, or the create fails:

Msg 15581  ·  Level 16  ·  State 1
Please create a master key in the database or open the master key in the session before performing this operation.
USE [master];
GO
-- only if master has no database master key yet
-- CREATE MASTER KEY ENCRYPTION BY PASSWORD = N'<another long password>';

-- from .cer and .pvk
CREATE CERTIFICATE [TDE_Cert]
FROM FILE = N'D:\CertRestore\TDE_Cert_20261028.cer'
WITH PRIVATE KEY
(
    FILE = N'D:\CertRestore\TDE_Cert_20261028.pvk',
    DECRYPTION BY PASSWORD = N'<the backup password>'
);

-- or from the .pfx
-- CREATE CERTIFICATE [TDE_Cert]
-- FROM FILE = N'D:\CertRestore\TDE_Cert_20261028.pfx'
-- WITH FORMAT = 'PFX',
-- PRIVATE KEY (DECRYPTION BY PASSWORD = N'<the backup password>');

SELECT name, pvt_key_encryption_type_desc, thumbprint
FROM   sys.certificates
WHERE  name = N'TDE_Cert';

On the lab, with a second database standing in for the other server, the restore and the comparison looked like this:

-- Lab: a second database stands in for the other server
USE [CertLabRestore];
CREATE CERTIFICATE [TDE_Cert]
FROM FILE = N'D:\CertBackup\TDE_Cert_20261028.pfx'
WITH FORMAT = 'PFX',
PRIVATE KEY (DECRYPTION BY PASSWORD = N'<long generated password>');

SELECT 'source' AS copy, thumbprint FROM master.sys.certificates WHERE name = N'TDE_Cert'
UNION ALL
SELECT 'restored', thumbprint FROM sys.certificates WHERE name = N'TDE_Cert';
CREATE CERTIFICATE FROM FILE with FORMAT PFX, then source and restored copies showing the same thumbprint

Both forms restored with the same thumbprint as the source and the key ENCRYPTED_BY_MASTER_KEY. A wrong password fails with Msg 15465, The private key password is invalid, which is worth finding out now rather than during an outage.

The final test is restoring a TDE database or an encrypted backup on that instance. TDE Restore Fails: Cannot Find Server Certificate (Error 33111) walks through that restore and what fails when the certificate is missing.


Frequently Asked Questions

Do I need to back up the master key as well?
The certificate backup is enough to recreate the certificate elsewhere, as the restore test above shows: the target only needs a master key of its own. Backing up the database master key and the service master key as well makes a full rebuild of the original server simpler. Working with SQL Server Database Master Keys covers both.
When should I back it up?
Straight after creating the certificate, before you enable TDE or take the first encrypted backup. Again whenever it is renewed or rotated, because the new certificate is a different file.
What if the private key is already lost?
If the certificate still exists on the server, export it now; the server can still read the key even if nobody kept a copy. If the server is gone and there is no backup, the TDE data and encrypted backups cannot be recovered.
Is PFX better than .cer and .pvk?
One file instead of two, and a standard format a vault understands. It needs SQL Server 2022 or later, and it does not update pvt_key_last_backup_date. On a mixed estate the .cer and .pvk pair is the form every version can restore.

Summary

Check sys.certificates for a NULL pvt_key_last_backup_date, remembering that only the .pvk export sets it. Back up with BACKUP CERTIFICATE ... WITH PRIVATE KEY, or as PFX on SQL Server 2022 and later with ALGORITHM stated. Copy the files off the server as an administrator, keep the password somewhere else, and prove it with CREATE CERTIFICATE ... FROM FILE and a matching thumbprint on a test instance.


Related

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *