CREATE DATABASE 中文man頁面
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';

















