{
    "content": [
        {
            "type": "text",
            "text": "# INSERT(7) (man)\n\n**Summary:** INSERT - create new rows in a table\n\n**Synopsis:** [ WITH [ RECURSIVE ] withquery [, ...] ]\nINSERT INTO tablename [ AS alias ] [ ( columnname [, ...] ) ]\n[ OVERRIDING { SYSTEM | USER } VALUE ]\n{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }\n[ ON CONFLICT [ conflicttarget ] conflictaction ]\n[ RETURNING { * | outputexpression [ [ AS ] outputname ] } [, ...] ]\nwhere conflicttarget can be one of:\n( { indexcolumnname | ( indexexpression ) } [ COLLATE collation ] [ opclass ] [, ...] ) [ WHERE indexpredicate ]\nON CONSTRAINT constraintname\nand conflictaction is one of:\nDO NOTHING\nDO UPDATE SET { columnname = { expression | DEFAULT } |\n( columnname [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) |\n( columnname [, ...] ) = ( sub-SELECT )\n} [, ...]\n[ WHERE condition ]\n\n## Examples\n\n- `Insert a single row into table films:`\n- `INSERT INTO films VALUES`\n- `('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');`\n- `In this example, the len column is omitted and therefore it will have the default value:`\n- `INSERT INTO films (code, title, did, dateprod, kind)`\n- `VALUES ('T601', 'Yojimbo', 106, '1961-06-16', 'Drama');`\n- `This example uses the DEFAULT clause for the date columns rather than specifying a value:`\n- `INSERT INTO films VALUES`\n- `('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes');`\n- `INSERT INTO films (code, title, did, dateprod, kind)`\n- `VALUES ('T601', 'Yojimbo', 106, DEFAULT, 'Drama');`\n- `To insert a row consisting entirely of default values:`\n- `INSERT INTO films DEFAULT VALUES;`\n- `To insert multiple rows using the multirow VALUES syntax:`\n- `INSERT INTO films (code, title, did, dateprod, kind) VALUES`\n- `('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),`\n- `('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');`\n- `This example inserts some rows into table films from a table tmpfilms with the same column`\n- `layout as films:`\n- `INSERT INTO films SELECT * FROM tmpfilms WHERE dateprod < '2004-05-07';`\n- `This example inserts into array columns:`\n- `-- Create an empty 3x3 gameboard for noughts-and-crosses`\n- `INSERT INTO tictactoe (game, board[1:3][1:3])`\n- `VALUES (1, '{{\" \",\" \",\" \"},{\" \",\" \",\" \"},{\" \",\" \",\" \"}}');`\n- `-- The subscripts in the above example aren't really needed`\n- `INSERT INTO tictactoe (game, board)`\n- `VALUES (2, '{{X,\" \",\" \"},{\" \",O,\" \"},{\" \",X,\" \"}}');`\n- `Insert a single row into table distributors, returning the sequence number generated by the`\n- `DEFAULT clause:`\n- `INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')`\n- `RETURNING did;`\n- `Increment the sales count of the salesperson who manages the account for Acme Corporation,`\n- `and record the whole updated row along with current time in a log table:`\n- `WITH upd AS (`\n- `UPDATE employees SET salescount = salescount + 1 WHERE id =`\n- `(SELECT salesperson FROM accounts WHERE name = 'Acme Corporation')`\n- `RETURNING *`\n- `INSERT INTO employeeslog SELECT *, currenttimestamp FROM upd;`\n- `Insert or update new distributors as appropriate. Assumes a unique index has been defined`\n- `that constrains values appearing in the did column. Note that the special excluded table is`\n- `used to reference values originally proposed for insertion:`\n- `INSERT INTO distributors (did, dname)`\n- `VALUES (5, 'Gizmo Transglobal'), (6, 'Associated Computing, Inc')`\n- `ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname;`\n- `Insert a distributor, or do nothing for rows proposed for insertion when an existing,`\n- `excluded row (a row with a matching constrained column or columns after before row insert`\n- `triggers fire) exists. Example assumes a unique index has been defined that constrains values`\n- `appearing in the did column:`\n- `INSERT INTO distributors (did, dname) VALUES (7, 'Redline GmbH')`\n- `ON CONFLICT (did) DO NOTHING;`\n- `Insert or update new distributors as appropriate. Example assumes a unique index has been`\n- `defined that constrains values appearing in the did column.  WHERE clause is used to limit`\n- `the rows actually updated (any existing row not updated will still be locked, though):`\n- `-- Don't update existing distributors based in a certain ZIP code`\n- `INSERT INTO distributors AS d (did, dname) VALUES (8, 'Anvil Distribution')`\n- `ON CONFLICT (did) DO UPDATE`\n- `SET dname = EXCLUDED.dname || ' (formerly ' || d.dname || ')'`\n- `WHERE d.zipcode <> '21201';`\n- `-- Name a constraint directly in the statement (uses associated`\n- `-- index to arbitrate taking the DO NOTHING action)`\n- `INSERT INTO distributors (did, dname) VALUES (9, 'Antwerp Design')`\n- `ON CONFLICT ON CONSTRAINT distributorspkey DO NOTHING;`\n- `Insert new distributor if possible; otherwise DO NOTHING. Example assumes a unique index has`\n- `been defined that constrains values appearing in the did column on a subset of rows where the`\n- `isactive Boolean column evaluates to true:`\n- `-- This statement could infer a partial unique index on \"did\"`\n- `-- with a predicate of \"WHERE isactive\", but it could also`\n- `-- just use a regular unique constraint on \"did\"`\n- `INSERT INTO distributors (did, dname) VALUES (10, 'Conrad International')`\n- `ON CONFLICT (did) WHERE isactive DO NOTHING;`\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **SYNOPSIS** (21 lines)\n- **DESCRIPTION** (44 lines)\n- **PARAMETERS** (1 lines) — 2 subsections\n  - Inserting (72 lines)\n  - ON CONFLICT Clause (102 lines)\n- **OUTPUTS** (12 lines)\n- **NOTES** (4 lines)\n- **EXAMPLES** (98 lines)\n- **COMPATIBILITY** (15 lines)\n\n## Full Content\n\n### NAME\n\nINSERT - create new rows in a table\n\n### SYNOPSIS\n\n[ WITH [ RECURSIVE ] withquery [, ...] ]\nINSERT INTO tablename [ AS alias ] [ ( columnname [, ...] ) ]\n[ OVERRIDING { SYSTEM | USER } VALUE ]\n{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }\n[ ON CONFLICT [ conflicttarget ] conflictaction ]\n[ RETURNING { * | outputexpression [ [ AS ] outputname ] } [, ...] ]\n\nwhere conflicttarget can be one of:\n\n( { indexcolumnname | ( indexexpression ) } [ COLLATE collation ] [ opclass ] [, ...] ) [ WHERE indexpredicate ]\nON CONSTRAINT constraintname\n\nand conflictaction is one of:\n\nDO NOTHING\nDO UPDATE SET { columnname = { expression | DEFAULT } |\n( columnname [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) |\n( columnname [, ...] ) = ( sub-SELECT )\n} [, ...]\n[ WHERE condition ]\n\n### DESCRIPTION\n\nINSERT inserts new rows into a table. One can insert one or more rows specified by value\nexpressions, or zero or more rows resulting from a query.\n\nThe target column names can be listed in any order. If no list of column names is given at\nall, the default is all the columns of the table in their declared order; or the first N\ncolumn names, if there are only N columns supplied by the VALUES clause or query. The values\nsupplied by the VALUES clause or query are associated with the explicit or implicit column\nlist left-to-right.\n\nEach column not present in the explicit or implicit column list will be filled with a default\nvalue, either its declared default value or null if there is none.\n\nIf the expression for any column is not of the correct data type, automatic type conversion\nwill be attempted.\n\nINSERT into tables that lack unique indexes will not be blocked by concurrent activity.\nTables with unique indexes might block if concurrent sessions perform actions that lock or\nmodify rows matching the unique index values being inserted; the details are covered in\nSection 62.5.  ON CONFLICT can be used to specify an alternative action to raising a unique\nconstraint or exclusion constraint violation error. (See ON CONFLICT Clause below.)\n\nThe optional RETURNING clause causes INSERT to compute and return value(s) based on each row\nactually inserted (or updated, if an ON CONFLICT DO UPDATE clause was used). This is\nprimarily useful for obtaining values that were supplied by defaults, such as a serial\nsequence number. However, any expression using the table's columns is allowed. The syntax of\nthe RETURNING list is identical to that of the output list of SELECT. Only rows that were\nsuccessfully inserted or updated will be returned. For example, if a row was locked but not\nupdated because an ON CONFLICT DO UPDATE ... WHERE clause condition was not satisfied, the\nrow will not be returned.\n\nYou must have INSERT privilege on a table in order to insert into it. If ON CONFLICT DO\nUPDATE is present, UPDATE privilege on the table is also required.\n\nIf a column list is specified, you only need INSERT privilege on the listed columns.\nSimilarly, when ON CONFLICT DO UPDATE is specified, you only need UPDATE privilege on the\ncolumn(s) that are listed to be updated. However, all forms of ON CONFLICT also require\nSELECT privilege on any column whose values are read. This includes any column mentioned in\nconflicttarget (including columns referred to by the arbiter constraint), and any column\nmentioned in an ON CONFLICT DO UPDATE expression, or a WHERE clause condition.\n\nUse of the RETURNING clause requires SELECT privilege on all columns mentioned in RETURNING.\nIf you use the query clause to insert rows from a query, you of course need to have SELECT\nprivilege on any table or column used in the query.\n\n### PARAMETERS\n\n#### Inserting\n\nThis section covers parameters that may be used when only inserting new rows. Parameters\nexclusively used with the ON CONFLICT clause are described separately.\n\nwithquery\nThe WITH clause allows you to specify one or more subqueries that can be referenced by\nname in the INSERT query. See Section 7.8 and SELECT(7) for details.\n\nIt is possible for the query (SELECT statement) to also contain a WITH clause. In such a\ncase both sets of withquery can be referenced within the query, but the second one takes\nprecedence since it is more closely nested.\n\ntablename\nThe name (optionally schema-qualified) of an existing table.\n\nalias\nA substitute name for tablename. When an alias is provided, it completely hides the\nactual name of the table. This is particularly useful when ON CONFLICT DO UPDATE targets\na table named excluded, since that will otherwise be taken as the name of the special\ntable representing the row proposed for insertion.\n\ncolumnname\nThe name of a column in the table named by tablename. The column name can be qualified\nwith a subfield name or array subscript, if needed. (Inserting into only some fields of a\ncomposite column leaves the other fields null.) When referencing a column with ON\nCONFLICT DO UPDATE, do not include the table's name in the specification of a target\ncolumn. For example, INSERT INTO tablename ... ON CONFLICT DO UPDATE SET tablename.col\n= 1 is invalid (this follows the general behavior for UPDATE).\n\nOVERRIDING SYSTEM VALUE\nIf this clause is specified, then any values supplied for identity columns will override\nthe default sequence-generated values.\n\nFor an identity column defined as GENERATED ALWAYS, it is an error to insert an explicit\nvalue (other than DEFAULT) without specifying either OVERRIDING SYSTEM VALUE or\nOVERRIDING USER VALUE. (For an identity column defined as GENERATED BY DEFAULT,\nOVERRIDING SYSTEM VALUE is the normal behavior and specifying it does nothing, but\nPostgreSQL allows it as an extension.)\n\nOVERRIDING USER VALUE\nIf this clause is specified, then any values supplied for identity columns are ignored\nand the default sequence-generated values are applied.\n\nThis clause is useful for example when copying values between tables. Writing INSERT INTO\ntbl2 OVERRIDING USER VALUE SELECT * FROM tbl1 will copy from tbl1 all columns that are\nnot identity columns in tbl2 while values for the identity columns in tbl2 will be\ngenerated by the sequences associated with tbl2.\n\nDEFAULT VALUES\nAll columns will be filled with their default values, as if DEFAULT were explicitly\nspecified for each column. (An OVERRIDING clause is not permitted in this form.)\n\nexpression\nAn expression or value to assign to the corresponding column.\n\nDEFAULT\nThe corresponding column will be filled with its default value. An identity column will\nbe filled with a new value generated by the associated sequence. For a generated column,\nspecifying this is permitted but merely specifies the normal behavior of computing the\ncolumn from its generation expression.\n\nquery\nA query (SELECT statement) that supplies the rows to be inserted. Refer to the SELECT(7)\nstatement for a description of the syntax.\n\noutputexpression\nAn expression to be computed and returned by the INSERT command after each row is\ninserted or updated. The expression can use any column names of the table named by\ntablename. Write * to return all columns of the inserted or updated row(s).\n\noutputname\nA name to use for a returned column.\n\n#### ON CONFLICT Clause\n\nThe optional ON CONFLICT clause specifies an alternative action to raising a unique violation\nor exclusion constraint violation error. For each individual row proposed for insertion,\neither the insertion proceeds, or, if an arbiter constraint or index specified by\nconflicttarget is violated, the alternative conflictaction is taken.  ON CONFLICT DO\nNOTHING simply avoids inserting a row as its alternative action.  ON CONFLICT DO UPDATE\nupdates the existing row that conflicts with the row proposed for insertion as its\nalternative action.\n\nconflicttarget can perform unique index inference. When performing inference, it consists of\none or more indexcolumnname columns and/or indexexpression expressions, and an optional\nindexpredicate. All tablename unique indexes that, without regard to order, contain exactly\nthe conflicttarget-specified columns/expressions are inferred (chosen) as arbiter indexes.\nIf an indexpredicate is specified, it must, as a further requirement for inference, satisfy\narbiter indexes. Note that this means a non-partial unique index (a unique index without a\npredicate) will be inferred (and thus used by ON CONFLICT) if such an index satisfying every\nother criteria is available. If an attempt at inference is unsuccessful, an error is raised.\n\nON CONFLICT DO UPDATE guarantees an atomic INSERT or UPDATE outcome; provided there is no\nindependent error, one of those two outcomes is guaranteed, even under high concurrency. This\nis also known as UPSERT — “UPDATE or INSERT”.\n\nconflicttarget\nSpecifies which conflicts ON CONFLICT takes the alternative action on by choosing arbiter\nindexes. Either performs unique index inference, or names a constraint explicitly. For ON\nCONFLICT DO NOTHING, it is optional to specify a conflicttarget; when omitted, conflicts\nwith all usable constraints (and unique indexes) are handled. For ON CONFLICT DO UPDATE,\na conflicttarget must be provided.\n\nconflictaction\nconflictaction specifies an alternative ON CONFLICT action. It can be either DO NOTHING,\nor a DO UPDATE clause specifying the exact details of the UPDATE action to be performed\nin case of a conflict. The SET and WHERE clauses in ON CONFLICT DO UPDATE have access to\nthe existing row using the table's name (or an alias), and to the row proposed for\ninsertion using the special excluded table.  SELECT privilege is required on any column\nin the target table where corresponding excluded columns are read.\n\nNote that the effects of all per-row BEFORE INSERT triggers are reflected in excluded\nvalues, since those effects may have contributed to the row being excluded from\ninsertion.\n\nindexcolumnname\nThe name of a tablename column. Used to infer arbiter indexes. Follows CREATE INDEX\nformat.  SELECT privilege on indexcolumnname is required.\n\nindexexpression\nSimilar to indexcolumnname, but used to infer expressions on tablename columns\nappearing within index definitions (not simple columns). Follows CREATE INDEX format.\nSELECT privilege on any column appearing within indexexpression is required.\n\ncollation\nWhen specified, mandates that corresponding indexcolumnname or indexexpression use a\nparticular collation in order to be matched during inference. Typically this is omitted,\nas collations usually do not affect whether or not a constraint violation occurs. Follows\nCREATE INDEX format.\n\nopclass\nWhen specified, mandates that corresponding indexcolumnname or indexexpression use\nparticular operator class in order to be matched during inference. Typically this is\nomitted, as the equality semantics are often equivalent across a type's operator classes\nanyway, or because it's sufficient to trust that the defined unique indexes have the\npertinent definition of equality. Follows CREATE INDEX format.\n\nindexpredicate\nUsed to allow inference of partial unique indexes. Any indexes that satisfy the predicate\n(which need not actually be partial indexes) can be inferred. Follows CREATE INDEX\nformat.  SELECT privilege on any column appearing within indexpredicate is required.\n\nconstraintname\nExplicitly specifies an arbiter constraint by name, rather than inferring a constraint or\nindex.\n\ncondition\nAn expression that returns a value of type boolean. Only rows for which this expression\nreturns true will be updated, although all rows will be locked when the ON CONFLICT DO\nUPDATE action is taken. Note that condition is evaluated last, after a conflict has been\nidentified as a candidate to update.\n\nNote that exclusion constraints are not supported as arbiters with ON CONFLICT DO UPDATE. In\nall cases, only NOT DEFERRABLE constraints and unique indexes are supported as arbiters.\n\nINSERT with an ON CONFLICT DO UPDATE clause is a “deterministic” statement. This means that\nthe command will not be allowed to affect any single existing row more than once; a\ncardinality violation error will be raised when this situation arises. Rows proposed for\ninsertion should not duplicate each other in terms of attributes constrained by an arbiter\nindex or constraint.\n\nNote that it is currently not supported for the ON CONFLICT DO UPDATE clause of an INSERT\napplied to a partitioned table to update the partition key of a conflicting row such that it\nrequires the row be moved to a new partition.\n\nTip\nIt is often preferable to use unique index inference rather than naming a constraint\ndirectly using ON CONFLICT ON CONSTRAINT\nconstraintname. Inference will continue to work correctly when the underlying index is\nreplaced by another more or less equivalent index in an overlapping way, for example when\nusing CREATE UNIQUE INDEX ... CONCURRENTLY before dropping the index being replaced.\n\nWarning\nWhile CREATE INDEX CONCURRENTLY or REINDEX CONCURRENTLY is running on a unique index,\nINSERT ... ON CONFLICT statements on the same table may unexpectedly fail with a unique\nviolation.\n\n### OUTPUTS\n\nOn successful completion, an INSERT command returns a command tag of the form\n\nINSERT oid count\n\nThe count is the number of rows inserted or updated.  oid is always 0 (it used to be the OID\nassigned to the inserted row if count was exactly one and the target table was declared WITH\nOIDS and 0 otherwise, but creating a table WITH OIDS is not supported anymore).\n\nIf the INSERT command contains a RETURNING clause, the result will be similar to that of a\nSELECT statement containing the columns and values defined in the RETURNING list, computed\nover the row(s) inserted or updated by the command.\n\n### NOTES\n\nIf the specified table is a partitioned table, each row is routed to the appropriate\npartition and inserted into it. If the specified table is a partition, an error will occur if\none of the input rows violates the partition constraint.\n\n### EXAMPLES\n\nInsert a single row into table films:\n\nINSERT INTO films VALUES\n('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');\n\nIn this example, the len column is omitted and therefore it will have the default value:\n\nINSERT INTO films (code, title, did, dateprod, kind)\nVALUES ('T601', 'Yojimbo', 106, '1961-06-16', 'Drama');\n\nThis example uses the DEFAULT clause for the date columns rather than specifying a value:\n\nINSERT INTO films VALUES\n('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes');\nINSERT INTO films (code, title, did, dateprod, kind)\nVALUES ('T601', 'Yojimbo', 106, DEFAULT, 'Drama');\n\nTo insert a row consisting entirely of default values:\n\nINSERT INTO films DEFAULT VALUES;\n\nTo insert multiple rows using the multirow VALUES syntax:\n\nINSERT INTO films (code, title, did, dateprod, kind) VALUES\n('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),\n('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');\n\nThis example inserts some rows into table films from a table tmpfilms with the same column\nlayout as films:\n\nINSERT INTO films SELECT * FROM tmpfilms WHERE dateprod < '2004-05-07';\n\nThis example inserts into array columns:\n\n-- Create an empty 3x3 gameboard for noughts-and-crosses\nINSERT INTO tictactoe (game, board[1:3][1:3])\nVALUES (1, '{{\" \",\" \",\" \"},{\" \",\" \",\" \"},{\" \",\" \",\" \"}}');\n-- The subscripts in the above example aren't really needed\nINSERT INTO tictactoe (game, board)\nVALUES (2, '{{X,\" \",\" \"},{\" \",O,\" \"},{\" \",X,\" \"}}');\n\nInsert a single row into table distributors, returning the sequence number generated by the\nDEFAULT clause:\n\nINSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')\nRETURNING did;\n\nIncrement the sales count of the salesperson who manages the account for Acme Corporation,\nand record the whole updated row along with current time in a log table:\n\nWITH upd AS (\nUPDATE employees SET salescount = salescount + 1 WHERE id =\n(SELECT salesperson FROM accounts WHERE name = 'Acme Corporation')\nRETURNING *\n)\nINSERT INTO employeeslog SELECT *, currenttimestamp FROM upd;\n\nInsert or update new distributors as appropriate. Assumes a unique index has been defined\nthat constrains values appearing in the did column. Note that the special excluded table is\nused to reference values originally proposed for insertion:\n\nINSERT INTO distributors (did, dname)\nVALUES (5, 'Gizmo Transglobal'), (6, 'Associated Computing, Inc')\nON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname;\n\nInsert a distributor, or do nothing for rows proposed for insertion when an existing,\nexcluded row (a row with a matching constrained column or columns after before row insert\ntriggers fire) exists. Example assumes a unique index has been defined that constrains values\nappearing in the did column:\n\nINSERT INTO distributors (did, dname) VALUES (7, 'Redline GmbH')\nON CONFLICT (did) DO NOTHING;\n\nInsert or update new distributors as appropriate. Example assumes a unique index has been\ndefined that constrains values appearing in the did column.  WHERE clause is used to limit\nthe rows actually updated (any existing row not updated will still be locked, though):\n\n-- Don't update existing distributors based in a certain ZIP code\nINSERT INTO distributors AS d (did, dname) VALUES (8, 'Anvil Distribution')\nON CONFLICT (did) DO UPDATE\nSET dname = EXCLUDED.dname || ' (formerly ' || d.dname || ')'\nWHERE d.zipcode <> '21201';\n\n-- Name a constraint directly in the statement (uses associated\n-- index to arbitrate taking the DO NOTHING action)\nINSERT INTO distributors (did, dname) VALUES (9, 'Antwerp Design')\nON CONFLICT ON CONSTRAINT distributorspkey DO NOTHING;\n\nInsert new distributor if possible; otherwise DO NOTHING. Example assumes a unique index has\nbeen defined that constrains values appearing in the did column on a subset of rows where the\nisactive Boolean column evaluates to true:\n\n-- This statement could infer a partial unique index on \"did\"\n-- with a predicate of \"WHERE isactive\", but it could also\n-- just use a regular unique constraint on \"did\"\nINSERT INTO distributors (did, dname) VALUES (10, 'Conrad International')\nON CONFLICT (did) WHERE isactive DO NOTHING;\n\n### COMPATIBILITY\n\nINSERT conforms to the SQL standard, except that the RETURNING clause is a PostgreSQL\nextension, as is the ability to use WITH with INSERT, and the ability to specify an\nalternative action with ON CONFLICT. Also, the case in which a column name list is omitted,\nbut not all the columns are filled from the VALUES clause or query, is disallowed by the\nstandard.\n\nThe SQL standard specifies that OVERRIDING SYSTEM VALUE can only be specified if an identity\ncolumn that is generated always exists. PostgreSQL allows the clause in any case and ignores\nit if it is not applicable.\n\nPossible limitations of the query clause are documented under SELECT(7).\n\n\n\nPostgreSQL 14.23                                2026                                       INSERT(7)\n\n"
        }
    ],
    "structuredContent": {
        "command": "INSERT",
        "section": "7",
        "mode": "man",
        "summary": "INSERT - create new rows in a table",
        "synopsis": "[ WITH [ RECURSIVE ] withquery [, ...] ]\nINSERT INTO tablename [ AS alias ] [ ( columnname [, ...] ) ]\n[ OVERRIDING { SYSTEM | USER } VALUE ]\n{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }\n[ ON CONFLICT [ conflicttarget ] conflictaction ]\n[ RETURNING { * | outputexpression [ [ AS ] outputname ] } [, ...] ]\nwhere conflicttarget can be one of:\n( { indexcolumnname | ( indexexpression ) } [ COLLATE collation ] [ opclass ] [, ...] ) [ WHERE indexpredicate ]\nON CONSTRAINT constraintname\nand conflictaction is one of:\nDO NOTHING\nDO UPDATE SET { columnname = { expression | DEFAULT } |\n( columnname [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) |\n( columnname [, ...] ) = ( sub-SELECT )\n} [, ...]\n[ WHERE condition ]",
        "flags": [],
        "examples": [
            "Insert a single row into table films:",
            "INSERT INTO films VALUES",
            "('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');",
            "In this example, the len column is omitted and therefore it will have the default value:",
            "INSERT INTO films (code, title, did, dateprod, kind)",
            "VALUES ('T601', 'Yojimbo', 106, '1961-06-16', 'Drama');",
            "This example uses the DEFAULT clause for the date columns rather than specifying a value:",
            "INSERT INTO films VALUES",
            "('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes');",
            "INSERT INTO films (code, title, did, dateprod, kind)",
            "VALUES ('T601', 'Yojimbo', 106, DEFAULT, 'Drama');",
            "To insert a row consisting entirely of default values:",
            "INSERT INTO films DEFAULT VALUES;",
            "To insert multiple rows using the multirow VALUES syntax:",
            "INSERT INTO films (code, title, did, dateprod, kind) VALUES",
            "('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),",
            "('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');",
            "This example inserts some rows into table films from a table tmpfilms with the same column",
            "layout as films:",
            "INSERT INTO films SELECT * FROM tmpfilms WHERE dateprod < '2004-05-07';",
            "This example inserts into array columns:",
            "-- Create an empty 3x3 gameboard for noughts-and-crosses",
            "INSERT INTO tictactoe (game, board[1:3][1:3])",
            "VALUES (1, '{{\" \",\" \",\" \"},{\" \",\" \",\" \"},{\" \",\" \",\" \"}}');",
            "-- The subscripts in the above example aren't really needed",
            "INSERT INTO tictactoe (game, board)",
            "VALUES (2, '{{X,\" \",\" \"},{\" \",O,\" \"},{\" \",X,\" \"}}');",
            "Insert a single row into table distributors, returning the sequence number generated by the",
            "DEFAULT clause:",
            "INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')",
            "RETURNING did;",
            "Increment the sales count of the salesperson who manages the account for Acme Corporation,",
            "and record the whole updated row along with current time in a log table:",
            "WITH upd AS (",
            "UPDATE employees SET salescount = salescount + 1 WHERE id =",
            "(SELECT salesperson FROM accounts WHERE name = 'Acme Corporation')",
            "RETURNING *",
            "INSERT INTO employeeslog SELECT *, currenttimestamp FROM upd;",
            "Insert or update new distributors as appropriate. Assumes a unique index has been defined",
            "that constrains values appearing in the did column. Note that the special excluded table is",
            "used to reference values originally proposed for insertion:",
            "INSERT INTO distributors (did, dname)",
            "VALUES (5, 'Gizmo Transglobal'), (6, 'Associated Computing, Inc')",
            "ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname;",
            "Insert a distributor, or do nothing for rows proposed for insertion when an existing,",
            "excluded row (a row with a matching constrained column or columns after before row insert",
            "triggers fire) exists. Example assumes a unique index has been defined that constrains values",
            "appearing in the did column:",
            "INSERT INTO distributors (did, dname) VALUES (7, 'Redline GmbH')",
            "ON CONFLICT (did) DO NOTHING;",
            "Insert or update new distributors as appropriate. Example assumes a unique index has been",
            "defined that constrains values appearing in the did column.  WHERE clause is used to limit",
            "the rows actually updated (any existing row not updated will still be locked, though):",
            "-- Don't update existing distributors based in a certain ZIP code",
            "INSERT INTO distributors AS d (did, dname) VALUES (8, 'Anvil Distribution')",
            "ON CONFLICT (did) DO UPDATE",
            "SET dname = EXCLUDED.dname || ' (formerly ' || d.dname || ')'",
            "WHERE d.zipcode <> '21201';",
            "-- Name a constraint directly in the statement (uses associated",
            "-- index to arbitrate taking the DO NOTHING action)",
            "INSERT INTO distributors (did, dname) VALUES (9, 'Antwerp Design')",
            "ON CONFLICT ON CONSTRAINT distributorspkey DO NOTHING;",
            "Insert new distributor if possible; otherwise DO NOTHING. Example assumes a unique index has",
            "been defined that constrains values appearing in the did column on a subset of rows where the",
            "isactive Boolean column evaluates to true:",
            "-- This statement could infer a partial unique index on \"did\"",
            "-- with a predicate of \"WHERE isactive\", but it could also",
            "-- just use a regular unique constraint on \"did\"",
            "INSERT INTO distributors (did, dname) VALUES (10, 'Conrad International')",
            "ON CONFLICT (did) WHERE isactive DO NOTHING;"
        ],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 21,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 44,
                "subsections": []
            },
            {
                "name": "PARAMETERS",
                "lines": 1,
                "subsections": [
                    {
                        "name": "Inserting",
                        "lines": 72
                    },
                    {
                        "name": "ON CONFLICT Clause",
                        "lines": 102
                    }
                ]
            },
            {
                "name": "OUTPUTS",
                "lines": 12,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 4,
                "subsections": []
            },
            {
                "name": "EXAMPLES",
                "lines": 98,
                "subsections": []
            },
            {
                "name": "COMPATIBILITY",
                "lines": 15,
                "subsections": []
            }
        ]
    }
}