How to recover a PostgreSQL database from ZFS RAIDZ2 on FreeBSD via SSH

How can you recover a PostgreSQL database on FreeBSD if its files have been deleted or its ZFS pool and GPT partitions have been destroyed? We show how to use Hetman RAID Recovery over SSH to access a RAIDZ2 array with only three of its five SSDs, recover the cluster data directory and a backup from a snapshot, and then verify database operation in DBeaver.

How to recover a PostgreSQL database from ZFS RAIDZ2 on FreeBSD via SSH

RAIDZ2 redundancy protects against the failure of up to two members of a single vdev, but is no substitute for backups. Deleted files, a destroyed pool or a damaged partition table can make both the database and backups stored on the same storage inaccessible.

In this test, Hetman RAID Recovery recovers files from two ZFS pools, while PostgreSQL and DBeaver are used to verify and logically restore the database. We demonstrate the software on Windows; it is also available for Linux and macOS.

Go to view
How to Recover a PostgreSQL Database from ZFS RAIDZ2 on FreeBSD via SSH

How to Recover a PostgreSQL Database from ZFS RAIDZ2 on FreeBSD via SSH

FreeBSD, ZFS RAIDZ2 and PostgreSQL test environment

For the experiment, we prepared a Dell PowerEdge R710 server running FreeBSD 15.1. We created three GPT partitions on each of five SSDs and built three separate ZFS pools from them, each using one partition from each SSD.

ParameterSpecification
ServerDell PowerEdge R710
Operating systemFreeBSD 15.1-RELEASE-p3
Drives5 SSD
PartitioningGPT, 3 partitions on each SSD
File system and configurationZFS RAIDZ2, 5 members per pool
Pools analyzedpool2, pool3
DBMSPostgreSQL 17.11
Database clientDBeaver
Access during recovery3 of 5 SSDs over SSH
FreeBSD terminal: pool1, pool2 and pool3, each with five RAIDZ2 members in the ONLINE state

The PostgreSQL test database test_db simulates a service center. The service_center schema contains the devices, repair_orders and order_history tables, with information about devices, repair orders and their change history. We use DBeaver to view the tables.

DBeaver: original test_db database, service_center schema, tables and devices records

Physical cluster copy and logical dump

We prepared two sets of files: the complete PostgreSQL data directory (data) and a dump of an individual database in Custom format with the .backup extension. The data directory contains physical cluster files, whereas the dump is intended to restore the structure and data of a single database using pg_restore.

Copies of both sets were placed on pool2 and pool3. Before copying the data directory to pool3, we stopped PostgreSQL and did not restart it until copying was complete. A conventional file copy of a running cluster without a dedicated backup procedure may be inconsistent. See the PostgreSQL 17 documentation for details.

DBeaver: settings for creating a PostgreSQL dump in Custom format

Two data loss scenarios

On pool2, we created a recursive snapshot of the root dataset and its child datasets. We then deleted the files in the PostgreSQL directory and the dump folder. To recover the backup, we will use the state preserved in the snapshot before deletion.

FreeBSD terminal: zfs list -t snapshot -r pool2, root and child snapshots named snapshot_0
FreeBSD terminal: file list before deletion and empty result of a subsequent find

On pool3, we ran zpool destroy -f pool3, then deleted the corresponding GPT partitions on all five SSDs. We will recover the physical PostgreSQL cluster copy from this pool. The screenshots show the destroy command and deletion of one of the corresponding GPT partitions.

FreeBSD terminal: execution of zpool destroy -f pool3 in the laboratory test
FreeBSD Control Panel: deletion of a pool3 GPT partition, mfid4p3 deleted message

After actual data loss, do not create new partitions or pools over the lost ones. Do not run TRIM or other operations that may modify the original media. On SSDs, the outcome depends in part on whether the required blocks remain accessible after space reclamation operations.

Connecting FreeBSD drives over SSH

Launch Hetman RAID Recovery on a separate computer. The FreeBSD server must be reachable over the network, SSH must be active, and the account must have read permissions for the required block devices.

For recovering data from failed RAID arrays, NAS devices, and other multi-disk storage systems.

