{
    "content": [
        {
            "type": "text",
            "text": "# CREATE_VIEW (man)\n\n## NAME\n\nCREATEVIEW - define a new view\n\n## SYNOPSIS\n\nCREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] VIEW name [ ( columnname [, ...] ) ]\n[ WITH ( viewoptionname [= viewoptionvalue] [, ... ] ) ]\nAS query\n[ WITH [ CASCADED | LOCAL ] CHECK OPTION ]\n\n## DESCRIPTION\n\nCREATE VIEW defines a view of a query. The view is not physically materialized. Instead, the\nquery is run every time the view is referenced in a query.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **PARAMETERS**\n- **NOTES** (1 subsections)\n- **EXAMPLES**\n- **COMPATIBILITY**\n- **SEE ALSO**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "CREATE_VIEW",
        "section": "",
        "mode": "man",
        "summary": "CREATEVIEW - define a new view",
        "synopsis": "CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] VIEW name [ ( columnname [, ...] ) ]\n[ WITH ( viewoptionname [= viewoptionvalue] [, ... ] ) ]\nAS query\n[ WITH [ CASCADED | LOCAL ] CHECK OPTION ]",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [
            "Create a view consisting of all comedy films:",
            "CREATE VIEW comedies AS",
            "SELECT *",
            "FROM films",
            "WHERE kind = 'Comedy';",
            "This will create a view containing the columns that are in the film table at the time of view",
            "creation. Though * was used to create the view, columns added later to the table will not be",
            "part of the view.",
            "Create a view with LOCAL CHECK OPTION:",
            "CREATE VIEW universalcomedies AS",
            "SELECT *",
            "FROM comedies",
            "WHERE classification = 'U'",
            "WITH LOCAL CHECK OPTION;",
            "This will create a view based on the comedies view, showing only films with kind = 'Comedy'",
            "and classification = 'U'. Any attempt to INSERT or UPDATE a row in the view will be rejected",
            "if the new row doesn't have classification = 'U', but the film kind will not be checked.",
            "Create a view with CASCADED CHECK OPTION:",
            "CREATE VIEW pgcomedies AS",
            "SELECT *",
            "FROM comedies",
            "WHERE classification = 'PG'",
            "WITH CASCADED CHECK OPTION;",
            "This will create a view that checks both the kind and classification of new rows.",
            "Create a view with a mix of updatable and non-updatable columns:",
            "CREATE VIEW comedies AS",
            "SELECT f.*,",
            "countrycodetoname(f.countrycode) AS country,",
            "(SELECT avg(r.rating)",
            "FROM userratings r",
            "WHERE r.filmid = f.id) AS avgrating",
            "FROM films f",
            "WHERE f.kind = 'Comedy';",
            "This view will support INSERT, UPDATE and DELETE. All the columns from the films table will",
            "be updatable, whereas the computed columns country and avgrating will be read-only.",
            "Create a recursive view consisting of the numbers from 1 to 100:",
            "CREATE RECURSIVE VIEW public.nums1100 (n) AS",
            "VALUES (1)",
            "UNION ALL",
            "SELECT n+1 FROM nums1100 WHERE n < 100;",
            "Notice that although the recursive view's name is schema-qualified in this CREATE, its",
            "internal self-reference is not schema-qualified. This is because the implicitly-created CTE's",
            "name cannot be schema-qualified."
        ],
        "see_also": [
            {
                "name": "ALTERVIEW",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/ALTERVIEW/7/json"
            },
            {
                "name": "DROPVIEW",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/DROPVIEW/7/json"
            },
            {
                "name": "CREATEMATERIALIZEDVIEW",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/CREATEMATERIALIZEDVIEW/7/json"
            },
            {
                "name": "VIEW",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/VIEW/7/json"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 15,
                "subsections": []
            },
            {
                "name": "PARAMETERS",
                "lines": 76,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 25,
                "subsections": [
                    {
                        "name": "Updatable Views",
                        "lines": 54
                    }
                ]
            },
            {
                "name": "EXAMPLES",
                "lines": 58,
                "subsections": []
            },
            {
                "name": "COMPATIBILITY",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 6,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "CREATEVIEW - define a new view\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] VIEW name [ ( columnname [, ...] ) ]\n[ WITH ( viewoptionname [= viewoptionvalue] [, ... ] ) ]\nAS query\n[ WITH [ CASCADED | LOCAL ] CHECK OPTION ]\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "CREATE VIEW defines a view of a query. The view is not physically materialized. Instead, the\nquery is run every time the view is referenced in a query.\n\nCREATE OR REPLACE VIEW is similar, but if a view of the same name already exists, it is\nreplaced. The new query must generate the same columns that were generated by the existing\nview query (that is, the same column names in the same order and with the same data types),\nbut it may add additional columns to the end of the list. The calculations giving rise to the\noutput columns may be completely different.\n\nIf a schema name is given (for example, CREATE VIEW myschema.myview ...) then the view is\ncreated in the specified schema. Otherwise it is created in the current schema. Temporary\nviews exist in a special schema, so a schema name cannot be given when creating a temporary\nview. The name of the view must be distinct from the name of any other view, table, sequence,\nindex or foreign table in the same schema.\n",
                "subsections": []
            },
            "PARAMETERS": {
                "content": "TEMPORARY or TEMP\nIf specified, the view is created as a temporary view. Temporary views are automatically\ndropped at the end of the current session. Existing permanent relations with the same\nname are not visible to the current session while the temporary view exists, unless they\nare referenced with schema-qualified names.\n\nIf any of the tables referenced by the view are temporary, the view is created as a\ntemporary view (whether TEMPORARY is specified or not).\n\nRECURSIVE\nCreates a recursive view. The syntax\n\nCREATE RECURSIVE VIEW [ schema . ] viewname (columnnames) AS SELECT ...;\n\nis equivalent to\n\nCREATE VIEW [ schema . ] viewname AS WITH RECURSIVE viewname (columnnames) AS (SELECT ...) SELECT columnnames FROM viewname;\n\nA view column name list must be specified for a recursive view.\n\nname\nThe name (optionally schema-qualified) of a view to be created.\n\ncolumnname\nAn optional list of names to be used for columns of the view. If not given, the column\nnames are deduced from the query.\n\nWITH ( viewoptionname [= viewoptionvalue] [, ... ] )\nThis clause specifies optional parameters for a view; the following parameters are\nsupported:\n\ncheckoption (enum)\nThis parameter may be either local or cascaded, and is equivalent to specifying WITH\n[ CASCADED | LOCAL ] CHECK OPTION (see below). This option can be changed on existing\nviews using ALTER VIEW.\n\nsecuritybarrier (boolean)\nThis should be used if the view is intended to provide row-level security. See\nSection 41.5 for full details.\n\nquery\nA SELECT or VALUES command which will provide the columns and rows of the view.\n\nWITH [ CASCADED | LOCAL ] CHECK OPTION\nThis option controls the behavior of automatically updatable views. When this option is\nspecified, INSERT and UPDATE commands on the view will be checked to ensure that new rows\nsatisfy the view-defining condition (that is, the new rows are checked to ensure that\nthey are visible through the view). If they are not, the update will be rejected. If the\nCHECK OPTION is not specified, INSERT and UPDATE commands on the view are allowed to\ncreate rows that are not visible through the view. The following check options are\nsupported:\n\nLOCAL\nNew rows are only checked against the conditions defined directly in the view itself.\nAny conditions defined on underlying base views are not checked (unless they also\nspecify the CHECK OPTION).\n\nCASCADED\nNew rows are checked against the conditions of the view and all underlying base\nviews. If the CHECK OPTION is specified, and neither LOCAL nor CASCADED is specified,\nthen CASCADED is assumed.\n\nThe CHECK OPTION may not be used with RECURSIVE views.\n\nNote that the CHECK OPTION is only supported on views that are automatically updatable,\nand do not have INSTEAD OF triggers or INSTEAD rules. If an automatically updatable view\nis defined on top of a base view that has INSTEAD OF triggers, then the LOCAL CHECK\nOPTION may be used to check the conditions on the automatically updatable view, but the\nconditions on the base view with INSTEAD OF triggers will not be checked (a cascaded\ncheck option will not cascade down to a trigger-updatable view, and any check options\ndefined directly on a trigger-updatable view will be ignored). If the view or any of its\nbase relations has an INSTEAD rule that causes the INSERT or UPDATE command to be\nrewritten, then all check options will be ignored in the rewritten query, including any\nchecks from automatically updatable views defined on top of the relation with the INSTEAD\nrule.\n",
                "subsections": []
            },
            "NOTES": {
                "content": "Use the DROP VIEW statement to drop views.\n\nBe careful that the names and types of the view's columns will be assigned the way you want.\nFor example:\n\nCREATE VIEW vista AS SELECT 'Hello World';\n\nis bad form because the column name defaults to ?column?; also, the column data type defaults\nto text, which might not be what you wanted. Better style for a string literal in a view's\nresult is something like:\n\nCREATE VIEW vista AS SELECT text 'Hello World' AS hello;\n\nAccess to tables referenced in the view is determined by permissions of the view owner. In\nsome cases, this can be used to provide secure but restricted access to the underlying\ntables. However, not all views are secure against tampering; see Section 41.5 for details.\nFunctions called in the view are treated the same as if they had been called directly from\nthe query using the view. Therefore the user of a view must have permissions to call all\nfunctions used by the view.\n\nWhen CREATE OR REPLACE VIEW is used on an existing view, only the view's defining SELECT rule\nis changed. Other view properties, including ownership, permissions, and non-SELECT rules,\nremain unchanged. You must own the view to replace it (this includes being a member of the\nowning role).\n",
                "subsections": [
                    {
                        "name": "Updatable Views",
                        "content": "Simple views are automatically updatable: the system will allow INSERT, UPDATE and DELETE\nstatements to be used on the view in the same way as on a regular table. A view is\nautomatically updatable if it satisfies all of the following conditions:\n\n•   The view must have exactly one entry in its FROM list, which must be a table or another\nupdatable view.\n\n•   The view definition must not contain WITH, DISTINCT, GROUP BY, HAVING, LIMIT, or OFFSET\nclauses at the top level.\n\n•   The view definition must not contain set operations (UNION, INTERSECT or EXCEPT) at the\ntop level.\n\n•   The view's select list must not contain any aggregates, window functions or set-returning\nfunctions.\n\nAn automatically updatable view may contain a mix of updatable and non-updatable columns. A\ncolumn is updatable if it is a simple reference to an updatable column of the underlying base\nrelation; otherwise the column is read-only, and an error will be raised if an INSERT or\nUPDATE statement attempts to assign a value to it.\n\nIf the view is automatically updatable the system will convert any INSERT, UPDATE or DELETE\nstatement on the view into the corresponding statement on the underlying base relation.\nINSERT statements that have an ON CONFLICT UPDATE clause are fully supported.\n\nIf an automatically updatable view contains a WHERE condition, the condition restricts which\nrows of the base relation are available to be modified by UPDATE and DELETE statements on the\nview. However, an UPDATE is allowed to change a row so that it no longer satisfies the WHERE\ncondition, and thus is no longer visible through the view. Similarly, an INSERT command can\npotentially insert base-relation rows that do not satisfy the WHERE condition and thus are\nnot visible through the view (ON CONFLICT UPDATE may similarly affect an existing row not\nvisible through the view). The CHECK OPTION may be used to prevent INSERT and UPDATE commands\nfrom creating such rows that are not visible through the view.\n\nIf an automatically updatable view is marked with the securitybarrier property then all the\nview's WHERE conditions (and any conditions using operators which are marked as LEAKPROOF)\nwill always be evaluated before any conditions that a user of the view has added. See\nSection 41.5 for full details. Note that, due to this, rows which are not ultimately returned\n(because they do not pass the user's WHERE conditions) may still end up being locked.\nEXPLAIN can be used to see which conditions are applied at the relation level (and therefore\ndo not lock rows) and which are not.\n\nA more complex view that does not satisfy all these conditions is read-only by default: the\nsystem will not allow an insert, update, or delete on the view. You can get the effect of an\nupdatable view by creating INSTEAD OF triggers on the view, which must convert attempted\ninserts, etc. on the view into appropriate actions on other tables. For more information see\nCREATE TRIGGER (CREATETRIGGER(7)). Another possibility is to create rules (see CREATE RULE\n(CREATERULE(7))), but in practice triggers are easier to understand and use correctly.\n\nNote that the user performing the insert, update or delete on the view must have the\ncorresponding insert, update or delete privilege on the view. In addition the view's owner\nmust have the relevant privileges on the underlying base relations, but the user performing\nthe update does not need any permissions on the underlying base relations (see Section 41.5).\n"
                    }
                ]
            },
            "EXAMPLES": {
                "content": "Create a view consisting of all comedy films:\n\nCREATE VIEW comedies AS\nSELECT *\nFROM films\nWHERE kind = 'Comedy';\n\nThis will create a view containing the columns that are in the film table at the time of view\ncreation. Though * was used to create the view, columns added later to the table will not be\npart of the view.\n\nCreate a view with LOCAL CHECK OPTION:\n\nCREATE VIEW universalcomedies AS\nSELECT *\nFROM comedies\nWHERE classification = 'U'\nWITH LOCAL CHECK OPTION;\n\nThis will create a view based on the comedies view, showing only films with kind = 'Comedy'\nand classification = 'U'. Any attempt to INSERT or UPDATE a row in the view will be rejected\nif the new row doesn't have classification = 'U', but the film kind will not be checked.\n\nCreate a view with CASCADED CHECK OPTION:\n\nCREATE VIEW pgcomedies AS\nSELECT *\nFROM comedies\nWHERE classification = 'PG'\nWITH CASCADED CHECK OPTION;\n\nThis will create a view that checks both the kind and classification of new rows.\n\nCreate a view with a mix of updatable and non-updatable columns:\n\nCREATE VIEW comedies AS\nSELECT f.*,\ncountrycodetoname(f.countrycode) AS country,\n(SELECT avg(r.rating)\nFROM userratings r\nWHERE r.filmid = f.id) AS avgrating\nFROM films f\nWHERE f.kind = 'Comedy';\n\nThis view will support INSERT, UPDATE and DELETE. All the columns from the films table will\nbe updatable, whereas the computed columns country and avgrating will be read-only.\n\nCreate a recursive view consisting of the numbers from 1 to 100:\n\nCREATE RECURSIVE VIEW public.nums1100 (n) AS\nVALUES (1)\nUNION ALL\nSELECT n+1 FROM nums1100 WHERE n < 100;\n\nNotice that although the recursive view's name is schema-qualified in this CREATE, its\ninternal self-reference is not schema-qualified. This is because the implicitly-created CTE's\nname cannot be schema-qualified.\n",
                "subsections": []
            },
            "COMPATIBILITY": {
                "content": "CREATE OR REPLACE VIEW is a PostgreSQL language extension. So is the concept of a temporary\nview. The WITH ( ... ) clause is an extension as well.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "ALTER VIEW (ALTERVIEW(7)), DROP VIEW (DROPVIEW(7)), CREATE MATERIALIZED VIEW\n(CREATEMATERIALIZEDVIEW(7))\n\n\n\nPostgreSQL 14.23                                2026                                  CREATE VIEW(7)",
                "subsections": []
            }
        }
    }
}