How to resolve HTTP 502 error?

At this scale I recommend dedicated PG resources, any reason you are not running your database on a dedicated instance or managed PG?