Description
Use the INSERT command to add new rows into a table. This can be done either one row at a time, or in sets. Used with the VALUES keyword, an INSERT statement can only insert one row of data. To insert multiple rows, you can instead supply a query. Results from the query are then fed into the INSERT command's target table.
If an incorrect data type is provided for a field on insertion, PostgreSQL will attempt to automatically coerce it into the appropriate type. If it cannot, the INSERT will fail.
When inserting values into columns (instead of whole rows), the columns can be listed in any order; however, the values for those columns will need to be listed in the same order.
Note: If you leave out values for any fields in your table, the database will automatically do one of two things. Fields for which you have not specified a default value will be set to NULL. Fields for which you
have
specified a default value will be set to their defaults.
Examples
The following example inserts a single row into the employees table:
booktown=#
INSERT INTO employees
booktown-#
VALUES (106, 'Hall', 'Timothy');
INSERT 3752064 1
Alternatively, you can insert only an ID number and last name, and not a first name, by specifying a target column list preceding the VALUES clause. This results in a NULL value for the first_name column in the new row:
booktown=#
INSERT INTO employees (id, last_name)
booktown=#
VALUES (108, 'Williams');
INSERT 3752065 1
The next example inserts all 15 rows from the books table into the book_backup table by providing a query from which to insert data:
booktown=#
INSERT INTO book_backup
booktown-#
SELECT * FROM books;
INSERT 0 15