Skip to content

PostgreSQL TABLESPACE

    CREATE TABLESPACE

    CREATE TABLESPACE — define a new tablespace

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

    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 DATABASECREATE TABLECREATE INDEX or ADD CONSTRAINT to have the data files for these objects stored within the specified tablespace.

    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_costrandom_page_costeffective_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. 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.

    Tablespaces are only supported on systems that support symbolic links.

    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';

    ALTER TABLESPACE

    ALTER TABLESPACE — change the definition of a tablespace

    ALTER TABLESPACE name RENAME TO new_name
    ALTER TABLESPACE name OWNER TO { new_owner | CURRENT_USER | SESSION_USER }
    ALTER TABLESPACE name SET ( tablespace_option = value [, ... ] )
    ALTER TABLESPACE name RESET ( tablespace_option [, ... ] )

    ALTER TABLESPACE can be used to change the definition of a tablespace.

    You must own the tablespace to change the definition of a tablespace. To alter the owner, you must also be a direct or indirect member of the new owning role. (Note that superusers have these privileges automatically.)

    Parameters

    name

    The name of an existing tablespace.

    new_name

    The new name of the tablespace. The new name cannot begin with pg_, as such names are reserved for system tablespaces.

    new_owner

    The new owner of the tablespace.

    tablespace_option

    A tablespace parameter to be set or reset. Currently, the only available parameters are seq_page_costrandom_page_costeffective_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. 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.

    Examples

    Rename tablespace index_space to fast_raid:

    ALTER TABLESPACE index_space RENAME TO fast_raid;

    Change the owner of tablespace index_space:

    ALTER TABLESPACE index_space OWNER TO mary;

    DROP TABLESPACE

    DROP TABLESPACE — remove a tablespace

    DROP TABLESPACE [ IF EXISTS ] name

    DROP TABLESPACE removes a tablespace from the system.

    A tablespace can only be dropped by its owner or a superuser. The tablespace must be empty of all database objects before it can be dropped. It is possible that objects in other databases might still reside in the tablespace even if no objects in the current database are using the tablespace. Also, if the tablespace is listed in the temp_tablespaces setting of any active session, the DROP might fail due to temporary files residing in the tablespace.

    Parameters

    IF EXISTS

    Do not throw an error if the tablespace does not exist. A notice is issued in this case.

    name

    The name of a tablespace.

    DROP TABLESPACE cannot be executed inside a transaction block.

    Examples

    To remove tablespace mystuff from the system:

    DROP TABLESPACE mystuff;