Postgresql 中文操作指南
23.2. Creating a Database #
为了创建数据库,PostgreSQL 服务器必须处于启动并运行状态(请参阅 Section 19.3)。
In order to create a database, the PostgreSQL server must be up and running (see Section 19.3).
使用 SQL 命令创建数据库:
Databases are created with the SQL command CREATE DATABASE:
CREATE DATABASE name;
其中 name 遵循 SQL 标识符的一般规则。当前角色自动成为新数据库的所有者。数据库的所有者有权在以后将其删除(这也会删除其中的所有对象,即使它们有不同的所有者)。
where name follows the usual rules for SQL identifiers. The current role automatically becomes the owner of the new database. It is the privilege of the owner of a database to remove it later (which also removes all the objects in it, even if they have a different owner).
创建数据库是一种受限操作。有关如何授予权限的信息,请参阅 Section 22.2。
The creation of databases is a restricted operation. See Section 22.2 for how to grant permission.
由于您需要连接到数据库服务器才能执行_CREATE DATABASE_命令,那么问题是如何在任何给定站点上创建_first_数据库。第一个数据库总是由_initdb_命令在数据存储区域初始化时创建。(参见 Section 19.2。)此数据库称为 postgres。因此,要创建第一个“普通”数据库,您可以连接到 postgres。
Since you need to be connected to the database server in order to execute the CREATE DATABASE command, the question remains how the first database at any given site can be created. The first database is always created by the initdb command when the data storage area is initialized. (See Section 19.2.) This database is called postgres. So to create the first “ordinary” database you can connect to postgres.
在数据库集群初始化期间,还创建了这两个附加数据库 template1 和 template0。每当在集群内创建一个新数据库时,template1 基本上会被克隆。这意味着你对 template1 所做的任何更改都会传播到随后创建的所有数据库。因此,除非你想将它们传播到每个新创建的数据库,否则避免在 template1 中创建对象。template0 的目的是作为 template1 的原始内容的原始副本。在需要创建一个不带任何此类本地站点附加内容的数据库时,可以克隆它来代替 template1。更多详细信息显示在 Section 23.3 中。
Two additional databases, template1 and template0, are also created during database cluster initialization. Whenever a new database is created within the cluster, template1 is essentially cloned. This means that any changes you make in template1 are propagated to all subsequently created databases. Because of this, avoid creating objects in template1 unless you want them propagated to every newly created database. template0 is meant as a pristine copy of the original contents of template1. It can be cloned instead of template1 when it is important to make a database without any such site-local additions. More details appear in Section 23.3.
为了方便起见,有一个程序你可以从外壳程序执行,以便创建新数据库,createdb。
As a convenience, there is a program you can execute from the shell to create new databases, createdb.
createdb dbname
createdb 不进行任何神奇操作。它连接到 postgres 数据库并按上述说明确切地发出 CREATE DATABASE 命令。 createdb 引用页包含调用详细信息。请注意,如果没有提供任何参数的 createdb 会创建具有当前用户名的数据库。
createdb does no magic. It connects to the postgres database and issues the CREATE DATABASE command, exactly as described above. The createdb reference page contains the invocation details. Note that createdb without any arguments will create a database with the current user name.
Note
Chapter 21 包含有关如何限制谁可以连接到给定数据库的信息。
Chapter 21 contains information about how to restrict who can connect to a given database.
有时候,你希望为其他人创建一个数据库,并让他们成为新数据库的所有者,以便他们能够自行配置和管理数据库。若要实现这一目标,请使用以下命令之一:
Sometimes you want to create a database for someone else, and have them become the owner of the new database, so they can configure and manage it themselves. To achieve that, use one of the following commands:
CREATE DATABASE dbname OWNER rolename;
在 SQL 环境中,或者:
from the SQL environment, or:
createdb -O rolename dbname
从 shell。只有超级用户才能为他人(即,您不属于的某个角色)创建数据库。
from the shell. Only the superuser is allowed to create a database for someone else (that is, for a role you are not a member of).