// PostgreSQL error reference

PostgreSQL 42P06: schema already exists

CREATE SCHEMA named a schema that already exists in this database. Add IF NOT EXISTS or choose another name.

SQLSTATE
42P06
Name
duplicate_schema
Category
Syntax Error or Access Rule Violation
Error message
schema "sales" already exists

What it means

Schema names are unique within a database. Creating one that exists fails with 42P06, typically from a migration or setup script that ran before. CREATE SCHEMA IF NOT EXISTS (PostgreSQL 9.3 and later) turns it into a notice.

Common causes

  • A setup or migration script running a second time.
  • Two tools or extensions that create the same schema.

How to fix it

  1. Make the statement idempotent.

    CREATE SCHEMA IF NOT EXISTS sales AUTHORIZATION app;
  2. Check who owns the existing schema if permissions look wrong afterwards.

    SELECT nspname, pg_get_userbyid(nspowner) AS owner FROM pg_namespace WHERE nspname = 'sales';

Reproduce it safely

CREATE SCHEMA public;  -- ERROR: schema "public" already exists

Related errors

More syntax error or access rule violation errors

The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.

// fewer errors, faster fixes

Work on PostgreSQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.