Below are five ways to check if a table exists in a PostgreSQL database.
The pg_tables
View
The pg_tables
view contains information about each table in the database.
We can use it to check if a given table exists in the current database:
SELECT EXISTS (
SELECT FROM
pg_tables
WHERE
schemaname = 'public' AND
tablename = 'actor'
);
Result:
True
In this case I get True
, which means that the table does exist (and I have access to it).
Depending on your configuration, you may get t
/f
instead of True
/False
.
The information_schema.tables
View
The information_schema.tables
view contains all tables and views defined in the current database that the current user has access to.
We can use it to check if a given table exists and that we have access to it:
SELECT EXISTS (
SELECT FROM
information_schema.tables
WHERE
table_schema LIKE 'public' AND
table_type LIKE 'BASE TABLE' AND
table_name = 'actor'
);
Result:
True
Another way to do it would be to get the count:
SELECT
COUNT(table_name)
FROM
information_schema.tables
WHERE
table_schema LIKE 'public' AND
table_type LIKE 'BASE TABLE' AND
table_name = 'actor';
Result:
1
The table_type
can be one of the following:
BASE TABLE | Persistent base table (normal table) |
VIEW | View |
FOREIGN | Foreign table |
LOCAL TEMPORARY | Temporary table |
You can omit table_type
from your filter if you want to check whether the name exists across all types.
System Catalogs
The system catalogs are the place where an RDBMS stores schema metadata, such as information about tables and columns, and internal bookkeeping information.
In Postgres, system catalogs are regular tables.
We can use two of them to check if a given table exists:
SELECT EXISTS (
SELECT FROM
pg_catalog.pg_class c
JOIN
pg_catalog.pg_namespace n ON
n.oid = c.relnamespace
WHERE
n.nspname = 'public' AND
c.relname = 'actor' AND
c.relkind = 'r'
);
Result:
True
The relkind
of r
is for ordinary table.
You can remove the relkind
filter altogether if you simply want to check whether or not an object already has the name that you want to give to a table.
Or you could filter by other types.
Here are the options:
r | ordinary table |
i | index |
S | sequence |
t | TOAST table |
v | view |
m | materialized view |
c | composite type |
f | foreign table |
p | partitioned table |
I | partitioned index |
The to_regclass()
Function
The to_regclass()
function translates a textual relation name to its OID. If the name exists, the OID is returned.
Example:
SELECT to_regclass('public.actor');
Result:
actor
If the table doesn’t exist, NULL is returned.
Cast to regclass
It’s also possible to cast the table name to type regclass
:
SELECT 'public.actor'::regclass
Result:
actor
However, if the table doesn’t exist, an error occurs.
Check if a Table Already Exists Before Creating It
If you need to create the table if it doesn’t exist, you can use the IF NOT EXISTS
clause of the CREATE TABLE
statement. If the table doesn’t exist, it will be created. If it already exists, it won’t be created.
See Create a Table Only if it Doesn’t Exist in PostgreSQL for an example.