TExcelTAD
Home › PostgreSQL › PostgreSQL INSERT INTO

PostgreSQL INSERT INTO

INSERT INTO adds new rows to a table. You name the table, list the columns you are filling, and give the values in the same order.

Inserting one row

pgsql · try it yourself
INSERT INTO categories (name) VALUES ('Cookery');

SELECT * FROM categories WHERE id > 5;
INSERT 0 1
 id |   name
----+----------
  6 | Children
  7 | Poetry
  8 | Cookery
(3 rows)

The command tag INSERT 0 1 has two numbers: the first is historical and is always 0, the second is the number of rows inserted. We did not provide an id, so the identity column gave the new row the next number, 8.

Inserting several rows at once

A single INSERT can carry many rows, separated by commas. This is faster than many single inserts and is applied all-or-nothing:

pgsql · try it yourself
INSERT INTO authors (name, country, birth_year) VALUES
    ('Kofi Mensah',   'Ghana',  1983),
    ('Lena Hofmann',  'Austria', 1979),
    ('Ruth Abebe',    'Ethiopia', NULL);

SELECT id, name, country, birth_year
FROM authors
WHERE id > 9;
INSERT 0 3
 id |     name     | country  | birth_year
----+--------------+----------+------------
 10 | Grace Liu    | Canada   |       1985
 11 | Kofi Mensah  | Ghana    |       1983
 12 | Lena Hofmann | Austria  |       1979
 13 | Ruth Abebe   | Ethiopia |
(4 rows)

NULL means "unknown": Ruth Abebe's birth year was not recorded.

Column list and defaults

Columns you leave out of the list get their default value (or NULL if they have none). In books, stock defaults to 0, tags to an empty array and details to an empty JSON object:

pgsql · try it yourself
INSERT INTO books (title, author_id, category_id, price, published)
VALUES ('Quiet Rivers', 10, 1, 16.40, '2025-09-01');

SELECT id, title, price, stock, tags, details
FROM books
WHERE title = 'Quiet Rivers';
INSERT 0 1
 id |    title     | price | stock | tags | details
----+--------------+-------+-------+------+---------
 21 | Quiet Rivers | 16.40 |     0 | {}   | {}
(1 row)

You can also write the keyword DEFAULT in place of a value, or use INSERT INTO t DEFAULT VALUES to create a row made only of defaults.

Tip: Always write the column list. INSERT INTO categories VALUES (...) without it depends on the exact column order of the table and breaks as soon as someone adds a column.

Common mistakes

Each of these statements fails for a different reason. Read the messages:

pgsql · try it yourself
INSERT INTO categories (name) VALUES ('Fiction');
INSERT INTO customers (first_name, last_name) VALUES ('Zoe', 'Park');
INSERT INTO books (title, author_id, category_id, price, published)
VALUES ('Too Cheap', 1, 1, -5, '2025-01-01');
INSERT INTO orders (customer_id, order_date, status) VALUES (1, 'yesterday-ish', 'pending');
ERROR:  duplicate key value violates unique constraint "categories_name_key"
DETAIL:  Key (name)=(Fiction) already exists.
ERROR:  null value in column "city" of relation "customers" violates not-null constraint
DETAIL:  Failing row contains (16, Zoe, Park, null, null, null, null).
ERROR:  new row for relation "books" violates check constraint "books_price_check"
DETAIL:  Failing row contains (21, Too Cheap, 1, 1, -5.00, 0, 2025-01-01, {}, {}).
ERROR:  invalid input syntax for type date: "yesterday-ish"
LINE 1: ...omer_id, order_date, status) VALUES (1, 'yesterday-ish', 'pe...
                                                   ^
  1. name in categories is UNIQUE, and Fiction already exists.
  2. city, country and joined are NOT NULL and have no default.
  3. The CHECK (price > 0) constraint rejects a negative price.
  4. The text cannot be converted to a date.

Inserting the result of a query

INSERT INTO … SELECT copies rows from a query. Here we put every UK customer on a newsletter list:

pgsql · try it yourself
CREATE TABLE newsletter (email text PRIMARY KEY, city text);

INSERT INTO newsletter (email, city)
SELECT email, city FROM customers WHERE country = 'UK';

SELECT * FROM newsletter ORDER BY email;
CREATE TABLE
INSERT 0 3
         email          |  city
------------------------+---------
 ben.carter@example.com | Leeds
 isla.m@example.com     | Glasgow
 olivia.m@example.com   | Leeds
(3 rows)

To see the generated id right away, add RETURNING id to an INSERT; the Upsert & RETURNING lesson shows how.

Test yourself

What does the command tag INSERT 0 3 tell you?

  1. Insert failed 3 times
  2. 3 rows were inserted
  3. Row 3 was inserted
  4. 0 rows were inserted

You insert a book without a stock value. What is stored, given stock integer NOT NULL DEFAULT 0?

  1. NULL
  2. 0
  3. An error is raised
  4. The previous row's stock

How do you insert three rows with one statement?

  1. Three VALUES keywords
  2. VALUES (...), (...), (...)
  3. INSERT MANY
  4. It is not possible