I need to copy records within the same table changing only one field.
My table has default-generated sequence entry_id_seq
, however I am not sure id
column is SERIAL (how to check that?).
\d tab
returns just this
Column | Type | Modifiers
-----------------+--------------------------------+------------------------
id | integer | not null
...
Indexes:
"tab_entry_pkey" PRIMARY KEY, btree (id)
So the problem is: when I try copy record in a simplified manner:
insert into tab_entry select * from tab_entry where id = 3052;
it throws error
ERROR: duplicate key value violates unique constraint "tab_entry_pkey"
DETAIL: Key (id)=(3052) already exists.
Default sequence doesn't generate next value by default. Is there any concise syntax allowing inserting and changing single field without full table specification aka FROM tab(col1, col2, col3, ..., col N)
?
The table has many fields, so I don't want to write them all, as this will affect code readability. I want something like this, but this syntax doesn't work
insert into tab_entry(id, *) select nextval('seq'), * from tab_entry where id = 3052;
And will this SELECT nextval('seq')
approach work if there be multiple records at once?
Psql and Postgres version is 9.6.2.
serial
is: dba.stackexchange.com/a/47107/6219insert into tab_entry(id,f1,f2,f3,..) select nextval('seq'),f1,f2,f3,... from tab_entry where id = 3052;