How to Repair a Corrupted Database

Plex stores the metadata and other information for your media in an SQLite database that is located in the lex data directory on your server. There is a possibility that the Plex database could become corrupted, albeit, very rarely. One way of corrupting the database could happen is if your server was turned off as Plex was writing to the database.

If you do find that the database has been corrupted, this article will explain methods you can use to return the database to a functioning state.

How to Repair a Corrupted Plex Database

The Plex SQLite tool

As mentioned earlier, the Plex database is an SQLite database. The database includes some customizations, so the Plex team has included their command line interpreter to work with the database: Plex SQLite.

The Plex SQLite tool is located in the same directory as the main Plex Media Server executable. The following table shows the location of the database for various systems:

Location of the Plex SQLite Tool
SystemLocation
Windows (32-bit)C:\Program Files (x86)\Plex\Plex Media Server\Plex SQLite.exe
Windows (64-bit)C:\Program Files\Plex\Plex Media Server\Plex SQLite.exe
macOS/Applications/Plex Media Server.app/Contents/MacOS/Plex SQLite
Linux (Desktop)/usr/lib/plexmediaserver/Plex SQLite
QNAP/share/CACHEDEV1_DATA/.qpkg/PlexMediaServer/Plex SQLite
Synology (DSM 6)/var/packages/Plex Media Server/target/Plex SQLite
Synology (DSM 7)/var/packages/PlexMediaServer/target/Plex SQLite

Before using the Plex SQLite tool with the Plex database, it is recommended that you first back up the Plex database. Creating a backup is always recommended, it should be done automatically on a regular schedule.

Also, you should stop Plex Media Server to prevent more corruption and to ensure there is no issue with Plex SQLite from accessing and working with the database.

Once both of the above things are done, you will need to know the location of the Plex database and then use the following command:

"Plex SQLite" [database path]/com.plexapp.plugins.library.db

The command is case-sensitive. For example, running the command using "plex sqlite" will not work.

After running that command, you will see a sqlite> shell prompt appear.

If you don't specify the full path to the database, or the path is incorrect, you will still get the SQLite prompt, however, if you try to execute any commands (discussed in the next section), you will get the following message:

Error: unable to open database "com.plexapp.plugins.library.db": unable to open database file

Ensure the path to the database is correct and try to run the Plex SQLite command again.

Database Diagnostic Commands

From the sqlite> command prompt you can run the diagnostic commands outlined below to try and repair a corrupt Plex database.

Ensure you always add a semi-colon at the end of each diagnostic command. If you exclude the semi-colon, the command will not run.

Check for corruption

The following command can be used to check for database corruption:

PRAGMA integrity_check;

If the database structure is valid, you will see a result of ok. While this command indicates there is no issue, there could be other issues that weren't detected. You can try additional commands to see if there is another underlying issue.

If an error is returned, you can try to repair it using the additional commands outlined below. Once those commands run, you can re-run this command to see if the result has changed.

Rebuild the database structure

Some issues could be related to the database structure, including some issues that the integrity_check command couldn't detect. You can rebuild the database structure using the following command:

VACUUM;

Rebuilding the indexes

If there are issues related to the database indexes, such as those that appear after running the integrity_check command, you can rebuild the indexes using the following command:

REINDEX;

Low-level database recovery

If the above commands did not work, you can have Plex attempt to salvage as much information as possible, and then recreate the database from the salvaged data.

The first step is to generate a recovery file. The below command will create a recovery file called db-recovery.sqlite

.output db-recovery.sqlite
.recover

Exit the SQLite command shell using .quit and then move the original database file to another location. If there is an issue with the recovery, then the original will act as a backup.

Restart the SQLite command shell by running "Plex SQLite", and at the sqlite> prompt run the following command to regenerate the database:

.read db-recovery.sqlite

Once the new database, with all valid data, has been recreated make sure the user that owns the database matches the following for the respective systems:

Plex Database User Ownership
SystemOwnership
Linux (Desktop)plex:plex
QNAPadmin:administrators
Synology (DSM 6)plex:users
Synology (DSM 7)PlexMediaServer:PlexMediaServer

Restore a database

While the above commands can be used to fix a corrupted database, it may be easier to restore a known good version of the database.

To do that, you would just need to stop Plex Media Server, and copy the database from your backup to the current database location - overwriting the existing database. Once that is done, restart Plex Media Server.

Subscribe
Display