My managed PostgreSQL went down today for a few hours facepalm, but that did not affect my uptime monitoring unfortunately (because the backend still returned HTTP 200).
Now I want to implement an API endpoint to check DB status.
My approach would be to do a SELECT 1 FROM account; and catch any exception. Or is there some other elegant way to do that?
I think ‘SELECT 1;’ should be enough - you just need to know that the connection is alive and not touch the schemas
Make sense… thank you!
Bit late to respond but thought I would anyway just in case it helps someone else. The query SELECT 1 FROM account; will doubtless work, but PostgreSQL actually has a definitive way of checking your database status called pg_isready. Is it more elegant than your solution? That depends. I can see a couple of benefits of using it:
1. If the 'accounts' table is dropped, renamed or moved to another schema then your check may no longer work.
2. pg_isready also has a few different return statuses that will give you more information about connection issues than a simple SELECT.
The docs are here: https://www.postgresql.org/docs/current/app-pg-isready.html
That is good to know, thank you!
Btw @lukaszkorecki already proposed a better solution by just using SELECT 1; so that it does not depend on any table.