PostgreSQL Error 42P06: duplicate schema
PostgreSQL error code 42P06 occurs when you attempt to create a schema that already exists in the database. Since schema names must be unique within a database, PostgreSQL raises this error immediately when a CREATE SCHEMA statement targets an already-existing schema name. This error is especially common in automated deployment pipelines and multi-environment setups.
Top 3 Causes
1. Missing IF NOT EXISTS in Migration Scripts
The most common cause is simply omitting the IF NOT EXISTS clause. Scripts that work fine on a fresh development database will fail on staging or production where the schema already exists.
-- ❌ Problematic
CREATE SCHEMA myapp;
-- ✅ Safe and idempotent
CREATE SCHEMA IF NOT EXISTS myapp;
-- ✅ With owner specified
CREATE SCHEMA IF NOT EXISTS myapp AUTHORIZATION app_user;
2. Duplicate Execution of Deployment Scripts
CI/CD pipelines or manual re-runs of setup scripts without idempotency checks trigger this error on every run after the first.
-- ✅ Use a DO block for conditional creation with logging
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_namespace WHERE nspname = 'myapp'
) THEN
CREATE SCHEMA myapp;
RAISE NOTICE 'Schema created.';
ELSE
RAISE NOTICE 'Schema already exists. Skipping.';
END IF;
END
$$;
3. Race Condition in Multi-Instance Applications
In microservices environments, multiple application instances may attempt to create the same schema simultaneously at startup. One instance succeeds; the others hit 42P06.
-- ✅ Handle race condition gracefully with exception handling
DO $$
BEGIN
CREATE SCHEMA myapp;
EXCEPTION
WHEN duplicate_schema THEN
RAISE NOTICE 'Schema already exists, skipping safely.';
END
$$;
Quick Fix Solutions
Check existing schemas first, then act accordingly:
-- List all non-system schemas
SELECT nspname AS schema_name
FROM pg_namespace
WHERE nspname NOT LIKE 'pg_%'
AND nspname <> 'information_schema'
ORDER BY nspname;
-- Safe drop and recreate (use carefully in production!)
DROP SCHEMA IF EXISTS myapp CASCADE;
CREATE SCHEMA myapp;
Prevention Tips
Always use IF NOT EXISTS — Make it a mandatory team convention enforced through code review checklists or SQL linters like sqlfluff in your CI pipeline.
-- Standard team template for schema creation
CREATE SCHEMA IF NOT EXISTS myapp AUTHORIZATION app_user;
COMMENT ON SCHEMA myapp IS 'Main application schema';
Design idempotent migration scripts — Whether you use Flyway, Liquibase, or plain SQL scripts, every migration file should produce the same result regardless of how many times it runs. Never assume a clean-slate database in production environments.
-- Idempotent multi-schema initialization example
CREATE SCHEMA IF NOT EXISTS myapp;
CREATE SCHEMA IF NOT EXISTS myapp_audit;
CREATE SCHEMA IF NOT EXISTS myapp_archive;
Related Errors
| Error Code | Name | Description |
|---|---|---|
42P07 |
duplicate_table |
Creating a table that already exists |
42710 |
duplicate_object |
Duplicate index, type, or function |
3F000 |
invalid_schema_name |
Schema name is syntactically invalid |
📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.
Top comments (0)