Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres Insert without ANY VALUES FOR COLUMNS. ALL ARE DEFAULT

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.

like image 795
shoke Avatar asked Apr 30 '19 15:04

shoke


2 Answers

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

like image 199
Jaisus Avatar answered Oct 18 '22 08:10

Jaisus


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;
like image 36
TheWildHealer Avatar answered Oct 18 '22 07:10

TheWildHealer