PostgreSQL Error 42710 duplicate_object: How to Fix It
In this article
PostgreSQL error 42710 (duplicate_object) means you tried to create a database object that already exists: a table, index, function, trigger, schema, extension, or similar. The fix is to either drop or rename the existing object, skip creation with IF NOT EXISTS, or make your script idempotent so re-running it doesn't collide with what is already there. This guide walks through identifying the conflicting object and choosing the right fix.
What You'll Need
- Access to the PostgreSQL server via psql or another SQL client
- Credentials for a role that owns the object or has privileges to alter/drop it
- The full error message, which names the object type and name
Step-by-Step
- Read the full error message. The error always tells you what collided, for example:
ERROR: relation "users" already existsorERROR: function "my_func()" already exists. Note the object type (relation = table/view/index, type, function, schema, extension, etc.) and its exact name. - Confirm the existing object. In psql, inspect it. For a table:
\dtor querySELECT * FROM pg_tables WHERE tablename = 'users';. For any object type, the system catalogs in thepg_catalogschema hold the truth. Check the schema too:SELECT schemaname FROM pg_tables WHERE tablename = 'users';— the duplicate may live in a different schema than the one your search_path resolves first. - Decide whether the existing object should stay. If it was created intentionally (by an earlier migration or a teammate), you should not drop it blindly. Compare its definition:
\d+ usersin psql, or querypg_get_viewdef/pg_get_functiondeffor views and functions. - Option A: make the statement idempotent. If you want the script to run whether or not the object exists, add IF NOT EXISTS:
CREATE TABLE IF NOT EXISTS users (...);. This works for TABLE, INDEX, SCHEMA, DATABASE, TYPE, EXTENSION, and others. For functions and procedures there is no IF NOT EXISTS — useCREATE OR REPLACE FUNCTIONinstead, which replaces the existing definition without error. - Option B: drop the old object first. If the existing object is stale or wrong, remove it and re-create it:
DROP TABLE IF EXISTS users;then run your CREATE. Add CASCADE only if you understand what dependent objects it will remove. Always take a backup or dump of the table data first if it contains anything you need:pg_dump -t users dbname > users.sql. - Option C: rename instead of drop. To keep the old object around:
ALTER TABLE users RENAME TO users_old;then create the new one. Renames exist for most object types (indexes, functions via ALTER FUNCTION, schemas, etc.). - If the duplicate is an extension:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";resolves the common case where a setup script re-runs. Note that extension names are case-sensitive when quoted. - If the duplicate is a trigger or constraint: these cannot use IF NOT EXISTS. Drop and re-create:
DROP TRIGGER IF EXISTS set_updated_at ON users;before the CREATE TRIGGER statement. - Re-run your script or migration. If you use a migration tool, the usual cause is an out-of-order or partially applied migration. Bring the migration history table back in sync with the actual schema rather than forcing the CREATE through.
Common Mistakes
- Dropping the existing table without checking whether it holds live data. A quick
SELECT count(*)and a pg_dump first costs little. - Ignoring the schema in the error.
public.usersandreports.usersare different objects; creating one should not collide with the other, so if it does, your search_path or explicit schema qualification is the real issue. - Using
DROP ... CASCADEby habit. CASCADE silently drops dependent views, foreign keys, and functions. - Trying IF NOT EXISTS on functions and triggers, where the syntax is not supported, then assuming the script is safe to re-run when it is not.
- Blindly re-running a failed migration multiple times, compounding partial state.
Tips
- Write migrations idempotent from the start: IF NOT EXISTS on creates, IF EXISTS on drops, CREATE OR REPLACE on functions.
- Use explicit schema names (public.users, not users) in scripts to avoid search_path surprises across environments.
- In psql,
\d+ name,\df name, and\dx(extensions) are the fastest ways to see what already exists. - When two developers create the same object in different branches, resolve it in the migration files, not by manual drops on the shared database.
Troubleshooting
The object does not appear to exist, but CREATE still fails. Check the exact name and case. Unquoted identifiers fold to lowercase, so CREATE TABLE Users creates users; a quoted "Users" is a different table. Search all schemas: SELECT table_schema FROM information_schema.tables WHERE table_name = 'users';
The error names an index, but you never created one. A unique or primary key constraint creates an index with the same name. If your CREATE INDEX uses the same name as an existing constraint's index, rename one of them.
You dropped the object and the error persists. You may be connected to a different database or server than the one the script targets. Verify with SELECT current_database(), inet_server_addr(); on both connections.
Permission errors after fixing the duplicate. Dropping and re-creating a table also drops its grants. Re-apply GRANT statements, or prefer ALTER/CREATE OR REPLACE approaches that preserve privileges where possible.
Frequently Asked Questions
What does SQLSTATE 42710 mean?
It is the standard SQLSTATE code for duplicate_object: a CREATE statement targeted an object name that already exists in that schema.
Does CREATE TABLE IF NOT EXISTS update the existing table?
No. It silently skips creation if the table exists; the existing structure is left untouched. Use ALTER TABLE to change an existing table.
Why is there no IF NOT EXISTS for functions?
PostgreSQL provides CREATE OR REPLACE FUNCTION instead, which updates the definition in place. Triggers and constraints have neither, so drop them with IF EXISTS first.
Can 42710 happen inside a transaction or migration?
Yes. Any CREATE in a transaction can raise it, and the transaction aborts. Make migration steps idempotent so a re-run after a partial failure succeeds.
Is it safe to just drop the existing object?
Only if you have verified it holds no data or dependencies you need. Inspect it, dump it if it contains data, and avoid CASCADE unless you know every dependent object.