From d838ee9701fc36f1967819bbf8b5bfdf1bf552d8 Mon Sep 17 00:00:00 2001 From: Matheus Alcantara Date: Wed, 11 Feb 2026 15:35:25 -0300 Subject: [PATCH v3 4/4] Add documentation for CREATE SCHEMA ... LIKE --- doc/src/sgml/ref/create_schema.sgml | 92 ++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 2 deletions(-) diff --git a/doc/src/sgml/ref/create_schema.sgml b/doc/src/sgml/ref/create_schema.sgml index ed69298ccc6..3b9bad643d1 100644 --- a/doc/src/sgml/ref/create_schema.sgml +++ b/doc/src/sgml/ref/create_schema.sgml @@ -22,6 +22,7 @@ PostgreSQL documentation CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ] +CREATE SCHEMA schema_name [ IF NOT EXISTS ] LIKE source_schema [ like_option [ ... ] ] CREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ] CREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ] CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification @@ -32,6 +33,10 @@ CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_sp | CURRENT_ROLE | CURRENT_USER | SESSION_USER + +and like_option can be: + + { INCLUDING | EXCLUDING } { TABLE | INDEX | ALL } @@ -120,6 +125,87 @@ CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_sp + + + LIKE source_schema [ like_option ... ] + + + The LIKE clause specifies a schema from which the new + schema automatically copies the definitions of all supported objects + (such as tables and indexes). + + + + The new schema and the original schema are completely decoupled after + creation is complete. Changes to objects in the original schema will + not be applied to the new schema, and changes to the new schema will + not affect the original. + + + + Only objects that are self-contained within the source schema or + reference other objects within that same schema are copied. Any object + that references an object located in a different schema will be + skipped. For example, a foreign key referencing a table in a + different schema, or an index on a table located elsewhere, will + not be created in the new schema. + + + + The optional like_option clauses specify + which additional properties of the original schema's objects to copy. + Specifying INCLUDING copies the object, while + specifying EXCLUDING omits the object. + EXCLUDING is the default. If multiple + specifications are made for the same kind of object, the last one + is used. The available options are: + + + + + INCLUDING TABLE + + + Definitions of tables, partitioned tables, and their associated + sequences for identity columns are copied to the new schema. + Foreign keys are re-created in the new schema only if the + referenced table is also part of the source schema. + + + Partitioned tables are supported, provided that the parent + table and all of its partitions are defined within the source + schema. If a partition or its parent table resides in a + different schema, that specific object will be skipped. + + + Note that for SERIAL data types, the sequence is + shared with the source schema; a new sequence is not created + for the new table, meaning the new table's column will default + to the original sequence. + + + + + + INCLUDING INDEX + + + Indexes from the source schema will be created on the new tables, + provided the index does not reference objects in a different + schema. This option only takes effect if + INCLUDING TABLE is also active. If tables + are excluded (either by default or explicitly), indexes will + not be created, as they have no parent object to attach to. + For example, CREATE SCHEMA foo LIKE bar INCLUDING INDEX + or CREATE SCHEMA foo LIKE bar INCLUDING ALL EXCLUDING TABLE + will result in no indexes being created. + + + + + + + @@ -128,8 +214,10 @@ CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_sp To create a schema, the invoking user must have the - CREATE privilege for the current database. - (Of course, superusers bypass this check.) + CREATE privilege for the current database. For + LIKE the user also must have the USAGE + privilege on the source schema. + (Of course, superusers bypass these checks.) -- 2.52.0