sdgfsdh
sdgfsdh

Reputation: 37065

How do I create a new row with all default values in Postgres?

I have a table with two columns, id and created. Both have default values, so I should be able to insert a new row without supplying any data.

However, this syntax does not work:

INSERT INTO books () VALUES () 

I would also like to return the generated id of the inserted row. This syntax also does not work:

INSERT INTO books () VALUES () RETURNING id 

How do I write this query in Postgres SQL?

Upvotes: 6

Views: 1734

Answers (2)

Vao Tsun
Vao Tsun

Reputation: 51529

here is example (basically, just use DEFAULT):

t=# create table d(i int default 0, t text default 'a');
CREATE TABLE
t=# insert into d values(DEFAULT,DEFAULT) returning *;
 i | t
---+---
 0 | a
(1 row)

INSERT 0 1

Upvotes: 3

klin
klin

Reputation: 121634

According to INSERT syntax:

insert into books default values
returning id;

Upvotes: 13

Related Questions