1

On PostgresSQL 12.2, running on macOS Catalina (10.15.4), have the following DB Schema:

CREATE TABLE public.reservation
(
    id integer,
    name character varying(255) COLLATE pg_catalog."default"
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public.reservation
    OWNER to postgres;

Here's listing the relations:

orders=# \d
            List of relations
 Schema |    Name     | Type  |  Owner   
--------+-------------+-------+----------
 public | reservation | table | postgres
(1 row)


After conducting the following inserts:

INSERT INTO reservation(name) VALUES('Michael');
INSERT INTO reservation(name) VALUES('Vito');
INSERT INTO reservation(name) VALUES('Sonny');
INSERT INTO reservation(name) VALUES('Fredo');

Then, checking the results:

SELECT * FROM reservation;

Receive the following output:

 id |  name  
----+--------
    | Michael
    | Vito
    | Sonny
    | Fredo
(4 rows)

How can I modify this table's structure (using DDL) to have it auto-increment ids with values?

2
  • Are you aware that a limit of 255 for a varchar column has no performance or storage advantage over 250, 263 or 3876 Commented May 10, 2020 at 19:02
  • @a_horse_with_no_name - Nope, I was not aware... I am coming from a MySQL background and transitioning to PostgreSQL and am following an online tutorial. Commented May 10, 2020 at 19:11

2 Answers 2

3

Just because you name a column id, doesn't mean it's an "auto increment" column. If you want that, you need to declare it as an identity column:

CREATE TABLE public.reservation
(
    id integer not null generated always as identity,
    name character varying(255) COLLATE pg_catalog."default"
);
Sign up to request clarification or add additional context in comments.

Comments

0

In Postgres, use SERIAL datatype for auto-increment integer id.

Run the below queries

CREATE SEQUENCE public.reservation_id_seq
    INCREMENT 1
    START 1
    MINVALUE 1
    MAXVALUE 2147483647
    CACHE 1;

ALTER SEQUENCE public.reservation_id_seq
    OWNER TO postgres;

AND THEN

CREATE TABLE public.reservation
(
    id integer NOT NULL DEFAULT 
    nextval('public.reservation_id_seq'::regclass),
    name character varying(255) COLLATE pg_catalog."default"
)

TABLESPACE pg_default;

ALTER TABLE public.reservation
    OWNER to postgres;

Thank you !!

5 Comments

With modern Postgres version using identity columns is preferred over serial: wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_serial
@Prateek Sharma - Thank you but can you kind of explain why I needed to use sequence for this? Sorry, I come from a MySQL background.
sequence is to track the latest id in an incremental order. It always +1 in the current id.
@PacificNW_Lover: you don't have to use sequences explicitly. An identity (or serial) column will do that automatically for you in the background.
@a_horse_with_no_name - How can I use identity`` instead of serial```? Can you provide an example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.