Home Blog WordPress Security Change the WordPress Database Prefix and Improve Security

Changing the WP Database Prefix and Improving Security*

Change the WordPress Database Prefix and Improve Security

There are different procedures that you can use to rename the WordPress database prefix. It depends on whether you have already installed WordPress or not. If you have not installed WordPress yet, you can simply specify a different database table prefix from the WordPress installation wizard or pre-define it in the wp-config.php file before running the installation. If you already installed WordPress, follow the below step-by-step procedure to improve your WordPress security.

Change WordPress Database Table Prefixes

Before you start

We recommend you to backup your WordPress database before you perform anything suggested in this tutorial. We also recommend you to redirect your visitors to a temporary maintenance page. If you are not comfortable using phpmyadmin and MySQL, hire professionals to do the job for you.

Change Table Prefix in wp-config.php file

Open your wp-config.php file, which can be found in the WordPress root directory, and change the $table_prefix variable value from wp_ to something else, like ra66be81_.

WP White Security Tip: Use only letters, numbers and underscores for the table prefix and try not to use dictionary words. Be creative, you do not need to remember such value.

After the change, the line in wp-config.php should look like:

$table_prefix=’ra66be81_’;

Change all WordPress Database Tables Name

To change the database tables name you need access to your WordPress MySQL database. You can access the WordPress database through phpMyAdmin which is accessible from your hosting provider’s CPanel. Below is a screenshot of the WordPress database tables in phpMyAdmin.

WordPress database tables from phpMyAdmin

The WordPress MySQL database contains 11 tables by default and as you can notice from the above screenshot, they all start with the wp_ prefix. You might have more tables if you installed a plugin that adds an own table to the database. You can rename the tables manually, one by one. Alternatively use the below script to automate the renaming of the tables. To use the below SQL query, click on the SQL tab (highlighted in the above screenshot), copy the content, paste it and change the prefix to the prefix you’ve chosen.

RENAME table `wp_commentmeta` TO `ra66be81_commentmeta`;
RENAME table `wp_comments` TO `ra66be81_comments`;
RENAME table `wp_links` TO `ra66be81_links`;
RENAME table `wp_options` TO `ra66be81_options`;
RENAME table `wp_postmeta` TO `ra66be81_postmeta`;
RENAME table `wp_posts` TO `ra66be81_posts`;
RENAME table `wp_terms` TO `ra66be81_terms`;
RENAME table `wp_term_relationships` TO `ra66be81_term_relationships`;
RENAME table `wp_term_taxonomy` TO `ra66be81_term_taxonomy`;
RENAME table `wp_usermeta` TO `ra66be81_usermeta`;
RENAME table `wp_users` TO `ra66be81_users`;

If you have other tables in your WordPress database using the same prefix, such as tables created by plugins, you can add them to this query as well. For your reference the syntax of the above SQL query is the following:

RENAME table `[old_table_name]` TO `[new_table_name]`;

The Options table

WordPress stores all the global options in the Options table. In this table there are some entries which also need to have their prefix changed. To retrieve a list of all the entries that are using the wp_ prefix and needs changing, use the following SQL query:

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

The query might return a good number of entries that need to be changed. It is important to change them all to avoid having problems.

The UserMeta table

The WordPress UserMeta table contains all the information about registered users, such as personalised settings. In this table there are also a number of entries that need to have their prefix changed. To retrieve a list of all entries that are using the wp_  prefix, use the following SQL query:

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

The number of entries returned when running this query might vary. It depends of how many WordPress plugins you have installed etc.

Backup and Test the Change

That should be it. Before you do any further changes, make a backup of WordPress again and proceed with testing the changes. Visit at least one page, one blog post and try to login to the WordPress dashboard (wp-admin section) to confirm that everything is working fine.


5 thoughts on “Change the WordPress Database Prefix and Improve Security

  1. Thanks! I figured out the UserMeta table but hadn’t spotted the Options entries.

    WP all working again 😉

  2. Very nice, helpful article. Thank you. But, I’m stuck on your last 2 steps (The Options table and The UserMeta table). You say “To retrieve a list of all the entries…” but what are the steps after that? Do the rename thing on those? For example, I ran the Options Table query and get 2 results: wp_user_roles and WPLANG ???

  3. Hi
    I changed DB table prefix but my site’s login URL not working. It showing me “This has been disabled”.
    Please help with my problem. please

    1. Hello Ruperth, it seems that the error is not related to the DB, but to be on the safe side I would recommend you to rename them back to what they were first to restore functionality.


Leave a Reply

Your email address will not be published. Required fields are marked *

Stay in the loop

Subscribe to the Melapress newsletter and receive curated WordPress management and security tips and content.

Newsletter icon

It’s free and you can unsubscribe whenever you want. Check our blog for a taste.

Envelope icon
newsletter-pop-up