Re: Adding 'serial' to existing column

From: rob stone <floriparob(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Lakes <robertl(at)propaas(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Adding 'serial' to existing column
Date: 2017-11-03 21:07:03
Message-ID: 1509743223.13050.10.camel@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 2017-11-03 at 16:47 -0400, Tom Lane wrote:
> Robert Lakes <robertl(at)propaas(dot)com> writes:
> > I am new to Postgres and I am trying to build this SQL statement in
> > my SQL
> > script:
> > ALTER TABLE listings_cdc ALTER COLUMN table_id SET DEFAULT
> > nextval('tab_id_seq');
>
> That looks correct, but this not so much:
>
> > I am trying to build the above-stated command as a dynamic SQL
> > statement:
> > EXECUTE 'ALTER TABLE listings_cdc ALTER COLUMN table_id SET
> > DEFAULT
> > nextval('||quote_ident(tab_id_seq)||')';
> > I've received an error when running the script this above-stated
> > command:
> > ERROR: column "tab_id_seq" does not exist
> > LINE 1: ...OLUMN table_id SET DEFAULT
> > nextval('||quote_ident(tab_id_seq...
>
> You want quote_literal, not quote_ident, because you're trying to
> produce
> a single-quoted literal.
>
> regards, tom lane
>
>

You could also do:-

ALTER SEQUENCE tab_id_seq OWNED BY listings_cdc.table_id;

which would establish the one-to-one relationship between the table and
its sequence for that column.

My 2 cents worth.

HTH,
Rob

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rhhh Lin 2017-11-03 21:12:02 Re: EXPLAIN <query> command just hangs...
Previous Message Rhhh Lin 2017-11-03 20:55:09 Re: EXPLAIN <query> command just hangs...