Resetting identity columns - Mailing list pgsql-general

From Ray O'Donnell
Subject Resetting identity columns
Date
Msg-id 5db0ed5c-00ba-65fa-92f0-7e02c1495d18@rodonnell.ie
Whole thread Raw
Responses Re: Resetting identity columns
Re: Resetting identity columns
Re: Resetting identity columns
List pgsql-general
Hi all,

I'm probably doing something silly.... I'm migrating data from one 
database table to another, where the old table used a SERIAL primary key 
and the new one uses GENERATED BY DEFAULT AS IDENTITY. Having loaded the 
data into the new table, I need to reset the underlying sequence so that 
it picks up from the highest existing value.

I'm using PostgreSQL 11.2 on Debian 9.

I've tried:

=# alter table orders alter column order_id restart with (
select max(order_id) + 1 from orders);

ERROR:  syntax error at or near "("
LINE 1: ...r table orders alter column order_id restart with (select ma...


I also tried it with a DO block:

=# do language plpgsql $$
$# declare m_max_id bigint;
$# begin
$# select max(order_id) + 1 from orders into m_max_id;
$# alter table orders alter column order_id restart with m_max_id;
$# end;
$# $$;

ERROR:  syntax error at or near "m_max_id"
LINE 5: ...er table orders alter column order_id restart with m_max_id;


What am I missing?

I should add that this is part of a larger migration script; otherwise I 
could just do it by hand the command line.

Thanks in advance,

Ray.

-- 
Raymond O'Donnell // Galway // Ireland
ray@rodonnell.ie



pgsql-general by date:

Previous
From: Scot Kreienkamp
Date:
Subject: RE: Streaming Replication
Next
From: Adrian Klaver
Date:
Subject: Re: Resetting identity columns