If you see Error establishing a database connection error is caused when your wordpress is not able to access the database. Primary reason for this error could be the database details entered in wp-config.php is incorrect.
A hacked website could also cause this error which could be recovered by restoring the database backup. You could easily restore database backup by using hosting control panel, PhpMyAdmin or other mysql clients, in case you don’t have backup you could ask your service provider to restore one for you.
The other reason behind this error could be allotted quota for database is exhausted or number of database limit is exceeded.
How to fix it?
In case of Incorrect database details:
Let’s start with the most common reason, incorrect database details. Locate a file wp-config.php stored in the root folder of yourwordpress directory. Open wp-config.php and navigate to database details i.e. Host name, userid, password and database name, if you find incorrect entries just replace them with the right one, save it and refresh the website and it should work.
In case of Website Hacked:
In case your website is hacked, you would be required to clean it up. Use some online tool available in order to check if website has some malicious codes, I would recommend Sucuri Site Scanner to scan your website, I have used it once which helped me remove malicious codes from theme’s function.php file. You can find more details about this problem at WordPress codex FAQ.
In case of Other issues:
In case your database limit or allotted quota is exhausted, you should contact your hosting provider to help you increase it.