When a PostgreSQL instance cannot open but the PGDATA is still available, PDU can directly read those files to export data without starting the database service.
iWhen to Use This Method
PostgreSQL instance cannot start or connect due to corruption or misconfiguration
You need to export data from all databases in the instance by reading files directly
The database files are still accessible on disk
This article addresses the scenario where a single PostgreSQL database instance cannot be opened, providing detailed instructions on how to use PDU for data recovery.
Fill in `PGDATA` in `pdu.ini`, i.e., the database directory.
[root@node1 xman]# cat pdu.ini#PostgreSQL data directoryPGDATA=/home/14pg/data#PostgreSQL archive directoryARCHIVE_DEST=#Disk for dropScan to scanDISK_PATH=#Number of data blocks to skip during dropScan. A smaller value provides more comprehensive disk coverage but is slower.BLOCK_INTERVAL=20#PGDATA to exclude during dropscanPGDATA_EXCLUDE=
Enter the pdu directory and execute `b;` to complete initialization.
Use `\l;` to view selectable databases. In the result below, testdb is a directory we manually initialized, representing the business database. restore(PDU) is a database built into PDU used for data recovery in special scenarios.
PDU.public=# \l;┌───────────────────┐│ Database Name │├───────────────────┤│ testdb ││ restore(PDU) │└───────────────────┘
After entering the database, all schemas within the current database and their table counts are automatically displayed. You can also use \dn to view the schemas under the current database.
For production environment recovery, create a new empty database with the same structure as the source database and pre-create the table objects. Use the COPY statements generated by PDU to import the exported CSV files into the new database to achieve data recovery.
For test environment recovery, you can use the DDL statements generated by PDU to create a new target database. Note: the DDL generated by PDU only includes CREATE TABLE; other objects like indexes or views are not included. Then use the COPY statements generated by PDU to import the exported CSV files into the new database to achieve data recovery.