Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Use On Conflict Do Nothing In Postgres Like Insert Or Ignore In Other Databases

Postgres doesn't have "INSERT OR IGNORE"

Instead, use :

sql
ON CONFLICT (column_name) DO NOTHING;

For example, given this table setup :

sql
CREATE TABLE IF NOT EXISTS public.states (
    db_id SERIAL PRIMARY KEY,
    abbreviation VARCHAR(2) NOT NULL UNIQUE,
    full_name VARCHAR(30)
);

ALTER TABLE public.states ENABLE ROW LEVEL SECURITY;

This INSERT statement can be run multiple times :

INSERT INTO public.states (abbreviation, full_name)
VALUES ('AL', 'Alabama')
ON CONFLICT (abbreviation) DO NOTHING;
;

The first time will insert the data. Subsequent runs will be skipped without throwing errors.