postgresql


INSERT All Versions

8.0
8.1
8.2
8.3
8.4
9.0
9.1
9.2
9.3
9.4
9.5

This draft deletes the entire topic.

inline side-by-side expand all collapse all

Examples

  • 1

    You can insert multiple rows in the database at the same time:

    INSERT INTO person (name, age) VALUES 
      ('john doe', 25),
      ('jane doe', 20);
    
  • 0

    Let's say we have a simple table called person:

    CREATE TABLE person (
        person_id BIGINT,
        name VARCHAR(255).
        age INT,
        city VARCHAR(255)
    );
    

    The most basic insert involves inserting all values in the table:

    INSERT INTO person VALUES (1, 'john doe', 25, 'new york');
    

    If you want to insert only specific columns, you need to explicitly indicate which columns:

    INSERT INTO person (name, age) VALUES ('john doe', 25);
    

    Note that if any constraints exist on the table , such as NOT NULL, you will be required to include those columns in either case.

  • 0

    You can insert data in a table as the result of a select statement:

    INSERT INTO person SELECT * FROM tmp_person WHERE age < 30;
    

    Note that the projection of the select must match the columns required for the insert. In this case, the tmp_person table has the same columns as person.

I am downvoting this example because it is...

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have question about INSERT? Ask Question

Inserting multiple rows

1

You can insert multiple rows in the database at the same time:

INSERT INTO person (name, age) VALUES 
  ('john doe', 25),
  ('jane doe', 20);

Basic INSERT

0

Let's say we have a simple table called person:

CREATE TABLE person (
    person_id BIGINT,
    name VARCHAR(255).
    age INT,
    city VARCHAR(255)
);

The most basic insert involves inserting all values in the table:

INSERT INTO person VALUES (1, 'john doe', 25, 'new york');

If you want to insert only specific columns, you need to explicitly indicate which columns:

INSERT INTO person (name, age) VALUES ('john doe', 25);

Note that if any constraints exist on the table , such as NOT NULL, you will be required to include those columns in either case.

Insert from select

0

You can insert data in a table as the result of a select statement:

INSERT INTO person SELECT * FROM tmp_person WHERE age < 30;

Note that the projection of the select must match the columns required for the insert. In this case, the tmp_person table has the same columns as person.

Topic Outline