获取 PostgreSQL 上的数据库创建日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24806122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:32:52  来源:igfitidea点击:

Get database creation date on PostgreSQL

databasepostgresql

提问by Za7pi

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 selectstatement or can I do it with access to the server?

我想在 POSTGRESQL 中获取数据库创建日期。我的版本是在 Ubuntu 14.04 上运行的 9.3.4。我可以用select语句来完成还是可以通过访问服务器来完成?

采纳答案by Craig Ringer

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.

系统中没有内置PostgreSQL数据库创建时间的记录。所有依赖文件系统创建/修改时间的方法都可能产生错误的答案。

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

例如,如果您pg_basebackup的副本节点随后故障转移到它,则创建时间将显示为创建副本的时间。pg_basebackup不保留文件创建/修改时间,因为它们通常被认为与数据库系统的操作无关。

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.

获取数据库创建时间的唯一可靠方法是在创建数据库时创建一个包含创建时间的 1 行表,然后设置权限/添加触发器以拒绝更新。

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_VERSIONfile. @Bob showed a useful way to do that.

如果您不介意错误答案的风险,其中报告的创建时间比实际更新的时间长得多,您可以查看base/[dboid]/PG_VERSION文件的时间戳。@Bob展示了一种有用的方法来做到这一点

回答by Bernardo Jerez

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

我完全同意 Craig Ringer(很好的答案!)......但就我的目的而言,这已经足够了:

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

Simple and clean (but superuser).

简单干净(但超级用户)。