Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 42P06: Duplicate Schema Fix

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 psql or 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 SELECT queries against information_schema.schemata or pg_namespace to inspect existing schemas.

Step-by-Step

  1. Confirm the current database. In psql, run SELECT 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_database before doing anything else.
  2. 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 contains public, information_schema, and pg_catalog; those are normal.
  3. 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.
  4. 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, use ALTER ROLE your_role SET search_path TO reporting, public;. You can also qualify objects directly, for example CREATE TABLE reporting.events (...);.
  5. Create the schema under a new name. If the existing schema belongs to another application, choose a distinct name such as reporting_v2 and run CREATE SCHEMA reporting_v2;. Optionally add an owner: CREATE SCHEMA reporting_v2 AUTHORIZATION your_role;.
  6. 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 that IF NOT EXISTS only 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.
  7. 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 use DROP SCHEMA reporting CASCADE;, which permanently deletes every table, view, function, and sequence inside it. Take a backup first with pg_dump -n reporting your_database > reporting_backup.sql. There is no undo.
  8. 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 \dn in psql or the equivalent schema list in your client.

Common Mistakes

  • Checking the wrong database. Running the schema list in postgres when your application uses appdb will show no conflict, yet the error persists on the real connection. Always verify current_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 CASCADE casually. Dropping a schema with CASCADE removes dependent objects, and if other schemas reference them, those references break too. Confirm with a backup before running it.
  • Confusing schema with database. CREATE DATABASE and CREATE SCHEMA are different; a duplicate database raises a different error (42P04), not 42P06.
  • Relying on IF NOT EXISTS as 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_prod and app_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+ in psql to see schemas with their owners and access privileges at a glance.
  • When restoring a dump, remember it may already contain CREATE SCHEMA statements; restoring into a database that already has those schemas triggers 42P06. Restore into a clean database or use pg_restore --clean where 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.