PostgreSQL 17.12Lab-verified · 2026-12-01

PostgreSQL error 42P07: relation already exists

A table, index or other relation with that name already exists in the schema. Index and constraint names count too.

PostgreSQL 17.12
42P07: relation "orders" already exists

Jump to a cause

  1. 01Running the same CREATE TABLE twice
  2. 02Reusing an index name on another table
  3. 03Backup made with RENAME keeps the old products_pkey name
  4. 04Creating a view with the same name as a table (compatibility view)

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17

In PostgreSQL, tables, indexes, sequences and views all share one namespace per schema, called relations. So 42P07 can come from a table name, but also from an index name or the index behind a primary key.

The quoted name in the message tells you which one. If it is not a table you know, look it up in pg_indexes.

01Running the same CREATE TABLE twice

A migration or setup script runs again, and CREATE TABLE orders fails because orders already exists in schema lab_a.

Setup
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
✕ ERROR
ERROR:  42P07: relation "orders" already exists
Checkrun as lab_a
SELECT table_schema, table_name FROM information_schema.tables WHERE table_name = 'orders';
OUTPUT
 table_schema | table_name
--------------+------------
 lab_a        | orders
(1 row)

information_schema.tables shows lab_a.orders is already there. If the script should be safe to rerun, use CREATE TABLE IF NOT EXISTS: PostgreSQL then prints NOTICE: relation "orders" already exists, skipping and continues. Note that it does not compare columns; an existing table with a different structure is left as it is.

After the fixrun as lab_a
CREATE TABLE IF NOT EXISTS orders (id int PRIMARY KEY, amount numeric);
✓ SUCCESS
NOTICE:  42P07: relation "orders" already exists, skipping
CREATE TABLE

02Reusing an index name on another table

Index names must be unique in the schema, not per table. idx_created_at already exists on customers, so creating an index with the same name on orders gives 42P07 with the index name in the message.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, created_at date);
CREATE TABLE orders (id int PRIMARY KEY, created_at date);
CREATE INDEX idx_created_at ON customers (created_at);
OUTPUT
CREATE TABLE
CREATE TABLE
CREATE INDEX
Reproducerun as lab_a
CREATE INDEX idx_created_at ON orders (created_at);
✕ ERROR
ERROR:  42P07: relation "idx_created_at" already exists
Checkrun as lab_a
SELECT tablename, indexname FROM pg_indexes WHERE indexname = 'idx_created_at';
OUTPUT
 tablename |   indexname
-----------+----------------
 customers | idx_created_at
(1 row)

pg_indexes shows idx_created_at belongs to customers. Include the table name in index names: CREATE INDEX idx_orders_created_at ON orders (created_at) succeeds.

After the fixrun as lab_a
CREATE INDEX idx_orders_created_at ON orders (created_at);
✓ SUCCESS
CREATE INDEX

03Backup made with RENAME keeps the old products_pkey name

ALTER TABLE products RENAME TO products_backup renames only the table. Its primary key index is still called products_pkey. Creating a new products with CONSTRAINT products_pkey then fails, and the message names products_pkey, not the table.

Setup
Setuprun as lab_a
CREATE TABLE products (id int, name text, CONSTRAINT products_pkey PRIMARY KEY (id));
ALTER TABLE products RENAME TO products_backup;
OUTPUT
CREATE TABLE
ALTER TABLE
Reproducerun as lab_a
CREATE TABLE products (id int, name text, price int, CONSTRAINT products_pkey PRIMARY KEY (id));
✕ ERROR
ERROR:  42P07: relation "products_pkey" already exists
Checkrun as lab_a
SELECT tablename, indexname FROM pg_indexes WHERE indexname = 'products_pkey';
OUTPUT
    tablename    |   indexname
-----------------+---------------
 products_backup | products_pkey
(1 row)
Fixrun as lab_a
ALTER INDEX products_pkey RENAME TO products_backup_pkey;
OUTPUT
ALTER INDEX

pg_indexes shows products_pkey on products_backup. Rename the old index first: ALTER INDEX products_pkey RENAME TO products_backup_pkey. Then the new products table is created.

After the fixrun as lab_a
CREATE TABLE products (id int, name text, price int, CONSTRAINT products_pkey PRIMARY KEY (id));
✓ SUCCESS
CREATE TABLE

04Creating a view with the same name as a table (compatibility view)

After restructuring the table into sales_v2, a view named sales was created for old programs. Tables, views, indexes and sequences share one name space per schema, so it fails with relation "sales" already exists.

Setup
Setuprun as lab_a
CREATE TABLE sales (id int PRIMARY KEY, amount int);
INSERT INTO sales VALUES (1, 100), (2, 250);
-- new table after restructuring
CREATE TABLE sales_v2 (id int PRIMARY KEY, amount int, sold_on date);
INSERT INTO sales_v2 SELECT id, amount, date '2026-12-01' FROM sales;
OUTPUT
CREATE TABLE
INSERT 0 2
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
CREATE VIEW sales AS SELECT id, amount FROM sales_v2;
✕ ERROR
ERROR:  42P07: relation "sales" already exists
Check the causerun as lab_a
SELECT relname, relkind FROM pg_class WHERE relname = 'sales' AND relnamespace = 'lab_a'::regnamespace;
OUTPUT
 relname | relkind
---------+---------
 sales   | r
(1 row)

In pg_class, sales has relkind r (an ordinary table). A view would show v.

Fixrun as lab_a
ALTER TABLE sales RENAME TO sales_old;
OUTPUT
ALTER TABLE

Freeing the old name with ALTER TABLE sales RENAME TO sales_old let the view be created, and sales returned both rows.

After the fixrun as lab_a
CREATE VIEW sales AS SELECT id, amount FROM sales_v2;
SELECT * FROM sales ORDER BY id;
✓ SUCCESS
CREATE VIEW
 id | amount
----+--------
  1 |    100
  2 |    250
(2 rows)

FAQ

What does PostgreSQL relation already exists mean?

Something named like that already exists in the schema. A relation can be a table, index, sequence or view, so the name may belong to an index rather than a table.

How do I create a table only if it does not exist in PostgreSQL?

Use CREATE TABLE IF NOT EXISTS. It skips with a NOTICE instead of an error, but it does not check that the existing columns match.

Why does CREATE INDEX say relation already exists?

Index names are unique per schema. Another table already has an index with that name. Check pg_indexes and use a name that includes the table.

relation "table_pkey" already exists after renaming a table

Renaming a table does not rename its primary key index. Rename the old index with ALTER INDEX ... RENAME TO ... before creating the new table.

Can a view have the same name as a table in PostgreSQL?

Not in the same schema. Tables, views, indexes and sequences share names, so you get 42P07. Here the table was renamed first, then the view was created.

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