3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "INSERT" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 INSERT \- create new rows in a table
35 [ WITH [ RECURSIVE ] \fIwith_query\fR [, \&.\&.\&.] ]
36 INSERT INTO \fItable_name\fR [ AS \fIalias\fR ] [ ( \fIcolumn_name\fR [, \&.\&.\&.] ) ]
37 [ OVERRIDING { SYSTEM | USER } VALUE ]
38 { DEFAULT VALUES | VALUES ( { \fIexpression\fR | DEFAULT } [, \&.\&.\&.] ) [, \&.\&.\&.] | \fIquery\fR }
39 [ ON CONFLICT [ \fIconflict_target\fR ] \fIconflict_action\fR ]
40 [ RETURNING [ WITH ( { OLD | NEW } AS \fIoutput_alias\fR [, \&.\&.\&.] ) ]
41 { * | \fIoutput_expression\fR [ [ AS ] \fIoutput_name\fR ] } [, \&.\&.\&.] ]
43 where \fIconflict_target\fR can be one of:
45 ( { \fIindex_column_name\fR | ( \fIindex_expression\fR ) } [ COLLATE \fIcollation\fR ] [ \fIopclass\fR ] [, \&.\&.\&.] ) [ WHERE \fIindex_predicate\fR ]
46 ON CONSTRAINT \fIconstraint_name\fR
48 and \fIconflict_action\fR is one of:
51 DO UPDATE SET { \fIcolumn_name\fR = { \fIexpression\fR | DEFAULT } |
52 ( \fIcolumn_name\fR [, \&.\&.\&.] ) = [ ROW ] ( { \fIexpression\fR | DEFAULT } [, \&.\&.\&.] ) |
53 ( \fIcolumn_name\fR [, \&.\&.\&.] ) = ( \fIsub\-SELECT\fR )
55 [ WHERE \fIcondition\fR ]
60 inserts new rows into a table\&. One can insert one or more rows specified by value expressions, or zero or more rows resulting from a query\&.
62 The target column names can be listed in any order\&. If no list of column names is given at all, the default is all the columns of the table in their declared order; or the first
64 column names, if there are only
66 columns supplied by the
69 \fIquery\fR\&. The values supplied by the
73 are associated with the explicit or implicit column list left\-to\-right\&.
75 Each column not present in the explicit or implicit column list will be filled with a default value, either its declared default value or null if there is none\&.
77 If the expression for any column is not of the correct data type, automatic type conversion will be attempted\&.
80 into tables that lack unique indexes will not be blocked by concurrent activity\&. Tables with unique indexes might block if concurrent sessions perform actions that lock or modify rows matching the unique index values being inserted; the details are covered in
83 can be used to specify an alternative action to raising a unique constraint or exclusion constraint violation error\&. (See
91 to compute and return value(s) based on each row actually inserted (or updated, if an
93 clause was used)\&. This is primarily useful for obtaining values that were supplied by defaults, such as a serial sequence number\&. However, any expression using the table\*(Aqs columns is allowed\&. The syntax of the
95 list is identical to that of the output list of
96 \fBSELECT\fR\&. Only rows that were successfully inserted or updated will be returned\&. For example, if a row was locked but not updated because an
97 ON CONFLICT DO UPDATE \&.\&.\&. WHERE
100 was not satisfied, the row will not be returned\&.
104 privilege on a table in order to insert into it\&. If
105 ON CONFLICT DO UPDATE
108 privilege on the table is also required\&.
110 If a column list is specified, you only need
112 privilege on the listed columns\&. Similarly, when
113 ON CONFLICT DO UPDATE
114 is specified, you only need
116 privilege on the column(s) that are listed to be updated\&. However,
117 ON CONFLICT DO UPDATE
120 privilege on any column whose values are read in the
121 ON CONFLICT DO UPDATE
129 privilege on all columns mentioned in
130 RETURNING\&. If you use the
132 clause to insert rows from a query, you of course need to have
134 privilege on any table or column used in the query\&.
138 This section covers parameters that may be used when only inserting new rows\&. Parameters
142 clause are described separately\&.
148 clause allows you to specify one or more subqueries that can be referenced by name in the
156 It is possible for the
159 statement) to also contain a
161 clause\&. In such a case both sets of
163 can be referenced within the
164 \fIquery\fR, but the second one takes precedence since it is more closely nested\&.
169 The name (optionally schema\-qualified) of an existing table\&.
174 A substitute name for
175 \fItable_name\fR\&. When an alias is provided, it completely hides the actual name of the table\&. This is particularly useful when
176 ON CONFLICT DO UPDATE
177 targets a table named
178 \fIexcluded\fR, since that will otherwise be taken as the name of the special table representing the row proposed for insertion\&.
183 The name of a column in the table named by
184 \fItable_name\fR\&. The column name can be qualified with a subfield name or array subscript, if needed\&. (Inserting into only some fields of a composite column leaves the other fields null\&.) When referencing a column with
185 ON CONFLICT DO UPDATE, do not include the table\*(Aqs name in the specification of a target column\&. For example,
186 INSERT INTO table_name \&.\&.\&. ON CONFLICT DO UPDATE SET table_name\&.col = 1
187 is invalid (this follows the general behavior for
191 OVERRIDING SYSTEM VALUE
193 If this clause is specified, then any values supplied for identity columns will override the default sequence\-generated values\&.
195 For an identity column defined as
196 GENERATED ALWAYS, it is an error to insert an explicit value (other than
197 DEFAULT) without specifying either
198 OVERRIDING SYSTEM VALUE
200 OVERRIDING USER VALUE\&. (For an identity column defined as
201 GENERATED BY DEFAULT,
202 OVERRIDING SYSTEM VALUE
203 is the normal behavior and specifying it does nothing, but
205 allows it as an extension\&.)
208 OVERRIDING USER VALUE
210 If this clause is specified, then any values supplied for identity columns are ignored and the default sequence\-generated values are applied\&.
212 This clause is useful for example when copying values between tables\&. Writing
213 INSERT INTO tbl2 OVERRIDING USER VALUE SELECT * FROM tbl1
216 all columns that are not identity columns in
218 while values for the identity columns in
220 will be generated by the sequences associated with
226 All columns will be filled with their default values, as if
228 were explicitly specified for each column\&. (An
230 clause is not permitted in this form\&.)
235 An expression or value to assign to the corresponding column\&.
240 The corresponding column will be filled with its default value\&. An identity column will be filled with a new value generated by the associated sequence\&. For a generated column, specifying this is permitted but merely specifies the normal behavior of computing the column from its generation expression\&.
245 A query (\fBSELECT\fR
246 statement) that supplies the rows to be inserted\&. Refer to the
248 statement for a description of the syntax\&.
253 An optional substitute name for
261 By default, old values from the target table can be returned by writing
262 OLD\&.\fIcolumn_name\fR
264 OLD\&.*, and new values can be returned by writing
265 NEW\&.\fIcolumn_name\fR
267 NEW\&.*\&. When an alias is provided, these names are hidden and the old or new rows must be referred to using the alias\&. For example
268 RETURNING WITH (OLD AS o, NEW AS n) o\&.*, n\&.*\&.
271 \fIoutput_expression\fR
273 An expression to be computed and returned by the
275 command after each row is inserted or updated\&. The expression can use any column names of the table named by
276 \fItable_name\fR\&. Write
278 to return all columns of the inserted or updated row(s)\&.
282 may be qualified using
285 NEW, or the corresponding
290 NEW, to cause old or new values to be returned\&. An unqualified column name, or
291 *, or a column name or
293 qualified using the target table name or alias will return new values\&.
296 \fBINSERT\fR, all old values will be
297 NULL\&. However, for an
300 ON CONFLICT DO UPDATE
301 clause, the old values may be non\-NULL\&.
306 A name to use for a returned column\&.
308 .SS "ON CONFLICT Clause"
312 clause specifies an alternative action to raising a unique violation or exclusion constraint violation error\&. For each individual row proposed for insertion, either the insertion proceeds, or, if an
314 constraint or index specified by
315 \fIconflict_target\fR
316 is violated, the alternative
317 \fIconflict_action\fR
319 ON CONFLICT DO NOTHING
320 simply avoids inserting a row as its alternative action\&.
321 ON CONFLICT DO UPDATE
322 updates the existing row that conflicts with the row proposed for insertion as its alternative action\&.
324 \fIconflict_target\fR
326 \fIunique index inference\fR\&. When performing inference, it consists of one or more
327 \fIindex_column_name\fR
329 \fIindex_expression\fR
330 expressions, and an optional
331 \fIindex_predicate\fR\&. All
333 unique indexes that, without regard to order, contain exactly the
334 \fIconflict_target\fR\-specified columns/expressions are inferred (chosen) as arbiter indexes\&. If an
335 \fIindex_predicate\fR
336 is specified, it must, as a further requirement for inference, satisfy arbiter indexes\&. Note that this means a non\-partial unique index (a unique index without a predicate) will be inferred (and thus used by
337 ON CONFLICT) if such an index satisfying every other criteria is available\&. If an attempt at inference is unsuccessful, an error is raised\&.
339 ON CONFLICT DO UPDATE
344 outcome; provided there is no independent error, one of those two outcomes is guaranteed, even under high concurrency\&. This is also known as
347 \(lqUPDATE or INSERT\(rq\&.
349 \fIconflict_target\fR
351 Specifies which conflicts
353 takes the alternative action on by choosing
354 arbiter indexes\&. Either performs
355 \fIunique index inference\fR, or names a constraint explicitly\&. For
356 ON CONFLICT DO NOTHING, it is optional to specify a
357 \fIconflict_target\fR; when omitted, conflicts with all usable constraints (and unique indexes) are handled\&. For
358 ON CONFLICT DO UPDATE, a
359 \fIconflict_target\fR
364 \fIconflict_action\fR
366 \fIconflict_action\fR
367 specifies an alternative
369 action\&. It can be either
372 clause specifying the exact details of the
374 action to be performed in case of a conflict\&. The
379 ON CONFLICT DO UPDATE
380 have access to the existing row using the table\*(Aqs name (or an alias), and to the row proposed for insertion using the special
384 privilege is required on any column in the target table where corresponding
388 Note that the effects of all per\-row
390 triggers are reflected in
392 values, since those effects may have contributed to the row being excluded from insertion\&.
395 \fIindex_column_name\fR
399 column\&. Used to infer arbiter indexes\&. Follows
404 \fIindex_column_name\fR
408 \fIindex_expression\fR
411 \fIindex_column_name\fR, but used to infer expressions on
413 columns appearing within index definitions (not simple columns)\&. Follows
417 privilege on any column appearing within
418 \fIindex_expression\fR
424 When specified, mandates that corresponding
425 \fIindex_column_name\fR
427 \fIindex_expression\fR
428 use a particular collation in order to be matched during inference\&. Typically this is omitted, as collations usually do not affect whether or not a constraint violation occurs\&. Follows
435 When specified, mandates that corresponding
436 \fIindex_column_name\fR
438 \fIindex_expression\fR
439 use particular operator class in order to be matched during inference\&. Typically this is omitted, as the
441 semantics are often equivalent across a type\*(Aqs operator classes anyway, or because it\*(Aqs sufficient to trust that the defined unique indexes have the pertinent definition of equality\&. Follows
446 \fIindex_predicate\fR
448 Used to allow inference of partial unique indexes\&. Any indexes that satisfy the predicate (which need not actually be partial indexes) can be inferred\&. Follows
452 privilege on any column appearing within
453 \fIindex_predicate\fR
457 \fIconstraint_name\fR
459 Explicitly specifies an arbiter
461 by name, rather than inferring a constraint or index\&.
466 An expression that returns a value of type
467 boolean\&. Only rows for which this expression returns
469 will be updated, although all rows will be locked when the
470 ON CONFLICT DO UPDATE
471 action is taken\&. Note that
473 is evaluated last, after a conflict has been identified as a candidate to update\&.
476 Note that exclusion constraints are not supported as arbiters with
477 ON CONFLICT DO UPDATE\&. In all cases, only
479 constraints and unique indexes are supported as arbiters\&.
483 ON CONFLICT DO UPDATE
485 \(lqdeterministic\(rq
486 statement\&. This means that the command will not be allowed to affect any single existing row more than once; a cardinality violation error will be raised when this situation arises\&. Rows proposed for insertion should not duplicate each other in terms of attributes constrained by an arbiter index or constraint\&.
488 Note that it is currently not supported for the
489 ON CONFLICT DO UPDATE
492 applied to a partitioned table to update the partition key of a conflicting row such that it requires the row be moved to a new partition\&.
498 .nr an-no-space-flag 1
506 It is often preferable to use unique index inference rather than naming a constraint directly using
507 ON CONFLICT ON CONSTRAINT
508 \fI constraint_name\fR\&. Inference will continue to work correctly when the underlying index is replaced by another more or less equivalent index in an overlapping way, for example when using
509 CREATE UNIQUE INDEX \&.\&.\&. CONCURRENTLY
510 before dropping the index being replaced\&.
515 On successful completion, an
517 command returns a command tag of the form
523 INSERT \fIoid\fR \fIcount\fR
531 is the number of rows inserted or updated\&.
533 is always 0 (it used to be the
535 assigned to the inserted row if
537 was exactly one and the target table was declared
539 and 0 otherwise, but creating a table
541 is not supported anymore)\&.
547 clause, the result will be similar to that of a
549 statement containing the columns and values defined in the
551 list, computed over the row(s) inserted or updated by the command\&.
554 If the specified table is a partitioned table, each row is routed to the appropriate partition and inserted into it\&. If the specified table is a partition, an error will occur if one of the input rows violates the partition constraint\&.
556 You may also wish to consider using
557 \fBMERGE\fR, since that allows mixing
561 within a single statement\&. See
565 Insert a single row into table
572 INSERT INTO films VALUES
573 (\*(AqUA502\*(Aq, \*(AqBananas\*(Aq, 105, \*(Aq1971\-07\-13\*(Aq, \*(AqComedy\*(Aq, \*(Aq82 minutes\*(Aq);
581 column is omitted and therefore it will have the default value:
587 INSERT INTO films (code, title, did, date_prod, kind)
588 VALUES (\*(AqT_601\*(Aq, \*(AqYojimbo\*(Aq, 106, \*(Aq1961\-06\-16\*(Aq, \*(AqDrama\*(Aq);
594 This example uses the
596 clause for the date columns rather than specifying a value:
602 INSERT INTO films VALUES
603 (\*(AqUA502\*(Aq, \*(AqBananas\*(Aq, 105, DEFAULT, \*(AqComedy\*(Aq, \*(Aq82 minutes\*(Aq);
604 INSERT INTO films (code, title, did, date_prod, kind)
605 VALUES (\*(AqT_601\*(Aq, \*(AqYojimbo\*(Aq, 106, DEFAULT, \*(AqDrama\*(Aq);
611 To insert a row consisting entirely of default values:
617 INSERT INTO films DEFAULT VALUES;
623 To insert multiple rows using the multirow
631 INSERT INTO films (code, title, did, date_prod, kind) VALUES
632 (\*(AqB6717\*(Aq, \*(AqTampopo\*(Aq, 110, \*(Aq1985\-02\-10\*(Aq, \*(AqComedy\*(Aq),
633 (\*(AqHG120\*(Aq, \*(AqThe Dinner Game\*(Aq, 140, DEFAULT, \*(AqComedy\*(Aq);
639 This example inserts some rows into table
643 with the same column layout as
650 INSERT INTO films SELECT * FROM tmp_films WHERE date_prod < \*(Aq2004\-05\-07\*(Aq;
656 This example inserts into array columns:
662 \-\- Create an empty 3x3 gameboard for noughts\-and\-crosses
663 INSERT INTO tictactoe (game, board[1:3][1:3])
664 VALUES (1, \*(Aq{{" "," "," "},{" "," "," "},{" "," "," "}}\*(Aq);
665 \-\- The subscripts in the above example aren\*(Aqt really needed
666 INSERT INTO tictactoe (game, board)
667 VALUES (2, \*(Aq{{X," "," "},{" ",O," "},{" ",X," "}}\*(Aq);
673 Insert a single row into table
674 distributors, returning the sequence number generated by the
682 INSERT INTO distributors (did, dname) VALUES (DEFAULT, \*(AqXYZ Widgets\*(Aq)
689 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:
696 UPDATE employees SET sales_count = sales_count + 1 WHERE id =
697 (SELECT sales_person FROM accounts WHERE name = \*(AqAcme Corporation\*(Aq)
700 INSERT INTO employees_log SELECT *, current_timestamp FROM upd;
706 Insert or update new distributors as appropriate\&. Assumes a unique index has been defined that constrains values appearing in the
708 column\&. Note that the special
710 table is used to reference values originally proposed for insertion:
716 INSERT INTO distributors (did, dname)
717 VALUES (5, \*(AqGizmo Transglobal\*(Aq), (6, \*(AqAssociated Computing, Inc\*(Aq)
718 ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED\&.dname;
724 Insert or update new distributors as above, returning information about any existing values that were updated, together with the new data inserted\&. Note that the returned values for
730 for non\-conflicting rows:
736 INSERT INTO distributors (did, dname)
737 VALUES (5, \*(AqGizmo Transglobal\*(Aq), (6, \*(AqAssociated Computing, Inc\*(Aq)
738 ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED\&.dname
739 RETURNING old\&.did AS old_did, old\&.dname AS old_dname,
740 new\&.did AS new_did, new\&.dname AS new_dname;
746 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
754 INSERT INTO distributors (did, dname) VALUES (7, \*(AqRedline GmbH\*(Aq)
755 ON CONFLICT (did) DO NOTHING;
761 Insert or update new distributors as appropriate\&. Example assumes a unique index has been defined that constrains values appearing in the
765 clause is used to limit the rows actually updated (any existing row not updated will still be locked, though):
771 \-\- Don\*(Aqt update existing distributors based in a certain ZIP code
772 INSERT INTO distributors AS d (did, dname) VALUES (8, \*(AqAnvil Distribution\*(Aq)
773 ON CONFLICT (did) DO UPDATE
774 SET dname = EXCLUDED\&.dname || \*(Aq (formerly \*(Aq || d\&.dname || \*(Aq)\*(Aq
775 WHERE d\&.zipcode <> \*(Aq21201\*(Aq;
777 \-\- Name a constraint directly in the statement (uses associated
778 \-\- index to arbitrate taking the DO NOTHING action)
779 INSERT INTO distributors (did, dname) VALUES (9, \*(AqAntwerp Design\*(Aq)
780 ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING;
786 Insert new distributor if possible; otherwise
787 DO NOTHING\&. Example assumes a unique index has been defined that constrains values appearing in the
789 column on a subset of rows where the
791 Boolean column evaluates to
798 \-\- This statement could infer a partial unique index on "did"
799 \-\- with a predicate of "WHERE is_active", but it could also
800 \-\- just use a regular unique constraint on "did"
801 INSERT INTO distributors (did, dname) VALUES (10, \*(AqConrad International\*(Aq)
802 ON CONFLICT (did) WHERE is_active DO NOTHING;
810 conforms to the SQL standard, except that the
814 extension, as is the ability to use
817 \fBINSERT\fR, and the ability to specify an alternative action with
818 ON CONFLICT\&. Also, the case in which a column name list is omitted, but not all the columns are filled from the
821 \fIquery\fR, is disallowed by the standard\&. If you prefer a more SQL standard conforming statement than
825 The SQL standard specifies that
826 OVERRIDING SYSTEM VALUE
827 can only be specified if an identity column that is generated always exists\&. PostgreSQL allows the clause in any case and ignores it if it is not applicable\&.
829 Possible limitations of the
831 clause are documented under