22

I have a table in Postgres that only has default column values (id, created_at).

The only way I can insert into this table is

INSERT INTO pages(id) VALUES(DEFAULT) RETURNING id;

Why can't I do this:

INSERT INTO pages RETURNING id;

Just curious.

0

2 Answers 2

32

You can use either :

INSERT INTO test DEFAULT VALUES returning id;

And all the explanations you want are right here : https://www.postgresql.org/docs/current/sql-insert.html

The syntax of PostgreSQL is quite imposed.

DEFAULT VALUES : All columns will be filled with their default values. (An OVERRIDING clause is not permitted in this form.)

Sign up to request clarification or add additional context in comments.

Comments

8

you need the values keyword, otherwise how would you tell how many rows you want to insert? However, you do not require the field names, so you can shorten your query a (very) little bit:

INSERT INTO pages VALUES(DEFAULT) RETURNING id;

Comments

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.