O'Reilly logo

PostgreSQL 9 Administration Cookbook by Hannu Krosing, Simon Riggs

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

What is the server uptime?

Or: How long is it since the server started?

How to do it...

Issue the following SQL from any interface:

postgres=# SELECT date_trunc('second', current_timestamp - pg_postmaster_start_time()) as uptime;
uptime
──────────
00:38:15

How it works...

Postgres stores the server start time, so we can access it directly as follows:

postgres=# SELECT pg_postmaster_start_time();
pg_postmaster_start_time
───────────────────────────────
2009-11-26 09:39:23.354208+00

then we can do some SQL to get the uptime as follows:

postgres=# SELECT current_timestamp - pg_postmaster_start_time();
?column?
────────────────
00:35:30.22868

and then do some formatting, such as:

postgres=# SELECT date_trunc('second', current_timestamp - pg_postmaster_start_time()) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required