42P06
PostgreSQLERRORNotableSyntax Error or Access Rule ViolationHIGH confidence

duplicate schema

What this means

SQLSTATE 42P06 is raised when CREATE SCHEMA is called with a name that already exists and IF NOT EXISTS is not specified.

Why it happens
  1. 1CREATE SCHEMA specifying a schema name that already exists in the database
How to reproduce

Creating a schema that already exists.

trigger — this will error
trigger — this will error
CREATE SCHEMA myschema; -- myschema already exists

expected output

ERROR:  schema "myschema" already exists

Fix

Use CREATE SCHEMA IF NOT EXISTS

WHEN In migration scripts that may run multiple times.

Use CREATE SCHEMA IF NOT EXISTS
CREATE SCHEMA IF NOT EXISTS myschema;

Why this works

IF NOT EXISTS silently succeeds if the schema already exists, making the command idempotent.

Sources
Official documentation ↗

Class 42 — Syntax Error or Access Rule Violation (Postgres-specific)

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors