Postgresql 中文操作指南

CREATE TABLESPACE

CREATE TABLESPACE — 定义一个新表空间

CREATE TABLESPACE — define a new tablespace

Synopsis

CREATE TABLESPACE tablespace_name
    [ OWNER { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER } ]
    LOCATION 'directory'
    [ WITH ( tablespace_option = value [, ... ] ) ]

Description

CREATE TABLESPACE 注册一个新的集群范围表空间。表空间名称必须不同于数据库集群中的任何现有表空间的名称。

CREATE TABLESPACE registers a new cluster-wide tablespace. The tablespace name must be distinct from the name of any existing tablespace in the database cluster.

一个表空间允许超级用户在文件系统上定义一个替代位置,其中可以容纳包含数据库对象(比如表和索引)的数据文件。

A tablespace allows superusers to define an alternative location on the file system where the data files containing database objects (such as tables and indexes) can reside.

拥有适当权限的用户可以将 tablespace_name 传递给 CREATE DATABASECREATE TABLECREATE INDEXADD CONSTRAINT ,以便将这些对象的数据文件存储在指定表空间内。

A user with appropriate privileges can pass tablespace_name to CREATE DATABASE, CREATE TABLE, CREATE INDEX or ADD CONSTRAINT to have the data files for these objects stored within the specified tablespace.

Warning

一个表空间不能独立于定义它的集群使用;请参见 Section 23.6

A tablespace cannot be used independently of the cluster in which it is defined; see Section 23.6.

Parameters

  • tablespace_name

    • The name of a tablespace to be created. The name cannot begin with pg_, as such names are reserved for system tablespaces.

  • user_name

    • The name of the user who will own the tablespace. If omitted, defaults to the user executing the command. Only superusers can create tablespaces, but they can assign ownership of tablespaces to non-superusers.

  • directory

    • The directory that will be used for the tablespace. The directory must exist (CREATE TABLESPACE will not create it), should be empty, and must be owned by the PostgreSQL system user. The directory must be specified by an absolute path name.

  • tablespace_option

    • A tablespace parameter to be set or reset. Currently, the only available parameters are seq_page_cost, random_page_cost, effective_io_concurrency and maintenance_io_concurrency. Setting these values for a particular tablespace will override the planner’s usual estimate of the cost of reading pages from tables in that tablespace, and the executor’s prefetching behavior, as established by the configuration parameters of the same name (see seq_page_cost, random_page_cost, effective_io_concurrency, maintenance_io_concurrency). This may be useful if one tablespace is located on a disk which is faster or slower than the remainder of the I/O subsystem.

Notes

CREATE TABLESPACE 无法在事务块内执行。

CREATE TABLESPACE cannot be executed inside a transaction block.

Examples

要在文件系统位置 /data/dbs 处创建一个表空间 dbspace ,首先使用操作系统工具创建目录并设置正确的所有权:

To create a tablespace dbspace at file system location /data/dbs, first create the directory using operating system facilities and set the correct ownership:

mkdir /data/dbs
chown postgres:postgres /data/dbs

然后在 PostgreSQL 内发出表空间创建命令:

Then issue the tablespace creation command inside PostgreSQL:

CREATE TABLESPACE dbspace LOCATION '/data/dbs';

要创建一个属于其他数据库用户的表空间,请使用以下命令:

To create a tablespace owned by a different database user, use a command like this:

CREATE TABLESPACE indexspace OWNER genevieve LOCATION '/data/indexes';

Compatibility

CREATE TABLESPACE 是 PostgreSQL 扩展。

CREATE TABLESPACE is a PostgreSQL extension.