In an attempt to tidy up my backend Wordpress databases, I thought I’d change the Wordpress table prefix and the move all my Wordpress tables into a single database.

Before actually moving the tables, I took the plunge of just renaming them by changing the table prefix to uniquely identify which Wordpress installation they belong to.

I did this as follows…

  1. Dump the table names and prepend the new prefix:
    # for x in `/usr/local/bin/mysqlshow --user=${DB_USER} \
    --password=${DB_PASSWD} \
    --host=${DB_HOST} \
    ${DB_NAME} | grep ${OLD_WP_PREFIX} | grep -v ^Database | tr -d " | "`
    do
    echo "RENAME TABLE ${DB_NAME}.$x TO ${DB_NAME}.${NEW_WP_PREFIX}_$x;"
    done

    You’ll see all I did was to prepend a new prefix to the old one as all I’m doing is identifying which Wordpress installation the tables relate to.

  2. Copy the output from the above into phpMySQL and ran it. This output can also be directed back into mysql on the CLI. All my tables took on their new names successfully.
  3. Modify my wp-config.php to reflect the correct $table_prefix

Voila!!! At face value this appears to work and your blog will appear as it did before. That is until you attempt to login. You’ll be presented with this lovely message:

You do not have sufficient permissions to access this page.

Even when attempting to login as admin. The cause? Simple, some of the Wordpress options are actually saved using the Wordpress prefix. You will now need to open up your favourite MySQL admin tool and modify the following:

In table ${NEW_WP_PREFIX}_options:

  • Option ${OLD_WP_PREFIX}_user_roles

In table ${NEW_WP_PREFIX}_usermeta:

  • Option ${OLD_WP_PREFIX}_capabilities
  • Option ${OLD_WP_PREFIX}_user_level
  • Option ${OLD_WP_PREFIX}_autosave_draft_ids

The last will only exist if you have saved a draft whilst editing. For each of these, you need to change the ${OLD_WP_PREFIX} part to reflect your new prefix, eg: wp_user_roles becomes new_wp_user_roles.

You may need to change other options from plugins that store the prefix as part of the variable name.

If you’re using the Bad Behaviour plugin, you’ll also need to disable it, remove it’s option entries from the _options table and re-enable the plugin.


12 Responses to “HOWTO: Change Wordpress Table Prefix”  

  1. 1 SmellyGeekBoy

    Hi, I just moved a Wordpress install over to a different server and had to change my table prefixes. This is exactly what I needed to let me log in again after it broke spectacularly - thanks a lot!

  2. 2 Max

    Thank you!

    You would only wish that in a later version this is changed, I see no reason for storing the prefix as field names in the db.

  3. 3 Brandon

    Thanks! That was very helpful.

    I got that exact error message today after renaming my table_prefix, “You do not have sufficient permissions to access this page.”, and your post made it very simple for me to get things working quickly.

    Brandon Zehm
    TSheets.com

  4. 4 Ruthvick

    Beautiful!! Thanks for the research.

  5. 5 Eric

    Great post, very helpful. FYI, I found that phpMyAdmin on my host’s control panel could not change the ${OLD_WP_PREFIX}_user_roles item. I ended up using CocoaMySQL-SBG instead. Also note, there will be more than one occurrence of the items in ${NEW_WP_PREFIX}_usermeta if you have more than one account registered.

  1. 1 10 Ways to Secure your Wordpress Install | Hackosis
  2. 2 ???? » Blog Archive » 10 Ways to Secure your Wordpress Install
  3. 3 Drobny problem przy zmianie prefixu tabel | Migol's Blog
  4. 4 Robert Penz Blog » Improving the security of a installed Wordpress
  5. 5 Wordpress Tablo Adlar?n? De?i?tirmek | Azizce
  6. 6 panicprone.de » Change Wordpress $table_prefix
  7. 7 Bullet-Proof Your Wordpress Installation | New Webmasters

Speak Your Mind    cocomment icon