Skip to content

How To Change WordPress Database Prefix

WordPress uses a MySQL database to collect and store all the information. Your database is the brain of your WordPress site because it is the place where all your settings and posts are being stored.

Your WordPress database is also a target of all the bad hackers. The best way to protect your database is by changing the default WordPress database prefix.

Change Table Prefix Before Installation

You can’t perform this trick on your current WordPress installs, but you can do it with all your future WordPress installs or right now if you’re preparing to install a fresh WordPress copy.

Before installing WordPress, while configuring the wp-config.php file with all your database credentials, change the following part of the code:

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each a unique
 * prefix. Only numbers, letters, and underscores please!
 */
$table_prefix  = 'wp_';

Replace the wp_ prefix from above code to a random prefix, such as wp_kjd3qrsf_. You can generate random prefix for your database from random.org.

Change Table Prefix After Installation

You can also change your WordPress’ database prefix after installing your copy of WordPress. There are two ways of changing database prefix after installing WordPress, which are following:

Don’t forget to create a backup of your WordPress’ MySQL database!

With A Plugin

This is the best and the easiest way. If you’re non-technical guy then this way is just for you, also if you don’t like logging into your MySQL database account. You can use Change Database Prefix plugin for this task.

Just visit Dashboard > Settings > Change DB Prefix, and put down your new table prefix, and hit the Save button.

Change WordPress Database

Manually

Nerd, aren’t you? This part is not going to be hard for some tech people.

First, change the wp_ in the wp-config.php file with your custom table prefix. Login in to your MySQL database, click on the SQL button, which is located just right to the Structure button.

Just run the following SQL query with your own custom table prefix:

RENAME table `wp_commentmeta` TO `wp_bhcRBn58_commentmeta`;
RENAME table `wp_comments` TO `wp_bhcRBn58_comments`;
RENAME table `wp_links` TO `wp_bhcRBn58_links`;
RENAME table `wp_options` TO `wp_bhcRBn58_options`;
RENAME table `wp_postmeta` TO `wp_bhcRBn58_postmeta`;
RENAME table `wp_posts` TO `wp_bhcRBn58_posts`;
RENAME table `wp_terms` TO `wp_bhcRBn58_terms`;
RENAME table `wp_term_relationships` TO `wp_bhcRBn58_term_relationships`;
RENAME table `wp_term_taxonomy` TO `wp_bhcRBn58_term_taxonomy`;
RENAME table `wp_usermeta` TO `wp_bhcRBn58_usermeta`;
RENAME table `wp_users` TO `wp_bhcRBn58_users`;

Now we need to change the options and usermeta tables to make everything work.

First enter the following SQL query:

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

Rename any option that begins with wp_ to our new prefix. We will do same with the usermeta table. Run following SQL command:

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

Rename all the tables here as well, just like the previous step.

Don’t forget to visit your WordPress’ back and front-end to check if it’s all working. If this broke your WordPress, then you can restore your MySQL database from the backup that we created earlier.

2 thoughts on “How To Change WordPress Database Prefix”

Leave a Reply

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