简体   繁体   English

PostgreSQL中是否始终提供“postgres”数据库?

[英]Is the “postgres” database always available in PostgreSQL?

I'm writing a script that needs to get all the databases in a postgres cluster, and so it needs to connect to a database (it doesn't matter which) to do this. 我正在编写一个需要在postgres集群中获取所有数据库的脚本,因此它需要连接到数据库(这无关紧要)。

Is it okay to assume that the "postgres" database will always be available to connect to, or if not, is there a programatic way to determine an arbitrary database in the cluster that I can connect to? 是否可以假设“postgres”数据库始终可用于连接,或者如果没有,是否有一种编程方式来确定我可以连接到的群集中的任意数据库?

In my personal experience, yes, it's almost guaranteed that it will exist. 根据我的个人经验,是的,几乎可以肯定它会存在。 But, according to the PostgreSQL official documentation: 但是,根据PostgreSQL官方文档:

The database server itself does not require the postgres database to exist, 
but many external utility programs assume it exists.

So, you can't assume it will always be available, although in the practice, it will be available almost for sure. 所以,你不能认为它总是可用的,虽然在实践中,几乎可以肯定它是可用的。

PostgreSQL has three "system" databases: postgres, template0, and template1. PostgreSQL有三个“系统”数据库:postgres,template0和template1。 I'm pretty sure all of them can be dropped. 我很确定所有这些都可以被删除。 (You might want to test this in a virtual machine.) I'm certain that postgres and template1 can be dropped by a privileged user. (您可能希望在虚拟机中对此进行测试。)我确信特权用户可以删除postgres和template1。 By default, template0 doesn't accept connections. 默认情况下,template0不接受连接。

Having said that, the main purpose of "postgres" is to give you a database to connect to . 话虽如此,“postgres”的主要目的是为您提供一个连接数据库

The postgres database is also created when a database cluster is initialized. 初始化数据库集群时也会创建postgres数据库。 This database is meant as a default database for users and applications to connect to. 此数据库用作连接用户和应用程序的默认数据库。 It is simply a copy of template1 and can be dropped and recreated if necessary. 它只是template1的副本,可以在必要时删除并重新创建。

So it's a reasonable assumption, but not a bullet-proof assumption. 所以这是一个合理的假设,但不是一个防弹的假设。 I can imagine some paranoid (careful) shops might drop "postgres" and use a different database as their internal default in case their server were compromised. 我可以想象一些偏执(小心)商店可能会丢弃“postgres”并使用不同的数据库作为其内部默认值,以防他们的服务器遭到入侵。 That's kind of like using a non-default port for SSH. 这有点像使用SSH的非默认端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM