How to Change Database Table Prefix in WordPress

by Ahsan Parwez  June 21, 2017

Many people feel that WordPress is just bad when it comes to security. Over the years, WordPress has grown in popularity with millions of websites using it. Therefore, it has become a favorite target for most of the attackers and hackers who develop malicious and automated scripts to nuke websites with bots in order to hack a website, publish spam comments, or do SQL injection.

Change WordPress Database Table Prefix Banner

Good thing is that WordPress is really flexible and you can tighten security by making small adjustments. Though all security measures are not foolproof, one single security step can stop the basic attacks. By changing default WordPress database prefix, you can stop novice hackers from running SQL injection on your website.

When you install WordPress, it creates tables in the database with the wp_ prefix.

WordPress Database Tables

Before making any changes to your website, make sure you’ve created a backup. Use plugins like BackupBuddy or UpdraftPlus to schedule backups. If your site is hosted on Cloudways, then most probably you already have a backup of your entire site. If not you can take one manually by pressing the “Take Server Backup Now” button.

Cloudways Backups

I personally make changes in the database manually, so I know exactly what changes I have made. For beginners, it is better to use a plugin to make changes to your database. But, I will cover both the methods to change WordPress database prefix.

Changing WordPress Database Prefix Manually

I am more comfortable with making such changes manually. There are plugins available that do the exact same thing. But the thing is I don’t want another plugin on my site and you never know when a vulnerability in the plugin pops up and you lose your website. Here are the few steps involved in changing the database prefix manually.

  1. Editing wp-config.php
  2. Renaming the Database Tables Using SQL Query
  3. Renaming the wp_ fields in Database Table

First thing first, you need to decide what prefix you want to use. For the purpose of this tutorial, we are going to change database prefix from wp_ to cw_.

Step 1: Editing wp-config.php

Access the root directory of your WordPress installation through an FTP software like FileZilla, find and edit the wp-config.php file using any code editor of your choice. My favorite is Sublime Text Editor.

Edit wp-config file

In the wp-config.php file, find the line that reads $table_prefix = ‘wp_’ and replace the wp_ with cw_ (or whatever prefix you prefer).

Edit table prefix in wp-config.php

Save the file and upload it to the root of your WordPress installation.

Step 2: Renaming the Database Tables Using SQL Query

By default, WordPress creates tables with prefix wp_ in MySQL database that we connect to it. There may be more tables with wp_ prefix if you have a plugin that requires its own table.

To change the database prefix, access your MySQL database through MySQL Manager that your host offers.

Application Details Cloudways

The fastest way to change the database names is to change by running SQL queries. Run the SQL command from your MySQL Manager and use the following commands to make the change

Note: Make sure, you write above line for every table present in your database.

Change WordPress Table Prefix

You can use any name in place of cw_ but just keep in mind, only numbers, alphabets, and underscores are allowed.

Step 3: Renaming the wp_ fields in Database Table

Apart from tables, there are two fields (cw_options and cw_usermeta in our case) by default that has field name beginning with wp_.

By using the following queries, we can change the prefix of those fields containing a wp_ prefix.

Changing Database Prefix Through Plugin

If you don’t want to dive into database stuff and make changes manually, then you can use “Change Table Prefix plugin to make the changes in just 1 step.

Conclusion

In security perspective, this is not a foolproof method but it will allow you to stop basic attacks and automated scripts that target databases with a wp_ prefix. There is always a vulnerability and flaw waiting to be discovered but you can tighten them by following 10 WordPress security issues & how to fix them.

Make Your WordPress Website 100% Faster.

Host it now on Cloudways WordPress Hosting Platform.

About Ahsan Parwez

Ahsan is the Community Team Manager at Cloudways – A Managed Cloud Hosting Platform. He loves to solve problems and help Cloudways’ clients in any aspect he can. In his free time, you can find him playing RTS PC games.

Stay Connected:

You Might Also Like...

  • Peter Cralen

    Personally I found plugins for one time use, as one you mentioned as easy and good solution. It will do simply that job and then delete that plugin, so it will not affect site anymore.
    Playing with database – also for advantage users is sometimes headache, bc. just small mistake, misspelling one letter can broke whole website and its sometimes hard to find what is wrong. Plugins with thousands of downloads are usually stable enough to use them. Sure its not smart to keep that kind of plugin after job is done.
    Anyways simple and good tutorial, thanks Ahsan. (sometimes just small job can help a lot and this is one of that job, which probably I forget if did not read about it somewhere )

    • Haha that is just rude to use the plugin and then uninstalling it 😀 (kidding)

      You are right, playing with large databases can be very risky for newbies. That is why always keep backups of your site before making any changes to live site 🙂

  • Hi Ahsan Parwez,

    I have recently wrote about http://www.seomediaworld.com/change-wordpress-database-prefix-wordpress-security/ that was automatic plugin to change database prefix.This is manual guide, that is really helpful for those people who usually want to use less plugins on their blog.

    Thanks for the great article.

    • Most welcome. Thanks for sharing your tutorial with us 🙂