Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
42P06 means the schema name you are creating is already taken in this database. It usually shows up when a setup or migration script runs a second time.
In the lab, CREATE SCHEMA sales failed because sales already existed, and CREATE SCHEMA AUTHORIZATION lab_a failed because a schema named after the user already existed. CREATE SCHEMA IF NOT EXISTS turned the error into a NOTICE and the rest of the script ran.
01The script creates a schema that already exists
Schema sales was already there (owner lab_a). Running CREATE SCHEMA sales; again failed with schema "sales" already exists.
With CREATE SCHEMA IF NOT EXISTS sales; PostgreSQL printed NOTICE: ... already exists, skipping instead of an error, and the next CREATE TABLE IF NOT EXISTS sales.orders created the table.
Setup
GRANT CREATE ON DATABASE lab TO lab_a;
CREATE SCHEMA sales AUTHORIZATION lab_a;GRANT
CREATE SCHEMACREATE SCHEMA sales;ERROR: 42P06: schema "sales" already existsSELECT nspname, pg_get_userbyid(nspowner) AS owner FROM pg_namespace WHERE nspname = 'sales'; nspname | owner
---------+-------
sales | lab_a
(1 row)Look the schema up in pg_namespace first. If it is the one you want, use IF NOT EXISTS so the script can run more than once.
CREATE SCHEMA IF NOT EXISTS sales;
CREATE TABLE IF NOT EXISTS sales.orders (id int PRIMARY KEY);
\dt sales.*NOTICE: 42P06: schema "sales" already exists, skipping
CREATE SCHEMA
CREATE TABLE
List of relations
Schema | Name | Type | Owner
--------+--------+-------+-------
sales | orders | table | lab_a
(1 row)02CREATE SCHEMA AUTHORIZATION <user> when the user already has its own schema
CREATE SCHEMA AUTHORIZATION lab_a; without a name creates a schema named after the user, lab_a. That schema already existed, so it failed with schema "lab_a" already exists.
pg_namespace showed lab_a owned by lab_a, and search_path was "$user", public, so the user schema is already the default. CREATE SCHEMA IF NOT EXISTS AUTHORIZATION lab_a; skipped it, and a table created without a schema name went into lab_a.
Setup
GRANT CREATE ON DATABASE lab TO lab_a;GRANTCREATE SCHEMA AUTHORIZATION lab_a;ERROR: 42P06: schema "lab_a" already existsSELECT nspname, pg_get_userbyid(nspowner) AS owner FROM pg_namespace WHERE nspname = current_user;
SHOW search_path; nspname | owner
---------+-------
lab_a | lab_a
(1 row)
search_path
-----------------
"$user", public
(1 row)With the default search_path ("$user", public) a schema named after the user is used automatically, so you do not need to create it again.
CREATE SCHEMA IF NOT EXISTS AUTHORIZATION lab_a;
CREATE TABLE items (id int);
SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'items';NOTICE: 42P06: schema "lab_a" already exists, skipping
CREATE SCHEMA
CREATE TABLE
schemaname | tablename
------------+-----------
lab_a | items
(1 row)FAQ
How do I fix "schema already exists" in PostgreSQL?
Use CREATE SCHEMA IF NOT EXISTS name;. In the lab it printed a NOTICE (already exists, skipping) instead of an error and the script continued.
How do I check whether a schema exists?
Query pg_namespace: SELECT nspname, pg_get_userbyid(nspowner) AS owner FROM pg_namespace WHERE nspname = 'sales'; or use \dn in psql.
Why does CREATE SCHEMA AUTHORIZATION fail with 42P06?
Without a schema name it uses the user name. If a schema with that name already exists, you get 42P06. In the lab it was schema "lab_a" already exists.
Is 42P06 the same as 42P07?
No. 42P06 is for schemas, 42P07 is for tables, indexes and other relations that already exist.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12