database Postgresql 服务器上名为 postgres 的默认数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2370525/
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-09-08 07:37:36  来源:igfitidea点击:

Default database named postgres on Postgresql server

databasepostgresql

提问by dareios

Apparently there is a database "postgres" that is created by default on each postgresql server installation. Can anyone tell me or point me to documentation what it is used for?

显然,每个 postgresql 服务器安装上都有一个默认创建的数据库“postgres”。谁能告诉我或指向我文档它的用途?

采纳答案by sleske

It appears that it does not really have a well-defined purpose. According to the docs:

它似乎并没有真正定义明确的目的。根据文档:

Creating a database cluster consists of creating the directories in which the database data will live, generating the shared catalog tables (tables that belong to the whole cluster rather than to any particular database), and creating the "template1" and "postgres" databases.

[...]

The postgres database is a default database meant for use by users, utilities and third party applications.

创建数据库集群包括创建数据库数据所在的目录、生成共享目录表(属于整个集群而不是任何特定数据库的表)以及创建“template1”和“postgres”数据库。

[...]

postgres 数据库是供用户、实用程序和第三方应用程序使用的默认数据库。

(Source: http://www.postgresql.org/docs/current/app-initdb.html)

(来源:http: //www.postgresql.org/docs/current/app-initdb.html

回答by

When a client application connects to a Postgres server, it must specify which database that it wants to connect to. If you don't know the name of a database (within the cluster serviced by the postmaster to which you connect), you can find a list of database names with the command:

当客户端应用程序连接到 Postgres 服务器时,它必须指定它想要连接到的数据库。如果您不知道数据库的名称(在您连接的 postmaster 服务的集群内),您可以使用以下命令查找数据库名称列表:

psql -l

When you run that command, psql connects to the server and queries pg_database for a list of database names. However, since psql is a Postgres client application, it can't connect to the server without knowing the name of at least one database: Catch-22. So, psql is hard-coded to connect to a database named "postgres" when you run psql -l, but you can specify a template database in that case:

当您运行该命令时,psql 连接到服务器并查询 pg_database 以获取数据库名称列表。然而,由于 psql 是一个 Postgres 客户端应用程序,它无法在不知道至少一个数据库名称的情况下连接到服务器:Catch-22。因此,当您运行 psql 时,它被硬编码为连接到名为“postgres”的数据库psql -l,但在这种情况下您可以指定一个模板数据库:

psql -l -d template1

回答by Frank Heikens

There is also the database template0, your safety net when you screw up all others.

还有数据库模板0,当你搞砸所有其他的时候,你的安全网。

  1. postgres is your default database to connect with.
  2. template1 is your default for creating new databases, these are created just like template1
  3. template0 is usefull when template1 is corrupted (wrong settings etc.) and you don't want to spend a lot of time to fix this. Just drop template1 and create a new template1 using the database template0.
  1. postgres 是您要连接的默认数据库。
  2. template1 是您创建新数据库的默认设置,它们的创建方式与 template1 一样
  3. 当模板 1 损坏(错误设置等)并且您不想花费大量时间来解决此问题时,模板 0 很有用。只需删除 template1 并使用数据库 template0 创建一个新的 template1。

回答by Mr Africa

The comment above asked: "Is it safe to delete the postgres database if you're not using it?" - CMCDragonkai Oct 22 '16 at 10:37

上面的评论问:“如果不使用 postgres 数据库,删除它是否安全?” - CMCDragonkai 16 年 10 月 22 日 10:37

From the PostgreSQL documentation

来自 PostgreSQL 文档

After initialization, a database cluster will contain a database named postgres, which is meant as a default database for use by utilities, users and third party applications. The database server itself does not require the postgres database to exist, but many external utility programs assume it exists.

初始化后,数据库集群将包含一个名为 postgres 的数据库,它是供实用程序、用户和第三方应用程序使用的默认数据库。数据库服务器本身不需要 postgres 数据库存在,但许多外部实用程序假定它存在。

[Note: A database cluster is a collection of databases that is managed by a single instance of a running database server.]

[注意:数据库集群是由正在运行的数据库服务器的单个实例管理的数据库集合。]

回答by Mikael Lepist?

If you are using multiple database connections when creating new databases, then all the connections cannot be done to template1or template0.

如果在创建新数据库时使用多个数据库连接,则无法对template1或进行所有连接template0

Postgresql will throw an error if the source DB while creating new DB is accessed by other connections.

如果在创建新数据库时源数据库被其他连接访问,Postgresql 将抛出错误。

So for creating new DBs it is better to connect postgres.

因此,对于创建新数据库,最好连接postgres.

enter image description here

在此处输入图片说明