SoFunction
Updated on 2025-04-13

What should I do if PostgreSQL deletes the database incorrectly?

In PostgreSQL, if the database is deleted by mistake, whether it can be restored depends on whether there is a log policy for backup in advance and database configuration. Here are the possible recovery methods:

1. If there is a backup

Restore via backup and restore via pg_dump or pg_basebackup

  • Backup file type:
    • If you havepg_dumpGenerated SQL files (such as), you can directly restore it through the following command:
      psql -U username -d new_database_name -f 
      
    • If you have a binary backup (such as usingpg_basebackup), you need to restore the entire database directory.
      pg_basebackup -D /path/to/data_directory -F tar -X fetch -U backup_user
      

2. If Archive Log (WAL Log) is enabled

Enable WAL log recovery

  • Make sure that the PostgreSQL configuration file has WAL log archive enabled:examineWhether the following configuration is enabled:
    archive_mode = on
    archive_command = 'cp %p /path/to/archive/%f'
    
  • Restore to a certain point in time:usepg_basebackupand WAL logs perform point-in-time recovery (PITR, Point-in-Time Recovery):
    • Stop database service:
      systemctl stop postgresql
      
    • Restore backup files to the data directory.
    • Configurationor PostgreSQL 14+, specify the target recovery time:
      restore_command = 'cp /path/to/archive/%f %p'
      recovery_target_time = 'YYYY-MM-DD HH:MI:SS'
      
    • Start PostgreSQL:
      systemctl start postgresql
      

3. If there is no backup, log archive is not enabled

Check the operating system level snapshots

If there are regular snapshot backups at the server level (such as using cloud services or stored file system snapshots), the data directory can be restored through the snapshot. For example:

  • Recover RDS snapshots on AWS.
  • Restore local files using a snapshot of LVM or other tools.

4. PostgreSQL cannot directly restore deleted database

PostgreSQL does not provide the built-in Recycle Bin feature. If there is no backup and log archive is not enabled, the database that was deleted accidentallyCan't recover directly. Therefore, the following measures are recommended:

  • Regular backup:
    • Configure daily/weeklypg_dumpAutomatic backup.
    • usepg_basebackupGenerate a binary backup.
  • Enable archive logs:Enable WAL log archive in production to perform point-in-time recovery when data is corrupted or lost.
  • Prevent misoperation:
    • Use read-only roles to perform queries in production environments.
    • Set an additional approval mechanism for the delete command.

If your situation is that the database is lost due to no backup, you can contact a professional database recovery team to try data recovery, but the success rate is low and the cost is higher.

Summarize

This is the end of this article about what to do if PostgreSQL deletes the database by mistake. For more related PostgreSQL deletes the database by mistake, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!