How to fix "Error establishing a database connection" in WordPress
By Ash, Founder, HosteazePublished 3 min read
The short answer
This error means WordPress can't connect to the database that holds your content. The cause is usually wrong details in wp-config.php (database name, username, password or host), a database server that's down or overloaded, or damaged database tables. Check the four wp-config.php values first. WordPress's own guide names them as the usual cause.

Every page on a WordPress site is built from the database: posts, pages, menus, settings. When WordPress can't log in to that database, it can't build anything, so it shows this one line instead. The fix is usually a single wrong value in one file.
What causes the database connection error?
| Cause | Typical trigger | How to tell |
|---|---|---|
| Wrong details in wp-config.php | You moved hosts, restored a backup or changed the database password | Error appears right after the change |
| Database server down or overloaded | Server problem or traffic spike | Error comes and goes, or your host's status page shows an issue |
| Database limit reached | Your hosting account hit a database size or connection limit | Host's control panel or support confirms it |
| Damaged tables | A crash or failed update | The dashboard says "One or more database tables are unavailable" |
| Compromised site | Attacker changed files or passwords | wp-config.php has changed and you didn't change it |
How do I check the details in wp-config.php?
- Connect to your site by SFTP, or open your hosting control panel's file manager.
- Open
wp-config.phpin your WordPress folder (the one that also containswp-admin). - Find these four lines:
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );- Compare each value with the database details in your hosting control panel. Watch for a missing prefix: some hosts add your account name to the start, such as
acme_wp1. - Check
DB_HOST. It's oftenlocalhost, but some hosts use a separate server name. Your host's documentation will say which. - If you're not sure the password is right, set a new password for the database user in your control panel and paste it into
DB_PASSWORD. - Save the file and reload your site.
A quick way to test the username and password: log in to phpMyAdmin with them. If phpMyAdmin rejects them, so will WordPress.
What if the details are correct?
Then the database server is the next suspect. Try loading the site again in five minutes, since a server restart or brief overload often clears on its own. If it doesn't, contact your host and ask whether the database server is running and whether your account has hit a database limit. WordPress's own troubleshooting guide lists both as common causes.
How do I repair the WordPress database?
If you see "One or more database tables are unavailable", the tables may need repairing. WordPress has a built-in tool for this.
- Take a backup of the database first. See how to back up a WordPress site.
- Add
define( 'WP_ALLOW_REPAIR', true );towp-config.php, above the "stop editing" line. - Visit
yourbusiness.co.uk/wp-admin/maint/repair.php. - Click Repair Database.
- Remove the line from
wp-config.phpstraight away. The repair page works without logging in, so anyone could run it while that line is there.
How do I stop it coming back?
Keep a record of your database details somewhere safe, so a password change doesn't leave you guessing. If the error keeps returning under traffic, your site may be outgrowing its resources; why is my WordPress site slow covers how to tell. If the screen is blank rather than showing this message, see how to fix the white screen of death instead. And if wp-config.php changed without you touching it, follow what to do when your WordPress site is hacked.
Related guides: What is web hosting?, What is cPanel?.
Frequently asked questions
Is my content lost when I see this error?
Almost certainly not. The error means WordPress can't reach the database, not that the database is gone. Once the connection is fixed, your posts and pages come straight back.
Why did the error appear on its own when I hadn't changed anything?
Then the problem is probably on the server side. The database server may be down, restarting or overloaded by a traffic spike, or your account may have hit a database limit. Wait a few minutes, then contact your host if it persists.
Can a hacked site cause this error?
It can, if an attacker changes wp-config.php or the database password. WordPress's own troubleshooting guide lists a compromised site as a possible cause once you've ruled out wrong details and server problems.
