用于修复损坏或离线的 PGDATA、基于 WAL 的行恢复和丢失表的恢复的开源 PostgreSQL 数据恢复工具。
用于修复损坏或离线的 PGDATA、基于 WAL 的行恢复和丢失表的恢复的开源 PostgreSQL 数据恢复工具。
PDU (PostgreSQL Data Unloader) is a professional-grade, open-source disaster recovery and data extraction tool for PostgreSQL databases (versions 14-18). It reads PostgreSQL data files directly without requiring a running database instance—making it the ultimate solution for disaster recovery, forensic analysis, and emergency data extraction.
PostgreSQL DBAs face critical scenarios like these:
▸ Database completely corrupted - won't start, can't recover ▸ Accidental DELETE/UPDATE - critical data gone in seconds ▸ Data files deleted - filesystem corruption or operator error
Traditional tools like pg_filedump, pg_dirtyread, and pg_waldump each solve one problem with different learning curves. PDU unifies all these scenarios into a single, intuitive tool.
In Oracle ecosystems, professionals use ODU/DUL for direct data mining. PostgreSQL deserved the same level of capability—that's why PDU exists.
Two files (pdu + pdu.ini), zero dependencies. One tool handles corruption, deletion, and updates.
100% read-only operations. Your original data files remain untouched. Recover data faster than traditional PITR methods.
Powerful direct data file reading capabilities. Supports PostgreSQL 14-18.
PDU handles three critical disaster scenarios with two recovery methods:
| Scenario | What Happened | PDU Solution | Guide |
|---|---|---|---|
| ▸ Database Corruption | PostgreSQL won't start due to catalog/file corruption | Extract data directly from damaged files | Read → |
| ▸ Accidental DELETE/UPDATE | Wrong DELETE/UPDATE executed in production |
Replay WAL archives to recover original data | Read → |
| ▸ Data File Loss | Specific data files deleted or corrupted | Rebuild data from remaining files | Read → |
1. Direct Data File Extraction
2. WAL Archive Scanning
# Set PostgreSQL version (14-18)
sed -i 's/#define PG_VERSION_NUM [0-9]\+/#define PG_VERSION_NUM /g' basic.h
# Compile
make
# Result: ./pdu executablepdu.iniPGDATA=/var/lib/postgresql/15/main
ARCHIVE_DEST=/var/lib/postgresql/wal_archive./pduPDU> b; -- Bootstrap metadata from PGDATA
PDU> \l; -- List databases
PDU> use production_db; -- Switch database
PDU> \dn; -- List schemas
PDU> set public; -- Switch schema
PDU> \dt; -- List tables
PDU> \d+ customers; -- Describe table structure
PDU> unload tab customers; -- Export one table to CSVFull Guide: pduzc.com/quickstart
Problem: PostgreSQL won't start due to corrupted system catalog.
Solution:
PDU> b; -- Initialize metadata
PDU> use mydb;
PDU> set public;
PDU> unload tab customers; -- Export one table
PDU> unload sch public; -- Export all tables in schema
PDU> unload ddl; -- Generate DDL for current schema
PDU> unload copy; -- Generate COPY script for exported CSVsResult: CSV files exported to current directory, even if PostgreSQL won't start.
Problem: A critical DELETE statement was executed, and you need the deleted rows back.
Solution:
PDU> use production;
PDU> set public;
PDU> param restype delete; -- Scan DELETE records
PDU> param resmode tx; -- Restore by transaction ID
PDU> scan orders; -- Scan WAL for deleted rows
PDU> restore del ; -- Restore one transaction from scan resultsResult: Deleted rows recovered from WAL archives and exported to CSV.
For time-range recovery, use param resmode time;, set param starttime ; and param endtime ;, then run scan orders; and restore del all;.
Problem: An UPDATE statement modified hundreds of rows with wrong values.
Solution:
PDU> use production;
PDU> set public;
PDU> param restype update; -- Scan UPDATE records
PDU> param resmode tx; -- Restore by transaction ID
PDU> scan users;
PDU> restore upd ; -- Restore pre-UPDATE valuesResult: Original values before UPDATE statement recovered from WAL.
More Scenarios: pduzc.com/docs/instant-recovery
| Command | Description |
|---|---|
b; |
Bootstrap metadata from PGDATA |
use ; |
Switch to specified database |
set ; |
Switch to specified schema |
\l; |
List all databases |
\dn; |
List schemas in current database |
\dt; |
List tables in current schema |
\d+ ; |
Describe table structure (columns, types) |
\d ; |
Show table column types |
u tab ; / unload tab ; |
Export one table to CSV |
u sch ; / unload sch ; |
Export all tables in a schema |
u ddl; / unload ddl; |
Generate DDL for the current schema |
u copy; / unload copy; |
Generate COPY statements for exported CSVs |
scan ; |
Scan WAL archives for DELETE/UPDATE records of one table |
scan manual; |
Initialize metadata from files under the manual directory |
scan drop; |
Scan WAL for dropped/truncated table metadata |
meta tab ; |
Write one table's metadata to tab.config |
meta sch ; |
Write all table metadata for a schema to tab.config |
restore del ; |
Restore deleted rows for one scanned transaction |
restore upd ; |
Restore pre-UPDATE rows for one scanned transaction |
restore del all; / restore upd all; |
Restore scanned results in time-range mode |
add ; |
Manually add table metadata; put data files in restore/datafile |
restore db ; |
Initialize a custom database directory (Pro/Enterprise) |
dropscan idx; / ds idx; |
Build dropped-page index (Pro/Enterprise) |
dropscan; / ds; |
Scan disk using tables in tab.config (Pro/Enterprise) |
dropscan iso; / ds iso; |
Recover from an image file using tables in tab.config (Pro/Enterprise) |
dropscan repair; / ds repair; |
Retry failed TOAST table scans (Pro/Enterprise) |
dropscan clean; / ds clean; |
Clean restore/dropscan directories (Pro/Enterprise) |
dropscan copy; / ds copy; |
Generate COPY commands for DropScan output (Pro/Enterprise) |
p startwal ; / param startwal ; |
Set WAL scan start file |
p endwal ; / param endwal ; |
Set WAL scan end file |
p starttime ; / param starttime ; |
Set time-range recovery start time |
p endtime ; / param endtime ; |
Set time-range recovery end time |
p resmode tx|time; / param resmode tx|time; |
Set recovery mode |
p restype delete|update; / param restype delete|update; |
Set recovery type |
p exmode csv|sql; / param exmode csv|sql; |
Set export format |
p encoding utf8|gbk; / param encoding utf8|gbk; |
Set output encoding |
p isomode on|off; / param isomode on|off; |
Set image-save mode |
reset ; / reset all; |
Reset one parameter or all parameters |
show; |
Display current parameters |
t; |
Display supported data types |
exit; / \q; |
Exit PDU |
All interactive commands must end with ;.
Full Reference: pduzc.com/docs
| Parameter | Description | Example |
|---|---|---|
PGDATA |
PostgreSQL data directory path | /var/lib/postgresql/15/main |
ARCHIVE_DEST |
WAL archive directory (for DELETE/UPDATE recovery) | /var/lib/postgresql/wal_archive |
int2, int4, int8, float4, float8, numeric, decimaldate, time, timestamp, timestamptz, intervalvarchar, text, char, bpcharjsonuuidinet, cidr, macaddr, macaddr8int4range, tsrange, etc.)tsvector, tsquery)Comprehensive guides available at pduzc.com/docs/instant-recovery
| Scenario | Description | Official Guide |
|---|---|---|
| Corrupted Instance | PostgreSQL won't start—extract data straight from disk | Open Guide → |
| Corrupted Datafile | Specific datafile damaged—salvage reachable data | Open Guide → |
| Corrupted Database | Entire database broken—reconstruct catalog and data | Open Guide → |
| Deleted Records | Rows accidentally deleted—replay WAL to recover | Open Guide → |
| Updated Records | Wrong UPDATE executed—rollback with WAL analysis | Open Guide → |
| No Backup: DELETE / DROP TABLE / DROP DATABASE | Compare PITR, pg_dirtyread, pg_filedump, PDU, and professional recovery | Decision Guide → |
Q: make fails with "lz4.h: No such file or directory"
A: Install LZ4 development library:
# Ubuntu/Debian
sudo apt-get install liblz4-dev zlib1g-dev
# RHEL/CentOS
sudo yum install lz4-devel zlib-develQ: How do I change the PostgreSQL version target?
A: Edit basic.h and modify PG_VERSION_NUM:
sed -i 's/#define PG_VERSION_NUM [0-9]\+/#define PG_VERSION_NUM 16/g' basic.h
make clean && makeQ: PDU shows "PGDATA directory not found"
A: Verify the PGDATA path in pdu.ini is correct and accessible:
ls -la /var/lib/postgresql/15/mainQ: WAL scanning (scan ) returns no results
A: Ensure:
ARCHIVE_DEST in pdu.ini points to the correct WAL archive directoryQ: Exported CSV files contain garbled characters
A: Check database encoding:
PDU> \l; -- Look at the encoding columnThe CSV will use the database's encoding. Convert if needed:
iconv -f UTF8 -t GBK input.csv > output.csvQ: Unloading large tables takes too long
A: PDU performs full table scans. For better performance:
liblz4-dev (Debian/Ubuntu) or lz4-devel (RHEL/CentOS暂无开放 Issues,或尚未同步最近议题。