"Error Establishing a Database Connection" in WordPress: How to Fix It
What "Error establishing a database connection" means in WordPress, its usual causes, checking wp-config.php safely, repairing tables and when to ask your host.

On this page
- The error means WordPress cannot reach its database, usually through wrong wp-config.php details or a struggling server.
- Rule out the host first, then match the database name, user, password and host against your hosting panel.
- Use WordPress's repair tool for damaged tables, remove it straight after, and restore the database only as a last step.
Instead of your homepage, a single line of text: "Error establishing a database connection". It usually appears without warning, often right after a migration or a hosting change, and it takes the whole site offline. The good news is that your content is normally still there. WordPress just can't reach it. Here's how to work out why, step by step.
What the error means
A WordPress site has two parts: the files (WordPress itself, your theme and plugins) and a database that stores your pages, posts, settings, users and, on a store, your orders. Every time a page loads, WordPress connects to the database using four details saved in a file called wp-config.php:
- DB_NAME: the database's name
- DB_USER: the database username
- DB_PASSWORD: that user's password
- DB_HOST: the server the database lives on
If any of these is wrong, or the database server doesn't respond, WordPress stops and shows this error. A related message in the dashboard, "One or more database tables are unavailable", points to damaged tables rather than a failed connection.
The usual causes
| Cause | Typical clue |
|---|---|
| Wrong details in wp-config.php | Right after a migration, a host change, or someone changing the database password |
| Database server down or overloaded | The error comes and goes, or other sites on the same hosting are down too |
| Hosting limits reached | Happens during traffic spikes on busy shared hosting |
| Database or user removed | After hosting changes, an expired plan or a suspended account |
| Corrupted tables | After a server crash or failed update; the dashboard mentions tables needing repair |
| Tampered wp-config.php | Rare, but possible after a hack; unfamiliar code in the file |
Step 1: Rule out the hosting server
Many of these errors are on the host's side and fixed by them. Check your host's status page or support chat, and see whether other websites on the same hosting account are also failing. If the error appears on and off, especially at busy times, the database server is likely struggling or hitting connection limits.
Look in your hosting panel for resource limit warnings or a suspension notice too. For a wider checklist of what to look at when a site won't open, see website down: what to check, step by step.
Step 2: Check the database details in wp-config.php
If the host says everything is running, the connection details are the next suspect. Using your hosting file manager or SFTP, download a copy of wp-config.php before changing anything. Then compare it with the MySQL Databases section of your hosting panel:
- Database name: does a database with exactly that name exist? Many panels add your account name as a prefix, and the full name must match
- Username: does that user exist, and is it assigned to this database with all privileges?
- Host: often "localhost", but some hosts use a separate database server name. Your hosting panel or the host's documentation will tell you
- Password: panels rarely show existing passwords, so reset the database user's password in the panel and paste the new one into wp-config.php
Watch for stray spaces and keep the quote marks around each value intact. Save the file and reload the site. Never paste the contents of wp-config.php into public forums or screenshots, because it contains working passwords.
Step 3: Repair corrupted tables
If the site connects but the dashboard says tables are unavailable, WordPress has a built-in repair tool:
- Take a database backup first, from your hosting panel or phpMyAdmin
- Add this line to wp-config.php, above the line that says to stop editing:
define( 'WP_ALLOW_REPAIR', true ); - Visit yoursite.com/wp-admin/maint/repair.php and choose Repair Database
- Remove the line straight afterwards, because that repair page works without anyone logging in
Your host can also check and repair tables from their side. If repair doesn't fix things, restoring the database is the next step.
Step 4: Restore the database from a backup
If the database was deleted or is badly damaged, restore the latest good copy from your host's backups or your backup plugin; see the backup and restore guide.
On a WooCommerce store or booking site, remember that anything received after the backup was taken, such as orders, bookings and enquiries, won't be in the restored copy. Note them from your emails and payment gateway dashboard before you restore.
After a migration: the most common case
Moving to new hosting is the classic trigger. The new host gives you a new database name, user and password, and wp-config.php still has the old ones. Other migration-related issues:
- The database import failed partway, leaving tables missing
- DNS is still pointing some visitors to the old server, where the hosting may already be cancelled
- If you see the WordPress installation screen instead of the error, the table prefix in wp-config.php probably doesn't match the imported tables
Keep the old hosting active until the new site is fully tested and DNS has settled.
How to stop it happening again
- Store your database details securely, in a password manager, not in an email thread
- Choose hosting with enough resources for your traffic; see choosing WordPress hosting in India
- Use caching, which reduces how often pages need the database
- Keep automatic daily backups that include the database, stored off the server
- Set up uptime monitoring so you hear about outages before your customers do
Site down with a database error, or planning a move and want to avoid one? See WordPress maintenance and support and WordPress migration.
Need help with your website?
I'm Sameer, a freelance WordPress developer building fast, SEO-friendly websites since 2020. Tell me what you need and I'll reply with a plan and a fixed quote within 24 hours.