In the software, establish an SSH connection to the server and add the drives for analysis. In the test, we selected just three of the five SSDs belonging to the pools: mfid3, mfid4 and mfid5 (59.13 GB each as displayed by the software). The SSH wizard detected six physical disk devices on the server, including a separate disk for the zroot system pool; only three were added for analysis. The other two were not made available to the software: this simulates two unavailable members rather than physical damage. To find the destroyed pool3, the disk devices themselves must be read, not just the partitions currently visible to FreeBSD.

Hetman RAID Recovery: FreeBSD 15.1-RELEASE-p3 over SSH, mfid3, mfid4 and mfid5 selected

Stop writing to the original media, stop services using the storage, and work with complete byte-for-byte disk images whenever possible. An SSH connection alone does not protect the media against writes by other FreeBSD processes.

Automatic ZFS RAIDZ2 reconstruction from three drives

After connection, the software automatically detected the pool configurations in the RAID Volumes section. The detected objects include pool3, even though it had been destroyed and its GPT partitions deleted.

Select pool3 and check its parameters. In our test, the software identified ZFS RAIDZ2 and displayed three available components and two Empty disk positions. For pool3, it displayed a total size of 89.98 GB and 23.12 GB for each available component. These are the sizes displayed after the GPT partitions were deleted; they should not be equated with the original partition sizes or full SSD capacity.

Hetman RAID Recovery: pool3, ZFS RAIDZ2, three available components and two Empty disk positions

Check pool2 in the same way. The software identified its RAIDZ2 configuration with three available members of 18 GB each and two Empty disk positions. Double parity allows data reconstruction when two components are missing, provided the required blocks and metadata on the remaining media have survived. It does not guarantee recovery after every type of damage.

Hetman RAID Recovery: pool2, RAIDZ2, three 18 GB components and two Empty disk positions

Recovering physical PostgreSQL files from the destroyed ZFS pool

Scanning pool3

Double-click pool3 in Hetman RAID Recovery. In the analysis wizard, select Fast scan. In the test, a fast scan was sufficient to find the destroyed pool’s directory structure: the wizard reported 39 folders and 1403 files. This is the number of objects found, not a measure of database integrity.

pool3 analysis wizard: Fast scan selected
Fast scan of pool3 over SSH completed: 39 folders and 1403 files

Open the postgresql/data directory. PostgreSQL stores data in its own structure, including the base, global and pg_wal directories. Many files have numeric names that do not correspond to table names. Physical recovery requires a complete, consistent cluster directory rather than individual table files.

Detected postgresql/data directory: base, global, pg_wal and other cluster files

In the test configuration, the required files were in data. If tablespaces, WAL or configuration files are stored outside it, recover these dependencies as well, preserving their respective paths and links.

Saving the data directory

Go back one level, select the entire data directory and click Recovery. Select Save to hard disk and specify a folder on a separate physical drive in the computer. Preserve the original subdirectory structure.

Recovery wizard: destination folder on drive D: and directory structure recovery

Once complete, check that the directory has been saved. The presence of files alone does not demonstrate that the cluster works: we will verify this by starting PostgreSQL and reading records.

Windows File Explorer: saved data directory on drive D:

Recovering a PostgreSQL dump from a ZFS snapshot

Open pool2 and perform a Fast scan. When it is complete, go to Snapshots. In this test, the dataset snapshots are named snapshot_0. A recursive snapshot covers separate datasets, so the software displays multiple objects with the same timestamp. The snapshot of the separate PostgreSQL dataset is displayed as postgresql.

Hetman RAID Recovery: list of pool2 dataset snapshots, including postgresql and snapshot_0

In the test, the db-backups folder belonged to the root dataset of pool2, so look for it in the snapshot_0 object. Open this snapshot, find db-backups and select the dump with the .backup extension.

Detected .backup file in db-backups of snapshot_0

Click Recovery and save the dump to a separate drive on the Windows computer. Finish retrieving both sets of files first. If the snapshot is accessible using native ZFS tools, individual files can also be copied from it without rolling back the entire dataset.

Recovery wizard: saving the dump to a folder on drive D:

Starting PostgreSQL from the recovered data directory

