Changing the WordPress database table prefix is a simple but effective way to enhance your site’s security. By default, WordPress assigns the table prefix wp_
, making it a common target for hackers attempting SQL injection attacks. Modifying this prefix can help protect your database from such threats.
In this guide, we’ll walk you through three different methods to safely change your WordPress database table prefix.
Every action in a WordPress site is interlinked with the database:
![How Databases work in WordPress and what](https://wp-staging.com/wp-content/uploads/2020/08/database800x400-1.png)
Why Change the WordPress Table Prefix?
Changing the default wp_
table prefix helps:
- Reduce the risk of SQL injection attacks
- Make it harder for attackers to guess table names
- Add an extra layer of security to your WordPress site
Before You Start: Backup Your Database
Changing the WordPress database prefix can be a critical task, and it’s not always easy. You must ensure you don’t break your website by changing the WordPress table prefix.
Before changing the database prefix, we recommend doing it on a staging site. So you can check thoroughly if the update works and can safely push your staging site to the production site without any risk of data loss after changing the DB prefix.A staging site is a copy of your live site, so you can do different changes/testing and make those changes live with a single click.
You can create a free staging site with WP STAGING.
While installing WordPress, people’s most common mistakes are forgetting to change their WordPress Database prefix and allowing attackers to run automated SQL injections. So, taking preventive measures is crucial to protect your website from being hacked.
How to Change the WordPress Database Prefix
Follow this step-by-step guide to rename the WordPress database prefix safely.
There are three (3) options to change and rename the database prefix of your WordPress website:
- Use a plugin to change the database table prefix.
- Rename WordPress Database Prefix using Database Query with Adminer.
- Rename WordPress Database Prefix using Database Query with PhpMyAdmin.
Contents
Method 1: Use a Plugin to Change the Database Table Prefix
We can change the database prefix using a plugin. Here are a few simple steps to change the database prefix on WordPress.
- Go to the WordPress dashboard.
- Go to the ‘Plugins’ tab and click ‘Add New.’
- Search Brozzme DB Prefix & Tools Add-ons in the WordPress plugin repository.
- Click the install now button to install Brozzme DB Prefix & Tools Add-on on your site.
- Click the activate button to activate this plugin.
![updatewp database prefix via plugin](https://wp-staging.com/wp-content/uploads/2020/08/updatewp-database-prefix-via-plugin.jpg)
The plugin is activated, and we can start working on changing the WordPress database prefix right away.
6. Go to tools and click DB Prefix. See the current prefix of your database, and change the prefix to the new one.
![wordpress database prefix](https://wp-staging.com/wp-content/uploads/2020/08/wordpress-database-prefix.jpg)
If your wp-config.php file is not writable, you will receive an error notice because this plugin can’t change the database prefix in the wp-config.php file.
You’re done by clicking the “Change DB Prefix” button.
Method 2: Rename WordPress Database Prefix using Database SQL Query with Adminer
Change the Table Prefix in wp-config.php config file
Change the table prefix value in the wp-config.php file in the WordPress directory’s root folder.
To edit the wp-config.php file, you can log in via FTP or SFTP using Filezilla or any other FTP client. You can find FTP details on cPanel (depending on your hosting, as some host doesn’t have cPanel but they use their customized panel)
Search for this line in the wp-config.php file:
$table_prefix = 'wp_testing123_';
![This picture shows where the table prefix can be found in the wp-config.php file.](https://wp-staging.com/wp-content/uploads/2020/08/wp-config-datei-änderung-e1599612309334.png)
![This picture shows how the updated line looks like.](https://wp-staging.com/wp-content/uploads/2020/08/veränderte-wp-config-datei-e1599614969851.png)
You can only add a prefix with numbers, letters, and underscore. Once you are done with the changes in the wp-config.php file, save it.
Rename Database Tables
If you are using Adminer, you can find the SQL command button in the upper left corner and select the button.
![This picture shows a screenshot of where to find the SQL command button on Adminer](https://wp-staging.com/wp-content/uploads/2020/08/Adminer-SQLCommand-finden-e1599613334159.png)
You can use the SQL query below to change the database’s WordPress table prefix on an existing WordPress website.
Update the SET properties to your needs:
- database name
- oldprefix_
- newprefix_
That is the SQL query:
SET @database = "<strong>databasename</strong>";
SET @oldprefix = "<strong>oldprefix_</strong>";
SET @newprefix = "<strong>newprefix_</strong>";
SELECT
concat(
"RENAME TABLE ",
TABLE_NAME,
" TO ",
replace(TABLE_NAME, @oldprefix, @newprefix),
';'
) AS "SQL" FROM information_schema.TABLES WHERE TABLE_SCHEMA = @database;
After customizing the query, you’ll get something like this:
![A SQL query to update the WordPress database Prefix](https://wp-staging.com/wp-content/uploads/2020/08/SQLcommand-change-of-the-table-prefix-1-2-e1599615964374.png)
Execute that query, and as a result, you’ll get further questions like this:
RENAME oldprefix_options to newprefix_options; RENAME oldprefix_users to newprefix_users;
Copy these queries and execute them again to rename them to the new table names.
![Use this SQL query to rename WordPress database tables](https://wp-staging.com/wp-content/uploads/2020/08/SQLcommand-changetableto-1.png)
Rename prefix in user meta Table
After renaming the tables, you also need to replace some values in the table *_usermeta
and *_options
by using the queries below.
Don’t forget to update the highlighted strings.
To replace the values in the *_usermeta
table use this query:
UPDATE `newprefix_usermeta`
SET meta_key = REPLACE(meta_key, 'oldprefix_', 'newprefix_')
WHERE meta_key LIKE 'oldprefix_%';
![Use this SQL query to rename the table prefix in table wp_usermeta](https://wp-staging.com/wp-content/uploads/2020/08/SQLcommand-usermeta_change-1.png)
Update prefix in wp_options Table
The last step is to replace the values in the *_options
table.
Use this query:
UPDATE wp_options SET option_name = replace(option_name, 'wp_', 'new_') WHERE option_name LIKE 'wp_%';
![This picture shows the queries to replace the values in the options table](https://wp-staging.com/wp-content/uploads/2020/08/SQLcommand-optiontable_change.png)
That’s all.
Method 3: Rename WordPress Database Prefix using Database SQL Query with PHPmyAdmin
Change the table prefix in the wp-config.php.
Change the table prefix in the wp-config.php file in the WordPress directory’s root folder.
To edit the wp-config.php file, you can log in via FTP or SFTP using Filezilla or any other FTP client. You can find FTP details on cPanel (depending on your hosting, as some host doesn’t have cPanel but they use their customized panel)
See this line on the wp-config.php file.
$table_prefix = 'wp_testing123_';
You can only add a prefix with numbers, letters, and underscore. Once you are done with the changes in the wp-config.php file, save it.
Rename Database Tables
Suppose you are using cPanel, and access PhpMyAdmin. On the left side, you can see your databases. If you are not using cPanel, contact your host and access your site’s database.
Select the database where you want to change the table prefix specified in the file wp-config.php
and then do the steps below.
- Select the database.
- Check on the ‘check all’ checkbox to select all tables.
- Click on the drop-down and select ‘Replace table prefix.’
![change wordpress table prefix](https://wp-staging.com/wp-content/uploads/2020/08/change-wordpress-table-prefix.jpg)
- Replace the old prefix with the new prefix.’
![update wordpress database prefix](https://wp-staging.com/wp-content/uploads/2020/08/update-wordpress-database-prefix.jpg)
After entering a new prefix, click on continue, which will change the prefix in the database.
Rename Prefix in Options Table
Search the wp_ prefix in the options table using this query.
SELECT * FROM `wp_testing123_options` WHERE `option_name` LIKE '%wp_%'
Replace all old prefixes with new prefixes.
![](https://wp-staging.com/wp-content/uploads/2020/08/wp-database-prefix.jpg)
Update Prefix in Usermeta Table
We need to search wp_ as a prefix on the usermeta table and replace it using this query.
SELECT * FROM `wp_testing123_usermeta` WHERE `meta_key` LIKE '%wp_%'
![database prefix of wordpress site](https://wp-staging.com/wp-content/uploads/2020/08/database-prefix-of-wordpress-site.jpg)
Another option is that if you want to change the database prefix using a database query, you can use this single query to make it work.
If you like to change the WordPress table prefix of the database on an existing WordPress website, you can use the following SQL query:
SET @database = "database_name";
SET @old_prefix = "old_prefix_";
SET @new_prefix = "new_prefix_";
SELECT concat "RENAME TABLE ", TABLE_NAME, " TO", replace(TABLE_NAME, @old_prefix, @new_prefix),';') AS "SQL" FROM information_schema.TABLES WHERE TABLE_SCHEMA = @database;
This query will create another SQL query you can use to rename all table prefixes to the new ones.
After doing that, you also need to replace some values in the table wp_usermeta
and wp_options
using the queries below.
UPDATE `wp_testing123_usermeta`
SET meta_key = REPLACE(meta_key, 'wp_', 'new_')
WHERE meta_key LIKE 'wp_%';
UPDATE wp_options SET option_name = replace(option_name, 'wp_', 'new_') WHERE option_name LIKE 'wp_%';
That’s all.
That’s the manual method to change the database prefix.
Can Not Login to WordPress Admin Dashboard After Renaming Table Prefix
This is the most popular error user are running into when renaming the WordPress table prefix.
Verify that you updated the prefix of all these column names and that you don’t overlook one of them:
In the Table wp_option
- wp_user_roles
In the Table wp_user_meta
- wp_capabilities
- wp_user_level
After renaming the prefix of these columns to the same prefix of the table names then you will be able to login again to your WordPress website.
Conclusion
Changing the WordPress table prefix is a simple yet effective security measure. Whether you use phpMyAdmin, a plugin, or SQL scripts, always back up your database before making changes. This small tweak can go a long way in securing your WordPress site!