Check the uptime of a PostgreSQL database
you can use the following query. This query retrieves the server’s start time and calculates the uptime:
SELECT
now() - pg_postmaster_start_time() AS uptime,
pg_postmaster_start_time() AS server_start_time,
now() AS current_time;
Example Output:
| uptime | server_start_time | current_time |
|---|---|---|
| 2 days 3:45:20 | 2025-01-17 10:15:00+00 | 2025-01-19 14:00:20+00 |
Explanation:
pg_postmaster_start_time():- This function returns the timestamp when the PostgreSQL server process (postmaster) was last started.
now():- This function returns the current timestamp.
now() - pg_postmaster_start_time():- This calculates the difference between the current time and the server start time, giving you the uptime.