sqlpostgresqltablespace

How to set tablespace for a specific CREATE TABLE [Table] AS


How to set tablespace for a specific table creation AS clause?, like:

CREATE TABLE [New Table] AS 
TABLE [Old Table] 
--WITH NO DATA
;

In which tablespace new table with/without data is created for this specific 'AS' clause?

In official documentation, have not found such case: https://www.postgresql.org/docs/current/sql-createtable.html , though it says:

Tablespaces

The PostgreSQL concept of tablespaces is not part of the standard. Hence, the clauses TABLESPACE and USING INDEX TABLESPACE are extensions.

So, please help me out, what is default tablespace will be used in such "AS" creation case of the table, and how to specify custom table space for this. By ALTER -ing created table?, like only:

ALTER TABLE [New Table] 
SET TABLESPACE [Existing Tablespace];

Solution

  • As documented in the manual you can append the tablespace option to a CREATE TABLE AS statement:

    CREATE TABLE new_table 
    TABLESPACE custom_tablespace
    AS 
    TABLE old_table
    WITH NO DATA;