How to find postgresql server uptime

In this article, we will see how to find PostgreSQL Server uptime through simple query. uptime is the difference between PostgreSQL start time and current time.

Lets connect to PostgreSQL Server and run the commands:

Get the PostgreSQL Server start time by using below command:

select pg_postmaster_start_time();

pg_postmaster_start_time
——————————
2019-11-16 09:31:03.47614-08
(1 row)

Get the current time by using below command:

select current_timestamp;

current_timestamp ------------------------------- 2019-11-17 20:47:45.987514-08 (1 row)

How to find postgresql uptime

uptime is the difference between PostgreSQL start time and current time. So, below query will give the uptime.

select date_trunc('minute',current_timestamp-pg_postmaster_start_time()) as uptime;

uptime

---------------- 1 day 11:36:06 (1 row)

So, server uptime is one day and 11 hours 36 minutes and 6 seconds.