6

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?

2 Answers 2

13

According to INSERT syntax:

insert into books default values
returning id;
Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

Is there a way to specify all values as DEFAULT in one expression?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.