Search code examples
sqlpostgresqlauto-increment

Remove autoincrement from postgresql field


I have an existing table in a db, FK'd from several others, SQL below:

CREATE TABLE forecastsource (
    source_id integer DEFAULT nextval(('public.forecastsource_source_id_seq'::text)::regclass) NOT NULL,
    source_name character varying NOT NULL
);

I want to remove the autoincrement from the id field, and just move it to be a int field (without losing the current data in the table). How would I do that, other than dropping and recreating the table?


Solution

  • Just drop the default value:

    ALTER TABLE forecastsource ALTER COLUMN source_id DROP DEFAULT;
    

    You probably also want to drop the sequence then.