Ransomware-Encrypted PostgreSQL Database Recovery: What Survives and How Tables Are Rebuilt
When ransomware hits a Linux or Windows server running PostgreSQL, the data directory is usually encrypted along with everything else: the server will not start, and every file under the data directory has a new extension. If the backups were on the same server or a reachable share, they are often encrypted too.
Whether the data can come back without the key depends on how the ransomware encrypted the files and on how PostgreSQL stores them. This guide explains both, so you can judge what is realistic before you decide anything.
How PostgreSQL stores a database on disk
Unlike SQL Server or Oracle, which keep a database in a few large files, PostgreSQL stores each table and each index in its own files under base/<database id>/. A table larger than 1 GB is split into segment files of 1 GB each (12345, 12345.1, 12345.2 and so on). Every file is made of 8 kB pages, and each page has a header and row pointers that describe the rows inside it.
Next to the data are the system catalog (small tables such as pg_class and pg_attribute that record which file belongs to which table and what columns it has), the control file global/pg_control, and the write-ahead log in pg_wal (pg_xlog in old versions), made of 16 MB segment files.
What ransomware usually destroys
Many ransomware families encrypt only the first part of each file, or stripes through it, because encrypting every byte of a large server takes too long. On PostgreSQL this has a particular effect: because a big table is split into 1 GB segments, the start of every segment is encrypted, but the rest of each segment is often untouched. A 200 GB table may lose a few megabytes per gigabyte and keep the rest.
Small files are a different story. Tables and indexes smaller than the encrypted region are encrypted from end to end, and that includes most of the system catalog. So the typical result is: large tables mostly intact, small tables and the catalog gone, and the server unable to start because pg_control and the catalog cannot be read.
If the ransomware encrypted every byte of every file with a strong cipher, nothing can be recovered from those files without the key. Measuring sample files first tells you which case you are in.
How the tables are rebuilt
Because every 8 kB page carries its own header and row pointers, the intact pages of a table file can be read directly, without the server and without the catalog. Rows that were in an encrypted region are lost; rows in intact pages can be decoded.
To decode them, the column layout of each table must be known. With the catalog encrypted, that layout comes from somewhere else: an older backup or schema dump, the application's own installation scripts, a test or reporting copy of the database, or, failing those, from analysing the rows themselves. The mapping of file numbers to table names is rebuilt the same way.
The recovered rows are then loaded into a fresh PostgreSQL server of the same major version, checked against what the business expects (row counts, latest invoice numbers, date ranges) and handed over as a working database or as SQL files.
pg_dump backups, base backups and WAL
A plain-text pg_dump (.sql) that was partly encrypted is often the easiest case: everything outside the encrypted region is readable SQL, and the COPY blocks of each table can be cut out and loaded. Custom-format dumps (.dump, pg_dump -Fc) are compressed per table, so an encrypted region can make the rest of that table's compressed block unreadable, but other tables in the same dump can survive.
Base backups (pg_basebackup, tar files) contain the same data files and are recovered like the data directory. Archived WAL files can add changes made after the last backup, but only on top of a consistent base, so they matter most when an older clean backup exists.
Old copies help a lot: last month's dump, a reporting replica or a test database gives the exact schema, and comparing an old file with its encrypted version shows exactly how the ransomware encrypted.
What to do first
Do not run initdb on the encrypted data directory, do not reinstall PostgreSQL over it, and do not try pg_resetwal or repair tools on the encrypted files: they can overwrite the parts that are still intact. Copy the whole data directory, the WAL archive and every dump file to separate storage and work only on copies.
Collect the ransom note, two or three encrypted files (one large table segment, one small file and a dump if you have one), the PostgreSQL version, and any older copy of the database or its schema. Send these for a free assessment: we measure how each file was encrypted and tell you which tables can come back before any work is paid for.
Questions and answers
Can a ransomware-encrypted PostgreSQL database be recovered without paying?
Often, if the ransomware encrypted only part of each file. PostgreSQL splits large tables into 1 GB segments made of 8 kB pages, so the intact pages of each segment can be read and the rows rebuilt. Files encrypted from end to end with a strong cipher cannot be recovered without the key.
The PostgreSQL system catalog is encrypted. Is everything lost?
No. The catalog says which file belongs to which table and what columns it has. When it is encrypted, that information is rebuilt from an older backup or schema dump, the application's installation scripts, a test copy of the database, or by analysing the rows, and the intact table files are then decoded.
Can a partly encrypted pg_dump file be recovered?
A plain-text .sql dump usually can, outside the encrypted region, because it is readable SQL. Custom-format .dump files are compressed per table, so a table hit by the encryption may be lost while the other tables in the same file survive.
Should I run pg_resetwal on the encrypted database?
No. pg_resetwal and similar repairs cannot fix encrypted files and can overwrite the parts that are still intact. Copy the data directory first and work only on copies.
Ransomware data recovery service →
Facing this problem? Contact us now.
We have already solved 656+ ransomware and data recovery cases: encrypted and corrupted SQL Server, Oracle and MySQL databases, Tally, Busy and Marg ERP data, Veeam backups, VMware and Hyper-V virtual machines, RAID and NAS. Send us the details and we tell you what we can recover.
- ✓ Free assessment before you pay anything
- ✓ No data, no charge
- ✓ NDA on request
Email: info [at] asittech.com · Mon–Fri, 9:00–18:00 IST