PostgreSQL – How to Get Server Uptime

postgresqlpostgresql-9.4

I am working with postgresql-9.4 and for support and monitoring tasks I need to know how long my server has been up for? Is there any function or way to know this? Is there an sql way to know when my server started?

I really need to know the elapsed time since it started?

Best Answer

Use the built-in function pg_postmaster_start_time

select current_timestamp - pg_postmaster_start_time() as uptime

More details in the manual: http://www.postgresql.org/docs/current/static/functions-info.html

If you want seconds, use extract:

select extract(epoch from current_timestamp - pg_postmaster_start_time()) as uptime