How to Change WordPress Database Prefix [in Simple and Easy Steps]

WordPress-database-featured

Whenever you install WordPress with its default configuration, the database prefix “wp_” will be used for all the tables in your WordPress database. Even though it doesn’t look like much, your site might be at risk with this default table prefix. This is because most hackers and spammers target this default WordPress database prefix for automated or manual SQL injection attacks. Considering the security risk of having the default database prefix, changing WordPress database prefix is one of the most commonly recommended WordPress security settings.

In case you are wondering, here is how to change WordPress database prefix in two different ways. i.e, before installing WordPress, and after installing WordPress. Follow the method you are most comfortable with to change WordPress database prefix.

Note: since we are going to mess with the WordPress database itself, make sure that you have a good backup of WordPress database. This backup will help you restore the database, in case anything happens.

Change WordPress Database Prefix – Before Installing WordPress

If you want to change WordPress table prefix before installing, then the process is pretty simple.

1. Just open the wp-config.php file, scroll down and find the value $table_prefix. As you can see, the default database prefix is set to wp_.

Change WordPress Database Prefix - Default Table Prefix
Change WordPress Database Prefix – Default Table Prefix

2. Simply change the default table prefix to something like wp_bs645t_. Once you are done with the changes, save the file and proceed to the WordPress installation.

Change WordPress Database Prefix - Modified Table Prefix
Change WordPress Database Prefix – Modified Table Prefix

Change WordPress Database Prefix – After Installing WordPress

Even after installing WordPress, you can easily change WordPress database prefix in three simple steps.

  1. Change table prefix in WordPress config file
  2. Rename database tables
  3. Update Options and Usermeta tables

Just follow the below steps below as is and you should be good. Again, backup WordPress database before proceeding.

1. To start off, fire up your FTP client and open the wp-config.php file located in the root directory.

2. Once opened, scroll down and find the value $table_prefix. Now, replace the default wp_ prefix with your new database prefix. For instance, I’m using wp_bs645t_ as my new table prefix. Save the file and reupload it.

Change WordPress Database Prefix - Modified Table Prefix
Change WordPress Database Prefix – Modified Table Prefix

3. Now, login to cPanel (or whatever control panel you are using), find “phpMyAdmin” and click on it to open the application.

Change WordPress Database Prefix - Open phpMyAdmin in cPanel
Change WordPress Database Prefix – Open phpMyAdmin in cPanel

4. The above action will open the phpMyAdmin application. Here, click on the “Databases” link in the top navigation bar. Now, select the database that you want to change WordPress database prefix for.

Change WordPress Database Prefix - Open Database
Change WordPress Database Prefix – Open Database

5. Once the target database has been opened, click on the link “SQL” in the upper navigation bar.

Change WordPress Database Prefix - Select SQL Option
Change WordPress Database Prefix – Select SQL Option

6. This action will take you to the SQL query window. Here, you can run or execute SQL queries to change WordPress database prefix.

Change WordPress Database Prefix - Blank SQL Query Window
Change WordPress Database Prefix – Blank SQL Query Window

7. To change the table prefix, copy the below lines or quires, paste it in the SQL query field and click on the “Go” button to execute all the quires at once.

Note: don’t forget to replace wp_bs645t_ with your new table prefix.
RENAME table wp_commentmeta TO wp_bs645t_commentmeta; 
RENAME table wp_comments TO wp_bs645t_comments; 
RENAME table wp_links TO wp_bs645t_links; 
RENAME table wp_options TO wp_bs645t_options; 
RENAME table wp_postmeta TO wp_bs645t_postmeta; 
RENAME table wp_posts TO wp_bs645t_posts; 
RENAME table wp_terms TO wp_bs645t_terms; 
RENAME table wp_term_relationships TO wp_bs645t_term_relationships; 
RENAME table wp_term_taxonomy TO wp_bs645t_term_taxonomy; 
RENAME table wp_usermeta TO wp_bs645t_usermeta; 
RENAME table wp_users TO wp_bs645t_users;
Change WordPress Database Prefix - Execute SQL Quires
Change WordPress Database Prefix – Execute SQL Quires

8. The above action will change the table prefix of all the default WordPress tables. If you have any other additional tables (which can be seen on the left sidebar of the phpMyAdmin page), then use the above technique to change those table prefixes.

9. Now, you also need to update the “wp_options” and “wp_usermeta” tables for any old table prefixes.

10. To do that simply copy the below query lines and execute them. While executing, make sure that you replace:

  •  {%NEW_TABLE_PREFIX%} with the new prefix (example: wp_bs645t_)
  •  {%OLD_TABLE_PREFIX%} with the old table prefix (example: wp_)
UPDATE `{%NEW_TABLE_PREFIX%}usermeta` SET `meta_key` = replace(`meta_key`, '{%OLD_TABLE_PREFIX%}', '{%NEW_TABLE_PREFIX%}');
UPDATE `{%NEW_TABLE_PREFIX%}options` SET `option_name` = replace(`option_name`, '{%OLD_TABLE_PREFIX%}', '{%NEW_TABLE_PREFIX%}');

Once you’ve replaced the required values, this is how it looks like.

UPDATE `wp_bs645t_usermeta` SET `meta_key` = replace(`meta_key`, 'wp_', 'wp_bs645t_');
UPDATE `wp_bs645t_options` SET `option_name` = replace(`option_name`, 'wp_', 'wp_bs645t_');
Change WordPress Database Prefix - Update WordPress Tables
Change WordPress Database Prefix – Update WordPress Tables

11. As soon as you execute the above queries, you will receive the confirmation message showing the number of updated rows.

Change WordPress Database Prefix - Tables Updated
Change WordPress Database Prefix – Tables Updated

That’s all there is to do.

Conclusion

It is that simple to change WordPress database prefix. If you want to verify, navigate to the Structure tab and you will see the modified table prefix. Also, backup your WordPress site as soon as you change the WordPress database prefix.

Also, backup your WordPress site as soon as you change the WordPress database prefix.  This enables you to restore your site in future without needing to change the table prefix again.

Hopefully that helps and do comment below if you face any problems or just to share your thoughts and experiences about using the above methods to change WordPress table prefix.

2 Comments

  1. I want to do this, but I have one concern:

    if I change the database prefix, how will that affect WP auto updates? Will WP know what file to update if the prefix is different? Will I then have to manually update WP from then on, and what about these new WP security updates. Won’t changing the prefix mess that up as well?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.