Physical recovery requires a compatible PostgreSQL version, platform, configuration and cluster dependencies. In the test, we used the same installed version, PostgreSQL 17.11.

Work with a copy of the recovered directory, not the only instance of the files found. In a real recovery scenario, start the cluster on separate, healthy storage. In the laboratory test, we returned the files to pool2 only after both recovery operations were complete.

With the service stopped, transfer the data directory into the appropriate postgresql directory. Verify that the service configuration points to the correct directory. Set the PostgreSQL service account as the owner of the files and configure access permissions: the original Unix permissions may not have survived the transfer through Windows. In the laboratory environment, we ran chown -R postgres /pool2/postgresql/data and chmod 700 /pool2/postgresql/data. Permissions on nested files must also meet PostgreSQL requirements.

FreeBSD terminal: chown -R postgres and chmod 700 for /pool2/postgresql/data

Start PostgreSQL and check the service status. In the test, service postgresql start and service postgresql onestatus confirmed that the server was running from /pool2/postgresql/data. If the server does not start, check the log: possible causes include an incorrect path, permissions, missing files or cluster incompatibility. Do not forcibly reset WAL to bypass errors without separate diagnostics.

FreeBSD terminal: service postgresql start and onestatus, pg_ctl: server is running, path /pool2/postgresql/data

Once the server starts successfully, connect to the database through DBeaver. In the test, we opened the tables containing devices, repair orders and change history and viewed the records. This confirms the availability of the data checked through PostgreSQL, but is not a complete verification of all cluster objects.

DBeaver: SELECT query against the devices table in the physically recovered test_db
DBeaver: SELECT query against the order_history table in the physically recovered test_db

Logical PostgreSQL restore from a dump in DBeaver

The recovered .backup file can be used on a functioning PostgreSQL server. In the test, we transferred the dump from Windows to a separate directory on FreeBSD. Create a new empty database with a different name (test_db_restored in the test) to avoid overwriting the physical recovery result.

In DBeaver, right-click the new database and select Tools — Restore. Set Format to Custom and select the recovered dump under Backup file. This format uses pg_restore rather than executing the file as a plain SQL script.

DBeaver: Restore for test_db_restored, Custom format and recovered dump path

Check the target database and click Start. Wait for completion and check the restore log for errors. If necessary, prepare the required roles and extensions in advance: a dump of one database is not a copy of all global cluster objects.

DBeaver: pg_restore completion, constraint creation and task completion message

Refresh the object tree, open the tables and view the records. In our test, after importing the dump, we checked the data in the separate restored database. The screenshot shows a SELECT result for the devices table. The supplied materials do not document a full comparison of record counts or checksums before and after recovery.

DBeaver: separate test_db_restored database, SELECT results for the devices table

Conclusion

In the test, Hetman RAID Recovery automatically reconstructed two ZFS RAIDZ2 pools over SSH using only three of five SSDs. We retrieved the data directory from the destroyed pool3 and used it to start PostgreSQL 17, and obtained a dump from a pool2 snapshot that we imported into a separate database. In both cases, we verified that records were accessible in DBeaver.

The outcome depends on the preservation of ZFS blocks and metadata, the consistency of PostgreSQL files and the availability of required dependencies. A snapshot on the same pool is no substitute for an independent backup. After recovery, verify critical tables, record counts and data integrity, and create a new backup on separate storage.

Vladimir Artiukh

Author: , Technical Writer

Vladimir Artiukh is a technical writer for Hetman Software, as well as the voice and face of their English-speaking YouTube channel, Hetman Software: Data Recovery for Windows. He handles tutorials, how-tos, and detailed reviews on how the company’s tools work with all kinds of data storage devices.

Oleg Afonin

Editor: , Technical Writer

Oleg Afonin is an expert in mobile forensics, data recovery and computer systems. He often attends large data security conferences, and writes several blogs for such resources as xaker.ru, Elcomsoft and Habr. In addition to his online activities, Oleg’s articles are also published in professional magazines. Also, Oleg Afonin is the co-author of a well-known book, Mobile Forensics - Advanced Investigative Strategies.

Recommended For You