Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Change the WordPress Database Prefix to Improve Security

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Change the WordPress Database Prefix to Improve Security

WordPress Database is like a brain for your entire WordPress site because every single information is stored in there thus making it hacker’s favorite target. Spammers and hackers run automated codes for SQL injections. Well, unfortunately many people forget to change the database prefix while they install WordPress. This makes it easier for hackers to plan a mass attack by targeting the default prefix wp_. The smartest way you can protect your database is by changing the database prefix which is really easy to do on a site that you are setting up. But it takes a few steps to change the WordPress database prefix properly for your established site without completely messing it up.

Video Tutorial

If you don’t like the video or need more instructions, then continue reading.

Preparation

We recommend that you backup your WordPress Database before you perform anything suggested in this tutorial. It is important to keep daily backups of your site, we recommend BackupBuddy plugin for doing that. Next thing we recommend is that you redirect your visitors to a temporary maintenance page.

Change Table Prefix in wp-config.php

Open your wp-config.php file which is located in your WordPress root directory. Change the table prefix line from wp_ to something else like this wp_a123456_

So the line would look like this:

$table_prefix  = 'wp_a123456_';

Note: You can only change it to numbers, letters, and underscores.

Change all Database Tables Name

You need to access your database (most likely through phpMyAdmin), and then change the table names to the one we specified in wp-config.php file. If you are using the cPanel WordPress hosting, then you can find the phpMyAdmin link in your cPanel. Look at the image below:

phpMyAdmin

There are a total of 11 default WordPress tables, so changing them manually would be pain.

SQL Query

That’s why to make things faster, we have a SQL query that you can use.

RENAME table `wp_commentmeta` TO `wp_a123456_commentmeta`;
RENAME table `wp_comments` TO `wp_a123456_comments`;
RENAME table `wp_links` TO `wp_a123456_links`;
RENAME table `wp_options` TO `wp_a123456_options`;
RENAME table `wp_postmeta` TO `wp_a123456_postmeta`;
RENAME table `wp_posts` TO `wp_a123456_posts`;
RENAME table `wp_terms` TO `wp_a123456_terms`;
RENAME table `wp_termmeta` TO `wp_a123456_termmeta`;
RENAME table `wp_term_relationships` TO `wp_a123456_term_relationships`;
RENAME table `wp_term_taxonomy` TO `wp_a123456_term_taxonomy`;
RENAME table `wp_usermeta` TO `wp_a123456_usermeta`;
RENAME table `wp_users` TO `wp_a123456_users`;

You may have to add lines for other plugins that may add their own tables in the WordPress database. The idea is that you change all tables prefix to the one that you want.

The Options Table

We need to search the options table for any other fields that is using wp_ as a prefix, so we can replace them. To ease up the process, use this query:

SELECT * FROM `wp_a123456_options` WHERE `option_name` LIKE '%wp_%'

This will return a lot of results, and you need to go one by one to change these lines.

UserMeta Table

Next, we need to search the usermeta for all fields that is using wp_ as a prefix, so we can replace it. Use this SQL query for that:

SELECT * FROM `wp_a123456_usermeta` WHERE `meta_key` LIKE '%wp_%'

Number of entries may vary on how many plugins you are using and such. Just change everything that has wp_ to the new prefix.

Backup and Done

You are now ready to test the site. If you followed the above steps, then everything should be working fine. Now, you should make a new backup of your database just to be on the safe side.


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Chad Mowery says:

    Nice article. I think the query on the Options and UserMeta table will throw off less savvy individuals. Not sure how but it would help if you could provide more detail on those steps.

    I followed the guide and have successfully changed my DB table prefix!

  2. blade says:

    This does not add really any kind of security.
    If I can inject SQL, I can query against information_schema.tables and get info about tables, whatever fancy prefix you put in front of names ;)

    • thomas says:

      very true. a good htaaccess will block sql injections too. on top of that, my wordpress install only gives full database access to very select users by assigning those credentials based on certain things gathered long before a db connection. everybody else gets the very very basic access.

      i also stopped most hacking attempts cold by hiding the wpzlogin.php and further password protecting access to wp admin in cpanel. it takes a login just to get to the wp login page.

  3. John says:

    @Shivi

    This is all you need to do to change table.prefix ? even when live?

  4. shivi says:

    Hi , Nice article ! Thought of simplifying replacing table prefix.

    In Phpmyadmin once u select the database, you can see the list of tables.
    1. Enable the checkbox check all

    2. select Replace table prefix in the dropdown ‘with selected’

    3. Enter the existing prefix in from option: ‘wp_’ as mentioned in the article.

    4. Enter the new prefix in to option: ‘wp_a123456_’ as mentioned in the article.

    5. Click submit

    This helps to change table prefix for all tables including that of the plugin.

    You can check these articles if you need support on dehack your site

    • john says:

      So…. @SHIVI

      All I need to do, to change the table.prefix is go into my config.file change the prefix and then proceed to your steps 1-5 and the table.prefix will be changed everywhere (plugins etc..)
      And that’s it?

  5. Fahim says:

    Should i run Sql for every table? i mean i have 15 table. or only change two tables you described will fix the problem.

    • WPBeginner Support says:

      All tables. You would want to change table prefix from all your existing WordPress tables.

  6. Alph says:

    Hi guys,

    Thanks for these instructions. They helped me resolve my uppercase issues.

    As a beginner, some guidance as to the how to change the table names manually would have been appreciated. I would have felt more comfortable doing it that way.

    Also, for a beginner like me, some instruction on how to make the changes in the Options and Usermeta tables would been helpful.

    Thanks again. Could not have done it without you!

  7. Chris A says:

    Thanks for the instruction. I needed to remove an UPPER case letter from a prefix and this showed that exact records I needed to touch!

    TIP: with phpMyAdmin you can check all records at the bottom and select ‘replace table prefix’ this will bring up a standard replace box with ‘from’ and ‘to’ values.
    It will also save a lot of typing in step one – especially if it’s a large database.

  8. Prabhakar says:

    You made it !

    Thanks

  9. Igor says:

    Hi guys,

    when I made these changes it don’t give me to sign in as current user but it tells me to install wordpress again…

    Igor

    • WPBeginner Support says:

      This usually happens when the database you connected to is empty. Please recheck your database name.

  10. Alan P says:

    wp_termmeta is missing from the list of tables to rename.

  11. Rick says:

    My SQl DB have no table prefix so got error sometimes on wordpress Wpconfig.php without $table_prefix = ”;.
    how to add table prefix,
    when i try to add in config file and rename all table with same prefix
    and login in to wp got error “you don’t have permission to access”

  12. Sameer Panda says:

    Worked as a charm, thanks for sharing. :)

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.