CREATE TABLESPACE
CREATE TABLESPACE — define a new tablespace
CREATE TABLESPACE tablespace_name
[ OWNER { user_name | CURRENT_USER | SESSION_USER } ]
[ STORAGE { local | remote } ]
LOCATION 'directory'
[ WITH ( tablespace_option = value [, ... ] ) ]
CREATE TABLESPACE registers a new instance-wide tablespace. The tablespace name must be distinct from the name of any existing tablespace in the database instance.
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 UNDO, CREATE TABLE, CREATE INDEX or ADD CONSTRAINT to have the data files for these objects stored within the specified tablespace.
A tablespace cannot be used independently of the instance in which it is defined; see Section 22.6.
-
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.
-
localremoteThe type of storage device used for the tablespace. If omitted, defaults to
local.localindicates a local storage device, followed by a storage location corresponding to the directory path.remoteindicates a remote storage device, followed by a storage location corresponding to access information about storage server. -
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_costandeffective_io_concurrency. Setting either value for a particular tablespace will override the planner’s usual estimate of the cost of reading pages from tables in that tablespace, as established by the configuration parameters of the same name (see seq_page_cost, random_page_cost, effective_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.
CREATE TABLESPACE cannot be executed inside a transaction block.
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';
CREATE TABLESPACE is a PostgreSQL extension.