If you’re staring at the message “Error establishing a database connection” on your WordPress site, here’s the short version: WordPress can’t talk to the database where all your content lives, so it has nothing to show. Nothing is necessarily lost — your posts, pages, and settings are almost certainly still there. WordPress just can’t reach them right now. This guide explains why that happens and walks you through the fixes in order.
Every WordPress site is really two parts: the files (your theme, plugins, and WordPress itself) and the database (your actual content and settings). When the connection between them breaks, you get this error. There are only a handful of reasons it breaks, so let’s work through them.
What causes the error
The usual suspects, from most to least common:
- Wrong database login details in your
wp-config.phpfile (WordPress’s main settings file) — often after a migration or a host change. - The database server is down or overloaded — common on cheap shared hosting during traffic spikes.
- A corrupted database — usually after a crash, a bad plugin, or an interrupted update.
- A hacked or maxed-out server that’s run out of connections or resources.
Step 1: Check whether it’s just the front end
Try to load your admin area at yoursite.com/wp-admin. You might see a slightly different message: “One or more database tables are unavailable. The database may need to be repaired.” If you see that, jump ahead to the repair step — WordPress is telling you the database itself is damaged, not that the login is wrong. If both the front end and the admin throw the same generic connection error, keep going in order.
Step 2: Confirm your database credentials
This is the most common fix, especially if the error appeared right after you moved hosts or changed something. Your site’s wp-config.php file (in the main WordPress folder) holds four lines that must exactly match what your host gave you:
DB_NAME— the database nameDB_USER— the database usernameDB_PASSWORD— the passwordDB_HOST— usuallylocalhost, but some hosts use a specific server address
Open the file over SFTP (a secure way to connect to your site’s files) or your host’s file manager and compare those four values against your hosting control panel. A single wrong character — a password that was rotated, or a DB_HOST that should be something other than localhost — will cause this exact error. Fix any mismatch, save, and reload your site. Never post these values into a chat window, an email, or a support forum; they’re the keys to your data.
Step 3: Repair the database
If the credentials are correct but you saw that “tables may need to be repaired” message, WordPress has a built-in repair tool. Add this line to wp-config.php, just above the “That’s all, stop editing” comment:
define('WP_ALLOW_REPAIR', true);
Now visit yoursite.com/wp-admin/maint/repair.php in your browser and click “Repair Database” (or “Repair and Optimize”). No login is needed, which is exactly why you should remove that line again the moment you’re finished — leaving it in is a security hole. If the repair succeeds, your site should come right back.
Step 4: Check if the database server is simply down
If your credentials are right and the database isn’t corrupt, the server hosting your database may be down, overloaded, or refusing new connections. This is where cheap shared hosting shows its seams: dozens or hundreds of sites share one database server, and when it gets busy, yours can’t get a connection.
A couple of ways to tell:
- If you have other sites on the same host, check whether they’re down too — a shared outage points at the server.
- Check your host’s status page, or ask their support whether the database server is healthy.
If the server is the problem, there’s nothing to fix in your files — it’s the host’s job. And if it keeps happening, that’s your site telling you it has outgrown a bargain plan.
When it’s the host’s problem
A recurring database connection error that isn’t a credentials mistake is one of the clearest signs you’re on hosting that’s too crowded or too weak for your site. You shouldn’t have to babysit a repair tool because a neighbor’s traffic spike knocked over a shared database server.
This is one of the reasons people move to managed WordPress hosting. On our platform, sites don’t fight each other for a single overloaded database, server resources are monitored automatically so a resource crunch is caught before it becomes an outage, and every update we apply is verified to make sure your site still looks and works right afterward — no interrupted-update corruption. Migration is free and we handle it, so credentials never get fumbled in the move. If your database error keeps coming back, that’s usually the real fix.
If nothing works: restore from backup
Occasionally the database is genuinely damaged beyond a quick repair — after a crash or a bad plugin write. That’s what backups are for. A clean, recent copy restores your content to the last known-good state in minutes. Walk through it with our guide on how to restore WordPress from a backup. (Every plan we run includes off-site backups 3 times a day, so there’s always a clean copy waiting.)
A quick recap
- Check whether the admin area shows a “repair” message.
- Verify your four database credentials in
wp-config.php. - Run the built-in repair tool — then remove the enable line.
- Rule out a down or overloaded database server (that’s the host).
- Still broken? Restore from your latest clean backup.
If this error is a recurring visitor — or you’d simply rather not be editing config files under pressure — hand it off. This is a routine fix for our US-based team, and moving to hosting where the database isn’t fighting for scraps usually makes it stop for good. If your site is showing other symptoms too, our emergency troubleshooting checklist and our guide to the white screen of death cover the neighboring problems.