← Back to PostgreSQL Course | Chapter 4: Inserting & Modifying Data | Lesson 1 of 7

INSERT INTO

INSERT INTO adds a new row to a table.

In this page:

  1. INSERT INTO
Syntax
sql
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);

INSERT INTO

Name the columns and supply matching values: INSERT INTO t (a, b) VALUES (1, x). Omitted columns take their DEFAULT or NULL. Listing columns makes statements robust when the table changes. INSERT ... SELECT copies rows from a query.

Note: Always list column names in INSERT statements.

Example: INSERT INTO

sql
CREATE TABLE members (id INTEGER PRIMARY KEY, name TEXT NOT NULL, plan TEXT DEFAULT 'free', age INTEGER);
INSERT INTO members (id, name, age) VALUES (1, 'Ada', 36);
INSERT INTO members (id, name, plan) VALUES (2, 'Bob', 'pro');
SELECT * FROM members;

-- Output:
-- id | name | plan | age
-- 1 | Ada | free | 36
-- 2 | Bob | pro | NULL
Related Topics
Common Mistakes
  1. Relying on column order without naming columns
  2. Wrong number of values
  3. Forgetting quotes around text
Chapter Summary
  • INSERT INTO table (cols) VALUES (values)
  • Omitted columns use defaults
  • List the columns explicitly
  • INSERT ... SELECT copies rows
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.