Knowledgebase / WordPress
Fix the WordPress Error Establishing a Database Connection
Find why WordPress cannot connect to its database without replacing the database, exposing passwords or overwriting newer content.
Prerequisites
You need authorized file-manager or SFTP access, the hosting database panel, and a recent backup. Record when the error began and whether a migration, password reset or configuration edit happened immediately beforehand. Do not reinstall WordPress over the affected site.
The database stores posts, users and many application settings. Creating a new empty database with a similar name does not restore that information. Preserve the original database and configuration while identifying the failed connection.
Step 1: Determine the scope
Check the homepage and administration login. Note whether other sites using the same database service are affected. A widespread failure points toward the service or account resources, while a single recently moved site suggests configuration or privileges. Ask the host about a service incident before making application changes.
Review private PHP and database-related logs around the first failure. Record the error category, such as access denied or connection refused, without copying credentials into a public ticket. Logs may contain database names or local paths that should remain private.
Step 2: Compare configuration with the account
- Make a protected copy of wp-config.php outside the public directory.
- Locate DB_NAME, DB_USER, DB_PASSWORD and DB_HOST. Compare them with the actual database service information.
- Check account prefixes on database and username values. The complete name matters.
- Use the host-provided database hostname and any required port or socket configuration. Do not assume localhost on a remote database setup.
- Correct only a verified mismatch and retain valid PHP quoting.
Do not display wp-config.php in the browser or put a database password in a shell command that can enter history. Use the hosting panel’s supported credential workflow. If a database-user password must change, identify every application using that user and update them together.
Step 3: Check database assignment
In the database management panel, confirm that the configured user is assigned to the intended database with the privileges the application requires. A user can exist but have no access to that database. Avoid granting access to unrelated databases.
If authentication and assignment are correct, ask support to check database availability, connection limits and resource exhaustion. Repeated refreshes or mass connection tests can worsen an overloaded service. For a suspected corrupt table, obtain a backup and a provider-supported repair plan first; generic repair commands are not a substitute for diagnosis.
Troubleshooting after recovery
If the database connects but the site shows an installation screen, stop: WordPress may be pointing to an empty database or the wrong table prefix. Recheck the original configuration rather than completing installation. If older content appears, confirm the correct database copy before accepting new orders or edits.
The expected result is the original site loading with its current content and working login. Test a safe read and an authorized edit, then confirm no new connection errors. Remove temporary diagnostics and retain the protected backup until recovery is verified.
Frequently asked questions
Can I fix this by reinstalling WordPress?
Reinstallation does not correct a failed database connection and can complicate recovery.
Is DB_HOST always localhost?
No. Use the value supplied for the actual database service.
Should I reset the database password first?
Only when a credential problem is confirmed and every application using that account is accounted for.
Related guides and hosting options
Compare WordPress hosting against your application requirements. Confirm available tools and management responsibilities for your own account. For account-specific help, open a private support ticket with the domain, time and relevant redacted error.