INSERT (Insert Rows)
Last updated: 2026-11-04
INSERT (Insert Rows)
INSERT adds new rows to a table. It accepts multiple rows in a single statement — a multi-row VALUES list is far faster than repeated single-row inserts — and it can also copy rows from a query with INSERT ... SELECT. The RETURNING clause hands back the freshly inserted rows, while ON CONFLICT handles unique-key collisions.
| Kind | command |
|---|---|
| Example Language | SQL |
📝 Syntax
INSERT INTO <table> [(<column>, ...)]
VALUES (<value>, ...), (<value>, ...), ...
[ON CONFLICT (<conflict target>) DO NOTHING | DO UPDATE SET ...]
[RETURNING <column or expression>]
⚙️ Parameters
| INTO | The table rows are inserted into |
|---|---|
| VALUES | The data to insert; may contain several tuples |
| ON CONFLICT | What to do when a unique key conflicts |
| RETURNING | Returns the inserted rows |
Returns:The number of affected rows; with RETURNING, the inserted rows are returned as well.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
DROP TABLE
CREATE TABLE
id | name | price
---+---------------------+------
1 | Mechanical Keyboard | 79.99
2 | USB-C Cable | 12.50
3 | Monitor Stand | 49.00
📌 Key Points
- A multi-row VALUES list sends one statement and scans the table once — much faster than repeated single-row inserts.
- A GENERATED ALWAYS AS IDENTITY column requires OVERRIDING SYSTEM VALUE to be set explicitly; GENERATED BY DEFAULT allows it.
- RETURNING may reference inserted columns and DEFAULT expressions, so validation happens as the row lands.
- A unique-key clash raises duplicate key value violates unique constraint; use ON CONFLICT to handle it.
- Omitting the column list relies on the physical column order; explicit column names are safer.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhat is the difference between INSERT and INSERT ... SELECT?
AThe former inserts literal VALUES, the latter copies an entire query result — handy for moving data between tables.
QHow do I get the generated id right after inserting?
AUse RETURNING id: the statement hands the new row straight back, and with ON CONFLICT it returns the row after conflict handling.