Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert multiple default rows into a table in PostgresQL

I have a table with columns taking default values:

create table indexing_table
(
  id SERIAL PRIMARY KEY,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
);

How do I insert multiple default rows into this table? Do I have to repeat the command:

insert into indexing_table default values;

as many times as I want it to be inserted?

like image 418
Alex Avatar asked Jan 29 '15 00:01

Alex


1 Answers

When you have a default value, you can tell the database to use this default value:

INSERT INTO indexing_table(id, created_at) 
VALUES(default, default), 
  (default, default), 
  (default, default);

If you need hundreds of default records and one of your defaults is "now()", use generate_series():

INSERT INTO indexing_table(created_at)
SELECT NOW() FROM generate_series(1,100);
like image 96
Frank Heikens Avatar answered Oct 20 '22 04:10

Frank Heikens