
When WordPress shows nothing but the sentence Error establishing a database connection, the site files are intact and the content is in the database. The connection between the two just is not happening. That is good news because nothing has been lost, and bad news in the sense that the site is completely unavailable until the connection is back.
Open /wp-admin. If it shows the same error, the connection is missing entirely. If the admin says the database needs repairing, this is a damaged table rather than a connection problem, so go straight to step 4.
The wp-config.php file in the site root holds the four lines that create the connection. They are the database name, the user, the password and the host. The single most common cause is that somebody changed the database password in the hosting panel and did not update it here. Compare the values with the panel character by character. Our guide to managing the database shows where to find them.
The host is usually localhost. If someone replaced it with an IP address or a remote host name during a migration, the connection breaks even when everything else is right.
A database server can refuse new connections when too many are already open. You notice this as a site that works intermittently and fails at busy hours. The cause is often a heavy plugin or a traffic spike. If the symptom comes and goes by the hour, it is load rather than credentials.
An unexpected restart can leave a table half written. WordPress has a repair mode for this. Add the line define('WP_ALLOW_REPAIR', true); to wp-config.php and open /wp-admin/maint/repair.php. Remove the line right after the repair, because that page works without logging in.
A full disk brings the database down without any other warning, because it cannot write its own temporary files. Look at the space left in your hosting panel. The most common consumer is a backup plugin that has been storing its archives on the same disk.
During a migration it is easy to forget either the database import or the user privileges in the new place. Check that the database exists, that it has tables and that the user has rights to that exact database.
Do not reinstall WordPress. A fresh install creates an empty database and can write over the old one, and then the content really is gone. Take a backup before you try anything, see our guide on backing up a site.
We work out which one it is, a broken connection or a damaged database, and get the site back. We also check why the connection dropped, because otherwise the same cause returns on the next spike.