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
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
-
Make the statement idempotent.
CREATE SCHEMA IF NOT EXISTS sales AUTHORIZATION app;
-
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
3F000schema does not exist42P07relation already exists42710duplicate object42P04database already exists
More syntax error or access rule violation errors
42601syntax error at or near42501permission denied42846cannot coerce42803column must appear in the GROUP BY clause42830invalid foreign key42804datatype mismatch42P18could not determine data type of parameter42703column does not exist
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.
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.