Re: removing "serial" from table definitions.

From: Johannes Paul <treeclimate(at)gmail(dot)com>
To: Marc Mamin <M(dot)Mamin(at)intershop(dot)de>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: removing "serial" from table definitions.
Date: 2021-06-24 12:42:37
Message-ID: CAC5HKqDuK0JAx0Pc7UM8kgZ=Js4nWuS7A91nN61YKQuTL4sRFw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

From what I know, serial is just used to setup a table but it is then
converted to int in the table. Therefore, you probably cannot remove it
since it is not there any more anyway.

To setup table with int instead of serial, you could use this:

CREATE SEQUENCE table_name_id_seq;

CREATE TABLE table_name (
id integer NOT NULL DEFAULT nextval('table_name_id_seq')
);

ALTER SEQUENCE table_name_id_seq
OWNED BY table_name.id;

instead of

CREATE TABLE table_name(
id SERIAL
);

as explained on
https://www.postgresqltutorial.com/postgresql-serial/

Am Do., 24. Juni 2021 um 14:33 Uhr schrieb Marc Mamin <M(dot)Mamin(at)intershop(dot)de
>:

> Hi,
>
> Is there a way to change a data type from serial to int?
>
> I tried with :
>
> ALTER TABLE "admin".db_jobs ALTER COLUMN id TYPE int USING id::int;
>
> But this seems not to change anything, as if Posgres woud consider the
> statement as a no-op.
>
>
>
> My problem is that "serial" is not exported with pg_dump.
>
> Creating a db from the dump will hence result into a different table
> definition (which is equivalent tough)
>
>
>
> We are trying a transfer/migration tool on Azure, that check the table
> definitions between the source and target before starting the data
> transfer, and it blocks on that difference.
>
>
>
> best regards,
>
> Marc Mamin
>
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joe Conway 2021-06-24 12:47:14 Re: removing "serial" from table definitions.
Previous Message Marc Mamin 2021-06-24 12:33:03 removing "serial" from table definitions.