PostgreSQL Error 42P06: Duplicate Schema Fix
In this article
Error 42P06 means the schema name you are trying to create already exists in the current database. PostgreSQL raises it when a CREATE SCHEMA statement collides with an existing schema, and the fix is either to use the existing schema, rename your new one, or drop the old one if it is truly unused. The error looks like this: ERROR: schema "reporting" already exists with SQLSTATE 42P06. Because schemas are scoped to a single database, the same name can exist in another database without causing this error, so first confirm you are connected to the right database.
What You'll Need
- Access to the PostgreSQL server, either through
psqlor a client such as pgAdmin or DBeaver. - A role with permission to create schemas in the target database (typically the database owner or a superuser).
- The exact schema name that triggered the error, and the name of the database you were connected to.
- Optional but useful: the ability to run
SELECTqueries againstinformation_schema.schemataorpg_namespaceto inspect existing schemas.
Step-by-Step
- Confirm the current database. In
psql, runSELECT current_database();. Schemas are per-database, so if you expected a fresh database and see a different name, you are in the wrong place. Reconnect with\c your_databasebefore doing anything else. - List the schemas that exist. Run
SELECT schema_name FROM information_schema.schemata ORDER BY schema_name;or, for more detail including the owner,SELECT nspname, pg_get_userbyid(nspowner) AS owner FROM pg_namespace ORDER BY nspname;. Look for the name in the error message. Remember that every database already containspublic,information_schema, andpg_catalog; those are normal. - Decide which outcome you want. There are three legitimate paths: reuse the existing schema, create your new schema under a different name, or drop the existing schema if it is empty or obsolete. Pick one before running more SQL.
- Reuse the existing schema. If the schema already holds the tables you need, simply stop creating it. Set your search path so unqualified names resolve there:
SET search_path TO reporting, public;. To make it permanent for a role, useALTER ROLE your_role SET search_path TO reporting, public;. You can also qualify objects directly, for exampleCREATE TABLE reporting.events (...);. - Create the schema under a new name. If the existing schema belongs to another application, choose a distinct name such as
reporting_v2and runCREATE SCHEMA reporting_v2;. Optionally add an owner:CREATE SCHEMA reporting_v2 AUTHORIZATION your_role;. - Make creation idempotent in scripts. If the schema creation is part of a migration or setup script, guard it with
CREATE SCHEMA IF NOT EXISTS reporting;. This suppresses 42P06 when the schema is already present. Note thatIF NOT EXISTSonly skips the error; it does not verify that the existing schema has the tables or columns your script expects, so pair it with explicit checks if the schema contents matter. - Drop the old schema only if you are certain. First inspect what is inside:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'reporting';. If it is empty,DROP SCHEMA reporting;succeeds. If it contains objects, you must useDROP SCHEMA reporting CASCADE;, which permanently deletes every table, view, function, and sequence inside it. Take a backup first withpg_dump -n reporting your_database > reporting_backup.sql. There is no undo. - Re-run your original statement. After choosing a path, execute the original
CREATE SCHEMA(with the new name if you renamed) and confirm success with\dninpsqlor the equivalent schema list in your client.
Common Mistakes
- Checking the wrong database. Running the schema list in
postgreswhen your application usesappdbwill show no conflict, yet the error persists on the real connection. Always verifycurrent_database(). - Assuming a schema is empty. A schema can contain only functions or types, not tables, so a table-only check misses them. Use
SELECT count(*) FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'reporting';for a fuller picture. - Using
CASCADEcasually. Dropping a schema withCASCADEremoves dependent objects, and if other schemas reference them, those references break too. Confirm with a backup before running it. - Confusing schema with database.
CREATE DATABASEandCREATE SCHEMAare different; a duplicate database raises a different error (42P04), not 42P06. - Relying on
IF NOT EXISTSas a migration strategy. It hides drift: if the existing schema has an older table layout, your script will silently skip creation and later statements may fail on missing columns.
Tips
- Adopt a naming convention that includes the application or environment, such as
app_prodandapp_staging, to reduce accidental collisions. - Keep schema creation in a versioned migration file rather than ad-hoc SQL, so every environment follows the same path.
- Use
\dn+inpsqlto see schemas with their owners and access privileges at a glance. - When restoring a dump, remember it may already contain
CREATE SCHEMAstatements; restoring into a database that already has those schemas triggers 42P06. Restore into a clean database or usepg_restore --cleanwhere appropriate.
Troubleshooting
If the error appears even though your schema list does not show the name, check for a quoted or mixed-case name. CREATE SCHEMA "Reporting" is different from CREATE SCHEMA reporting; PostgreSQL folds unquoted identifiers to lowercase, so the two can coexist and confuse your search. Query pg_namespace with a case-insensitive filter: SELECT nspname FROM pg_namespace WHERE lower(nspname) = 'reporting';.
If permission errors accompany the duplicate error, confirm your role can create schemas: SELECT has_database_privilege(current_user, current_database(), 'CREATE');. If it returns false, ask the database owner to grant the privilege or create the schema for you.
If a migration tool keeps failing on the same statement, inspect the tool's migration history table to see whether a previous run partially applied. Some tools record a migration as complete only after all statements succeed, so a half-applied migration may need manual cleanup before re-running.
If you dropped the schema but the error persists, you may be connected to a different database or a different server than you think. Recheck the host, port, and database name in your connection string.
Frequently Asked Questions
Is error 42P06 the same as a duplicate table error?
No. 42P06 is specifically a duplicate schema. A duplicate table raises 42P07, and a duplicate database raises 42P04.
Can two databases on the same server have a schema with the same name?
Yes. Schemas are scoped to a database, so reporting can exist in both appdb and analytics without conflict.
Does CREATE SCHEMA IF NOT EXISTS check the schema contents?
No. It only suppresses the error when the schema name already exists; it does not confirm that tables, columns, or privileges match what your script expects.
What happens to objects inside a schema when I drop it?
They are deleted permanently. Without CASCADE, the drop fails if the schema is not empty; with CASCADE, all contained objects are removed.
Why does the error mention a schema I never created?
It may have been created by an earlier migration, a restore, or another application sharing the database. Check the schema owner in pg_namespace to trace its origin.
Can I rename an existing schema instead of dropping it?
Yes. ALTER SCHEMA reporting RENAME TO reporting_old; frees the original name without deleting data. You must own the schema or be a superuser to rename it.