Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 776
Marc Avatar asked Jul 17 '16 20:07

Marc


People also ask

How do I remove Autoincrement?

In MySQL, the syntax to reset the AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = value; table_name. The name of the table whose AUTO_INCREMENT column you wish to reset.

Is there auto increment in PostgreSQL?

PostgreSQL has the data types smallserial, serial and bigserial; these are not true types, but merely a notational convenience for creating unique identifier columns. These are similar to AUTO_INCREMENT property supported by some other databases.


1 Answers

Just drop the default value:

ALTER TABLE forecastsource ALTER COLUMN source_id DROP DEFAULT;

You probably also want to drop the sequence then.

like image 175
a_horse_with_no_name Avatar answered Sep 19 '22 16:09

a_horse_with_no_name