Fork me on GitHub
#sql
<
2023-10-31
>
Marius13:10:17

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?

lukasz14:10:50

I think ‘SELECT 1;’ should be enough - you just need to know that the connection is alive and not touch the schemas

👍 2
Marius16:10:53

Make sense… thank you!

fraxamo09:12:58

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

Marius09:12:24

That is good to know, thank you! Btw @U0JEFEZH6 already proposed a better solution by just using SELECT 1; so that it does not depend on any table.

👍 1