Rebuilding corrupted RPM database

Posted on in Categories Howto, RedHat/Fedora Linux, Troubleshooting last updated June 3, 2006

One of our client reported that he is getting an error and RPM database is corrupted. He is using Red Hat Linux

Sometime it is possible to fix RPM database errors. I am surprised that many admins do not make back up of RPM DB (/var/lib/rpm).

Anyways if you ever messed up with RPM database, here is a quick guide to fix it (you must have rpmdb tools installed):

Take system to single user mode to avoid further damage and to make backup/restore process easy:

# init 1

Method # 1

Remove /var/lib/rpm/__db* files to avoid stale locks:
# cd /var/lib
# rm __db*

Rebuild RPM database:
# rpm --rebuilddb
# rpmdb_verify Packages

Method # 2

If you are still getting errors, then try your luck with following commands:

# mv Packages Packages-BAKUP
# db_dump Packages-BAKUP | db_load Packages
# rpm -qa
# rpm --rebuilddb

Further reading

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

16 comment

  1. I found the same post/solution before in nixcraft and it is really a great solution but my question is how to recreate /var/lib/Packages if the original got “DELETED”.

  2. Thanks for a great site. It’s a fantastic resource for the sysadmin community.
    The code in Method 1 should be “cd /var/lib/rpm” rather than “cd /var/lib”. Please fix? 🙂

Leave a Comment