Check the uptime of a PostgreSQL Database

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:

uptimeserver_start_timecurrent_time
2 days 3:45:202025-01-17 10:15:00+002025-01-19 14:00:20+00

Explanation:

  1. pg_postmaster_start_time():
    • This function returns the timestamp when the PostgreSQL server process (postmaster) was last started.
  2. now():
    • This function returns the current timestamp.
  3. now() - pg_postmaster_start_time():
    • This calculates the difference between the current time and the server start time, giving you the uptime.

Leave a Reply