Short answer: “Error establishing a database connection” means WordPress cannot talk to its database. The usual causes are wrong database details in wp-config.php, a database server that is down or overloaded, or a corrupted database. Check with your host first, then verify wp-config.php, repair the database and restore a backup if needed.
Take a breath first. This is one of the most common WordPress errors, and almost every website owner runs into it at some point. Fixing it rarely means touching a single line of scary code.
This guide walks you through exactly why the WordPress “error establishing a database connection” happens and how to fix it, step by step, in plain language everyone can understand.
What Does Error Establishing a Database Connection Actually Mean?
WordPress is really two things working together: the files that make up your theme and plugins, and a database that stores your posts, pages, settings and user data. Every time someone visits your website, WordPress asks the database for that data and assembles the page.
When you see this error, it means WordPress tried to talk to the database and got no response. It is not always a disaster. It usually points to one of these five causes:
- Incorrect database credentials in wp-config.php
- The hosting server’s database is down or temporarily overloaded
- A corrupted WordPress database, where one or more tables are damaged
- A wp_options table error, where a core table has crashed
- A hacked or accidentally edited file that broke the connection settings
Let’s fix each of these, starting with the simplest checks and moving toward the more technical ones.
Step 1: Rule Out a Simple Hosting Outage First
Before touching any files, check whether your host is having a bad day. Sometimes a database outage on the hosting server is the real culprit, and no amount of file fixing will cure it because the problem is not on your end at all.
Here is how to check quickly:
- Log in to your hosting control panel (cPanel, Plesk or your host’s custom dashboard) and see if you can view your database from there.
- Open a support chat or ticket with your host and ask if there is a reported outage.
- Try visiting another website hosted on the same server, if you have access to one.
If the database really is down, your host needs to restart the database service on their end. There is nothing more you can do here except wait or escalate the ticket. If everything looks normal on their side, go to Step 2.
Step 2: Check Your wp-config.php Database Credentials
This is the single most common cause of this error. Your wp-config.php database credentials are the login details WordPress uses to access your database. If even one character is wrong, WordPress cannot connect.
Here is how to check them:
- Connect to your website using FTP, SFTP or the File Manager in your hosting panel.
- Locate the wp-config.php file in your site’s root folder.
- Download a copy first, just in case you need to revert.
- Open the file and look for these lines near the top:
define( ‘DB_NAME’, ‘your_database_name’ );
define( ‘DB_USER’, ‘your_database_username’ );
define( ‘DB_PASSWORD’, ‘your_database_password’ );
define( ‘DB_HOST’, ‘localhost’ );
- Log in to phpMyAdmin or your hosting database tool and confirm the actual database name, username and password match what is written in the file.
- Pay close attention to DB_HOST. Most hosts use localhost, but some managed hosts require a specific value, such as an IP address or a custom hostname.
If anything is mismatched, correct it, save the file and upload it again. Then refresh your website. This resolves the problem for a large share of people who see this error.
If you are not sure what your correct credentials are, your hosting company can look them up or help you reset the database password. Then paste the new password into wp-config.php.
Step 3: Check If a Plugin, Theme, or Hack Changed Your Files
Sometimes a plugin update, a theme conflict or, in rarer cases, a security breach can quietly alter your wp-config.php file or corrupt core files. If your credentials look correct in Step 2 but the error persists, this is worth investigating.
A quick way to check is to temporarily rename your plugins folder using FTP (rename wp-content/plugins to something like plugins-old) and see if the website comes back. If it does, a plugin is interfering, and you can rename folders back one by one to find the culprit.
If you suspect a hack, the safest move is replacing your files with a clean, known-good copy. This is exactly where a recent backup saves you hours of guesswork.
Step 4: Repair a Corrupted WordPress Database
If your credentials are correct and no file conflicts are found, the next possibility is a corrupted WordPress database. Sometimes individual tables inside the database get damaged, often after an interrupted update, a sudden server crash or a failed import.
Here is a quick way to test this.
Visit yoursite.com/wp-admin directly. If you get a specific message such as “One or more database tables are unavailable. The database may need to be repaired,” you have confirmed the issue is table corruption rather than incorrect credentials.
To repair it:
- Open wp-config.php again.
- Add this line just above the line that says /* That’s all, stop editing! */:
define( ‘WP_ALLOW_REPAIR’, true );
- Save the file and visit yoursite.com/wp-admin/maint/repair.php in your browser.
- Click “Repair Database” or “Repair and Optimize Database.”
- Let WordPress work through every table. This can take a minute or longer, depending on your database size.
Once finished, remove the line of code you added to wp-config.php. Leaving it in place exposes the repair tool to anyone who finds the URL, which is a real security risk.
Step 5: Fix a wp_options Table Error
Occasionally, the repair tool flags a specific problem: a wp_options table error, often shown as something like “wp_options: Table is marked as crashed.” The wp_options table is one of the most important tables in WordPress because it stores your site settings, your active plugin list and more.
To repair a crashed table manually:
- Log in to phpMyAdmin through your hosting panel.
- Select your WordPress database from the left sidebar.
- Find the wp_options table (it may have a different prefix, like wp7x_options).
- Tick the box next to it, then choose “Repair table” from the dropdown menu at the bottom.
- Once repaired, run the WordPress repair script from Step 4 again to confirm everything is fine.
If the table cannot be repaired through phpMyAdmin, that is a good time to restore from a clean backup rather than risk further data loss by experimenting.
Step 6: Restore From a Recent Backup
If none of the steps above solve the problem, or if you would rather avoid manual database work altogether, restoring a recent backup is often the fastest and safest way back online. A good backup restores your files and database exactly as they were before the error appeared.
This is exactly the kind of scenario WP Demi was built for. It creates full-site and database backups that you can restore in a few clicks, so an error like this can become a five-minute fix instead of a day of troubleshooting.
Step 7: Contact Your Hosting Provider
If you have worked through every step above and the website is still down, it is time to bring in your host’s support team. Explain what you have already tried (checked credentials, ran the repair tool, checked for outages) so they can skip the basics and dig into server-level logs. Many hosts can also check whether your database user has the right permissions or whether your account has hit a resource limit.
How to Prevent This Error From Happening Again
Fixing the error quickly is good. Making sure it does not surprise you again is better. A few practices go a long way:
- Keep your wp-config.php credentials stored somewhere safe outside your live website.
- Avoid editing core files unless you know exactly what you are changing.
- Create a fresh backup regularly and before every update so you always have a recent, restorable copy of your website.
- Keep plugins and themes up to date from trusted sources to reduce the risk of conflicts or corruption.
- Monitor your hosting resource usage so you are not caught off guard by a database outage caused by hitting your plan’s limits.
If backups are not already part of your routine, that is the best upgrade you can make today. Our guide on how to back up a WordPress site shows you how.
Download WP Demi and create your first backup in minutes.
Conclusion
That frightening white screen feels like the end of the world, but it almost never is. Nine times out of ten, the fix is small: correcting your wp-config.php database credentials, confirming your host’s database service is actually running, or repairing one broken table. Work through the steps in order and you will usually be back online quickly.
But here is the real takeaway. The “error establishing a database connection” only looks like a crisis if you have nothing to fall back on. With a recent, reliable backup ready, this whole ordeal shrinks to a five-minute repair.
Protect Your Site Before the Next Error Hits
Errors like this are far less stressful when you have a recent backup ready. WP Demi gives you full-site backups, one-click restores and simple WordPress migration, all from one lightweight plugin for developers, agencies and site owners. Try WP Demi free today.
FAQ’s
1. What causes the error establishing a database connection in WordPress?
It is usually caused by wrong database credentials in wp-config.php, a database server that is temporarily down, a corrupted database table, or a plugin or theme conflict that altered your core files.
2. Will I lose my content if I see this error?
Usually not. Your content lives in the database, and this error means WordPress cannot reach it right now. Once the connection is restored, your content normally comes back. A recent backup protects you in the rare case that tables are badly damaged.
3. How do I know if the problem is my database or my hosting server?
Try visiting yoursite.com/wp-admin directly. If you see a different message about database tables needing repair, the issue is likely corruption. If nothing loads at all and other sites on the same server are also down, it may be a hosting server database outage.
4. Is it safe to run the WordPress database repair tool?
Yes, it is a built-in WordPress feature designed for this exact situation. Just remember to remove the WP_ALLOW_REPAIR line from wp-config.php once you are done, since leaving it active is a security risk.
5. What is the fastest way to fix this error without editing code?
Restoring your latest backup is usually the quickest option, since it brings back a known working version of your files and database without manual troubleshooting. This is why creating regular backups is so valuable.
