A
A
Alexander2021-01-18 11:38:42
MySQL
Alexander, 2021-01-18 11:38:42

How to do AUTO_INCREMENT in postgresql?

What can replace the following lines from MySQL to PostgreSQL?

ALTER TABLE `commands`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-01-18
@Alexandre888

If you need to create a table, then autoincrement is done like this:

CREATE TABLE commands (
id serial PRIMARY KEY
)

if you need to change the table, then first create the sequence that serial creates, and then apply it to the column

M
Miron, 2021-01-23
@Miron11

I prefer sequences. About an hour ago, when I found the question, it turned out that my pgAdmin 4 was seriously lagging behind. I installed a new one, already 64-bit, connected to the desktop service, and only through the GUI built the following objects, in no hurry. Everything is amazing and comfortable. Hope it comes in handy :)
CREATE SEQUENCE public.testincrement_sequence
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;
CREATE TABLE public.testincrement
(
i integer NOT NULL DEFAULT nextval('testincrement_sequence'::regclass),
a character varying(250) COLLATE pg_catalog."default",
dt timestamp without time zone,
un character varying(128) COLLATE pg_catalog."default"
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question