Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get database creation date on PostgreSQL

Tags:

I want to get the Database creation date in POSTGRESQL. My version is 9.3.4 running on Ubuntu 14.04. Can I do it with a select statement or can I do it with access to the server?

like image 346
Za7pi Avatar asked Jul 17 '14 14:07

Za7pi


People also ask

How can I check when my DB was created?

To find the database creation date you can query sysibm. sysversions or syscat. tables.

What is :: date in PostgreSQL?

DATE data type in PostgreSQL is used to store dates in the YYYY-MM-DD format (e.g. 2022-03-24). It needs 4 bytes to store a date value in a column. Note that the earliest possible date is 4713 BC and the latest possible date is 5874897 AD. It is highly important to retain code readability at the stage of writing it.

Which command displays the current date in PostgreSQL?

The PostgreSQL CURRENT_DATE function returns the current date (the system date on the machine running PostgreSQL) as a value in the 'YYYY-MM-DD' format.


2 Answers

I completely agree with Craig Ringer (excellent answer!)... but for my purposes , this is good enough:

SELECT (pg_stat_file('base/'||oid ||'/PG_VERSION')).modification, datname FROM pg_database; 

Simple and clean (but superuser).

like image 75
Bernardo Jerez Avatar answered Sep 19 '22 17:09

Bernardo Jerez


There is no built-in record of the PostgreSQL database creation time in the system. All approaches that rely on file system creation/modification times can produce wrong answers.

For example, if you pg_basebackup a replica node then fail over to it, the creation time will appear to be the time the replica was created. pg_basebackup doesn't preserve file creation/modification times because they're generally not considered relevant for operation of the database system.

The only reliable way to get the database creation time is to create a 1-row table with the creation time in it when you create the database, then set permissions / add a trigger so it rejects updates.

If you don't mind the risk of a wrong answer where the creation time is reported as being much newer than it really was, you can look at the timestamp of the base/[dboid]/PG_VERSION file. @Bob showed a useful way to do that.

like image 34
Craig Ringer Avatar answered Sep 20 '22 17:09

Craig Ringer