Can't connect to the database
Connection refused, timed out, or authentication failed.
Read the error precisely
The three messages mean different things and point at different fixes.
Connection refused — you reached the machine and nothing is listening on that port. The database isn't running, or it's on a different port.
Connection timed out — you never reached the machine. A firewall, a network rule, or the wrong host entirely.
Authentication failed — you reached the database and it rejected your credentials. Username, password, or the database name.
Then check, in order
Is it running? Locally, has the service started? In production, is the instance awake — several managed hosts sleep free-tier databases.
Is the host right? localhost in your .env will not reach your production database. Inside containers, localhost means the container itself, not your machine.
Is your IP allowed? Managed databases allow-list by IP. Your home connection changed, or your host's outbound IP isn't on the list.
SSL required? Many managed providers refuse plain connections. The error can look like a timeout rather than anything about certificates.
Connection pool exhausted? If it works then fails under load, you're leaking connections — opening them without closing.
Database connection failing. Exact error: <paste> Environment: <local / production>. Database: <Postgres/MySQL/etc>, <self-hosted / managed on X>. My connection config, with the password redacted: <paste> Given that exact error message, which of these is it: not running, wrong host or port, IP not allow-listed, SSL required, bad credentials, or pool exhausted? Then give me the shortest command that tests the connection outside my application, so I can tell whether it's my code or the connection.
Test outside the app
Connect with the database's own client — psql, mysql — using the same credentials. If that works and your app doesn't, the problem is in your code or config parsing. If it fails too, the problem is the connection itself. That split is the fastest way through this.
"Works then stops after a while" is nearly always connection leaking. Every connection you open must be released, including on the error path.