NAME
CREATE DATABASE - 创建新数据库
SYNOPSIS
CREATE DATABASE name [ [ WITH ] [ OWNER [=] dbowner ] [ LOCATION [=] 'dbpath' ] [ TEMPLATE [=] template ] [ ENCODING [=] encoding ] ]
DESCRIPTION 描述
CREATE DATABASE 创建一个新的 PostgreSQL 数据库。
要创建一个数据库,你必须是一个超级用户或者有特殊的 CREATEDB 权限。 参阅 CREATE USER [create_user(7)]。
通常,创建者成为新数据库的管理员。 超级用户可以用 OWNER 子句创建其它用户所有的数据库。 他们甚至可以创建没有特殊权限的用户所有的数据库。 有CREATEDB权限的非超级用户使用只能创建自己使用的数据库。
可以声明一个可选的数据库位置,例如,在另一块硬盘上存放数据库。 该路径必须是事先用 initlocation[initlocation(1)] 命令准备好了的。
如果路径名不包含斜杠,那么它被解释成一个环境变量, 该变量必须为服务进程所知。这样数据库管理员 可以对能够在那里创建数据库进行控制。(例如,一个用户化的选择是 'PGDATA2'。)如果服务器带着 ALLOW_ABSOLUTE_DBPATHS (缺省时没有)选项编译, 那么也允许使用以斜杠开头为标识的绝对路径(例如, ' '/usr/local/pgsql/data')。 In either case, the final path name must be absolute and must not contain any single quotes.
缺省时,新数据库将通过克隆标准系统数据库 template1 来创建。不同的模板可以用 TEMPLATE = name 来写。尤其是,如果你用 TEMPLATE = template0, 你可以创建一个很纯净的数据库,只包括你的版本的 PostgreSQL 预定义的 标准对象。这个方法可以避免把任何已经加入到template1 里的本地安装对象拷贝到新数据库。
可选的编码参数允许选择数据库编码, 如果没有声明,缺省是所选用的模板数据库用的编码。
PARAMETERS 参数
- name
要创建的数据库名。- dbowner
数据库用户的名字,他将拥有新数据库,或者是写 DEFAULT 使用缺省的(也就是执行命令的用户)。- dbpath
在文件系统里存储新数据库的可选位置;用字串文本声明。 或者用 DEFAULT 表示使用缺省位置。- template
从哪个模板创建新数据库,这是模板名。或者用 DEFAULT 使用缺省模板(template1)。- encoding
创建新数据库用的多字节编码方法。声明一个字串文本名字 (比如,'SQL_ASCII'), 或者一个整数编号,或者是 DEFAULT 表示使用缺省编码。
可选参数可以以任意顺序写,而不仅是上面显示的顺序。
NOTES 注意
CREATE DATABASE 不能在一个事务块里面执行。 block.
类似 "could not initialize database directory" 这样的错误最有可能是因为数据目录的权限不够, 或者磁盘满,或者其它文件系统的问题。在使用可选的位置的时候,运行数据库服务器的用户必须有访问该位置的权限。
使用 DROP DATABASE [drop_database(7)] 删除一个数据库。
程序 createdb[createdb(1)] 是是这个命令的封装,提供来方便使用。
在用绝对路径指定的可选数据库位置时,有一些安全和数据完整性的问题, 这就是为什么缺省时没有打开这个特性的原因。 参考 ``Managing Databases'' 获取更多的信息。
尽管我们可以通过把某数据库名声明为模板(而非 template1)从非template1数据库拷贝数据库, 但是这(还)不是一个通用的 "COPY DATABASE" 功能。 因此,我们建议当做模板使用的数据库都应该是以只读方式对待的。 参阅 ``Managing Databases'' 获取更多信息。
EXAMPLES 例子
创建一个新的数据库:
CREATE DATABASE lusiadas;
在另一个地方 ~/private_db创建新数据库, 在 shell 里执行下面的东西: shell:
mkdir private_db initlocation ~/private_db
然后在一个 psql 会话里执行下面的东西:
CREATE DATABASE elsewhere WITH LOCATION '/home/olly/private_db';
#p#
NAME
CREATE DATABASE - create a new database
SYNOPSIS
CREATE DATABASE name [ [ WITH ] [ OWNER [=] dbowner ] [ LOCATION [=] 'dbpath' ] [ TEMPLATE [=] template ] [ ENCODING [=] encoding ] ]
DESCRIPTION
CREATE DATABASE creates a new PostgreSQL database.
To create a database, you must be a superuser or have the special CREATEDB privilege. See CREATE USER [create_user(7)].
Normally, the creator becomes the owner of the new database. Superusers can create databases owned by other users using the OWNER clause. They can even create databases owned by users with no special privileges. Non-superusers with CREATEDB privilege can only create databases owned by themselves.
An alternative location can be specified in order to, for example, store the database on a different disk. The path must have been prepared with the initlocation [initlocation(1)] command.
If the path name does not contain a slash, it is interpreted as an environment variable name, which must be known to the server process. This way the database administrator can exercise control over locations in which databases can be created. (A customary choice is, e.g., PGDATA2.) If the server is compiled with ALLOW_ABSOLUTE_DBPATHS (not so by default), absolute path names, as identified by a leading slash (e.g., /usr/local/pgsql/data), are allowed as well. In either case, the final path name must be absolute and must not contain any single quotes.
By default, the new database will be created by cloning the standard system database template1. A different template can be specified by writing TEMPLATE name. In particular, by writing TEMPLATE template0, you can create a virgin database containing only the standard objects predefined by your version of PostgreSQL. This is useful if you wish to avoid copying any installation-local objects that may have been added to template1.
The optional encoding parameter allows selection of the database encoding. When not specified, it defaults to the encoding used by the selected template database.
PARAMETERS
- name
- The name of a database to create.
- dbowner
- The name of the database user who will own the new database, or DEFAULT to use the default (namely, the user executing the command).
- dbpath
- An alternate file-system location in which to store the new database, specified as a string literal; or DEFAULT to use the default location.
- template
- The name of the template from which to create the new database, or DEFAULT to use the default template (template1).
- encoding
- Character set encoding to use in the new database. Specify a string constant (e.g., 'SQL_ASCII'), or an integer encoding number, or DEFAULT to use the default encoding.
Optional parameters can be written in any order, not only the order illustrated above.
NOTES
CREATE DATABASE cannot be executed inside a transaction block.
Errors along the line of ``could not initialize database directory'' are most likely related to insufficient permissions on the data directory, a full disk, or other file system problems. When using an alternate location, the user under which the database server is running must have access to the location.
Use DROP DATABASE [drop_database(7)] to remove a database.
The program createdb [createdb(1)] is a wrapper program around this command, provided for convenience.
There are security issues involved with using alternate database locations specified with absolute path names; this is why the feature is not enabled by default. See the chapter called ``Managing Databases'' in the documentation for more information.
Although it is possible to copy a database other than template1 by specifying its name as the template, this is not (yet) intended as a general-purpose ``COPY DATABASE'' facility. We recommend that databases used as templates be treated as read-only. See the chapter called ``Managing Databases'' in the documentation for more information.
EXAMPLES
To create a new database:
CREATE DATABASE lusiadas;
To create a new database in an alternate area ~/private_db, execute the following from the shell:
mkdir private_db initlocation ~/private_db
Then execute the following from within a psql session:
CREATE DATABASE elsewhere WITH LOCATION '/home/olly/private_db';