CREATE TABLESPACE
define a new tablespace
English manual coverage begins with PostgreSQL 6.4. Source history and runtime observations have their own recorded version boundaries.
Reading PG 18·Select a recorded version
- anchor
- SQL-CREATETABLESPACE
- file
- sql-createtablespace.html
- lang
- en
- name
- CREATE TABLESPACE
- purpose
- define a new tablespace
- purpose zh
- 定义一个新表空间
- sections same as
- Unknown
- slug
- 18
- synopsis text
- CREATE TABLESPACE tablespace_name [ OWNER { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER } ] LOCATION 'directory' [ WITH ( tablespace_option = value [, ... ] ) ]
Syntax
CREATE TABLESPACE tablespace_name
[ OWNER { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER } ]
LOCATION 'directory'
[ WITH ( tablespace_option = value [, ... ] ) ]Syntax diagrams
Read left to right. Branches are choices, bypasses are optional, loops repeat. Select an underlined parameter to open its rule.
Definition PG 18
CREATE TABLESPACE tablespace_name
[ OWNER { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER } ]
LOCATION 'directory'
[ WITH ( tablespace_option = value [, ... ] ) ]Description
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.
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
A tablespace cannot be used independently of the cluster in which it is defined; see Section 22.6.
Parameters
tablespace_nameThe name of a tablespace to be created. The name cannot begin with
pg_, as such names are reserved for system tablespaces.user_nameThe 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.
directoryThe directory that will be used for the tablespace. The directory must exist (
CREATE TABLESPACEwill 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_optionA tablespace parameter to be set or reset. Currently, the only available parameters are
seq_page_cost,random_page_cost,effective_io_concurrencyandmaintenance_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 how many concurrent I/Os are issued, 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 cannot be executed inside a transaction block.
Examples
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
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 is a PostgreSQL extension.
See Also
CREATE DATABASE, CREATE TABLE, CREATE INDEX, DROP TABLESPACE, ALTER TABLESPACECompare versions
Complete source facts
synopsis html
CREATE TABLESPACE <em class="replaceable"><code>tablespace_name</code></em>
[ OWNER { <em class="replaceable"><code>new_owner</code></em> | CURRENT_ROLE | CURRENT_USER | SESSION_USER } ]
LOCATION '<em class="replaceable"><code>directory</code></em>'
[ WITH ( <em class="replaceable"><code>tablespace_option</code></em> = <em class="replaceable"><code>value</code></em> [, ... ] ) ]Source references
Full definition and evidence JSON
Definition sources
center · PostgreSQL 18 · wiki-repair-20261023:28fee5401fc0c76b5645acee426fbfc9d975a5766f0c1429d2fd2ba7c744bc37
Text language: en · wiki-repair-20261023:28fee5401fc0c76b5645acee426fbfc9d975a5766f0c1429d2fd2ba7c744bc37