Skip to main content

Fix "Error establishing a database connection"

WordPress stores your posts, pages, settings and users in a database. When it shows "Error establishing a database connection", it could not reach that database at all, so it cannot build any page. This guide explains the common causes and how to check each one. It is for site owners who can connect to their site's files with SFTP or a file manager.

Why it happens​

CauseTypical sign
Wrong database details in wp-config.phpThe error appears straight after a move, a password change or an edit to wp-config.php
Database server not respondingThe error comes and goes, or other sites on the same account are affected too
Damaged database tablesThe front of the site fails but /wp-admin shows a different message, or the error follows a crash
An incomplete migrationThe error appears on the new host, while the old copy still works

Check the database details​

WordPress reads its database details from wp-config.php in the site's main folder. Open the file and find these four lines:

define( 'DB_NAME', 'example_wp' );
define( 'DB_USER', 'example_user' );
define( 'DB_PASSWORD', 'your-database-password' );
define( 'DB_HOST', 'localhost' );
SettingWhat it is
DB_NAMEThe name of the database
DB_USERThe database user WordPress signs in as
DB_PASSWORDThat user's password
DB_HOSTThe address of the database server. Often localhost, but some hosts use a separate hostname.
  1. In your hosting control panel or account, open the section for databases. The exact place depends on your host.
  2. Check that a database with exactly the DB_NAME value exists. Some hosts add a prefix, such as your account name, to every database and user name.
  3. Check that the user in DB_USER exists and has full rights on that database.
  4. If you are unsure of the password, set a new password for the database user there, then put the same password in DB_PASSWORD.
  5. Confirm the correct DB_HOST value with your host.
  6. Save wp-config.php and reload the site.

Watch for small mistakes: an extra space inside the quotes, a missing quote, or a straight quote replaced by a curly one by a word processor. Edit the file in a plain text editor. See Databases explained.

Check whether the database server is running​

If the details are correct and the error comes and goes, the database server may be down or overloaded.

  • Check whether other sites on the same hosting account show the same error.
  • Look at your host's status page or notices.
  • Check whether your account has reached a resource limit. See Resource limits.

On a VPS that you manage yourself, check that the database service (MySQL or MariaDB) is running and has enough memory and disk space.

Repair damaged tables​

If tables in the database are damaged, WordPress has a built-in repair tool. It is switched off by default because it works without signing in.

  1. Add this line to wp-config.php, above the line that says /* That's all, stop editing! */:

    define( 'WP_ALLOW_REPAIR', true );
  2. Open https://example.com/wp-admin/maint/repair.php, using your own domain.

  3. Choose Repair Database. The other option also optimises the tables and takes longer.

  4. When it finishes, remove the line from wp-config.php.

Remove the repair line straight away

While WP_ALLOW_REPAIR is set, anyone who knows the address can run the repair page. Delete the line as soon as the repair is done.

After a migration​

If the error appears right after moving the site to a new host:

  • The database may not have been imported yet, or the import stopped partway. Check that the tables exist, for example in phpMyAdmin if your host offers it.
  • wp-config.php probably still holds the old host's details. Update all four settings for the new host.
  • If you are testing with your computer's hosts file, make sure it points to the new server. See Plan a website migration.

Still stuck?​

Restore the files and database from your most recent working backup, then contact support with the time the error started and anything that changed shortly before.

At DomainPilot

Contact DomainPilot support with the support form. DomainPilot web hosting and WordPress hosting include a free website migration, where the team moves your site for you. Ask for one with the migration request form.

Did this article help you?