17

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.

b.lyte
  • 6,518
  • 4
  • 40
  • 51

2 Answers2

28

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.)

Jaisus
  • 1,019
  • 5
  • 14
6

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;
TheWildHealer
  • 1,546
  • 1
  • 15
  • 26