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
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);CREATE TABLECREATE TABLE orders (id int PRIMARY KEY, amount numeric);ERROR: 42P07: relation "orders" already existsSELECT table_schema, table_name FROM information_schema.tables WHERE table_name = 'orders'; 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.
CREATE TABLE IF NOT EXISTS orders (id int PRIMARY KEY, amount numeric);NOTICE: 42P07: relation "orders" already exists, skipping
CREATE TABLE02Reusing 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
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);CREATE TABLE
CREATE TABLE
CREATE INDEXCREATE INDEX idx_created_at ON orders (created_at);ERROR: 42P07: relation "idx_created_at" already existsSELECT tablename, indexname FROM pg_indexes WHERE indexname = 'idx_created_at'; 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.
CREATE INDEX idx_orders_created_at ON orders (created_at);CREATE INDEX03Backup 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
CREATE TABLE products (id int, name text, CONSTRAINT products_pkey PRIMARY KEY (id));
ALTER TABLE products RENAME TO products_backup;CREATE TABLE
ALTER TABLECREATE TABLE products (id int, name text, price int, CONSTRAINT products_pkey PRIMARY KEY (id));ERROR: 42P07: relation "products_pkey" already existsSELECT tablename, indexname FROM pg_indexes WHERE indexname = 'products_pkey'; tablename | indexname
-----------------+---------------
products_backup | products_pkey
(1 row)ALTER INDEX products_pkey RENAME TO products_backup_pkey;ALTER INDEXpg_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.
CREATE TABLE products (id int, name text, price int, CONSTRAINT products_pkey PRIMARY KEY (id));CREATE TABLE04Creating 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
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;CREATE TABLE
INSERT 0 2
CREATE TABLE
INSERT 0 2CREATE VIEW sales AS SELECT id, amount FROM sales_v2;ERROR: 42P07: relation "sales" already existsSELECT relname, relkind FROM pg_class WHERE relname = 'sales' AND relnamespace = 'lab_a'::regnamespace; relname | relkind
---------+---------
sales | r
(1 row)In pg_class, sales has relkind r (an ordinary table). A view would show v.
ALTER TABLE sales RENAME TO sales_old;ALTER TABLEFreeing the old name with ALTER TABLE sales RENAME TO sales_old let the view be created, and sales returned both rows.
CREATE VIEW sales AS SELECT id, amount FROM sales_v2;
SELECT * FROM sales ORDER BY id;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