Home > Mobile >  Whats the proper way to activate autoincrementation on PostgreSQL primary key?
Whats the proper way to activate autoincrementation on PostgreSQL primary key?

Time:10-15

Starting on using PostgreSQL, I am currently having a situation where my primary gets updated only from the code, but not frm script I am running at init...

I have the 3 following SQL file:

// 1.
GRANT ALL PRIVILEGES ON DATABASE testdb TO testapi;

// 2.
CREATE TABLE IF NOT EXISTS cities (
    id SERIAL PRIMARY KEY UNIQUE,
    name VARCHAR ( 50 ) UNIQUE NOT NULL
);

CREATE SEQUENCE IF NOT EXISTS cities_seq_id;
SELECT setval('cities_seq_id', (SELECT max(id) 1 FROM cities), false);
ALTER TABLE cities ALTER COLUMN id SET DEFAULT nextval('cities_seq_id');

// 3.
INSERT INTO cities (name)
VALUES ('Barcelona'),
       ('Seville'),
       ('Madrid'),
       ('Valencia'),
       ('Andorra la Vella'),
       ('Malaga');

Starting postgreSQL like so:

windows: docker run --name postgres -v            
  • Related