When I do a \dt
in psql I only get a listing of tables in the current schema (public
by default).
How can I get a list of all tables in all schemas or a particular schema?
In all schemas:
=> \dt *.*
In a particular schema:
=> \dt public.*
It is possible to use regular expressions with some restrictions
\dt (public|s).(s|t)
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | s | table | cpn
public | t | table | cpn
s | t | table | cpn
Advanced users can use regular-expression notations such as character classes, for example [0-9] to match any digit. All regular expression special characters work as specified in Section 9.7.3, except for
.
which is taken as a separator as mentioned above,*
which is translated to the regular-expression notation.*
,?
which is translated to.
, and$
which is matched literally. You can emulate these pattern characters at need by writing?
for.
,(R+|)
forR*
, or(R|)
forR?
.$
is not needed as a regular-expression character since the pattern must match the whole name, unlike the usual interpretation of regular expressions (in other words,$
is automatically appended to your pattern). Write*
at the beginning and/or end if you don't wish the pattern to be anchored. Note that within double quotes, all regular expression special characters lose their special meanings and are matched literally. Also, the regular expression special characters are matched literally in operator name patterns (i.e., the argument of\do
).
\dt
is equivolent to \dt public.*
, am I right?
Commented
Aug 25, 2015 at 12:09
\dt public.user_info, public.user_scope
?
Commented
Nov 11, 2015 at 3:53
\dt public.a; \dt public.b;
on one line.
Commented
Nov 11, 2015 at 3:54
search_path
, and that defaults to "$user", public.*
. Consequently, set search_path=s; \dt
is going to list all tables in the schema s
.
Commented
Jan 25, 2020 at 15:32
You can select the tables from information_schema
SELECT * FROM information_schema.tables
WHERE table_schema = 'public'
AND table_type = 'BASE TABLE'
to the where clause.
Commented
Jul 14, 2021 at 7:52
Alternatively to information_schema
it is possible to use pg_tables
:
select * from pg_tables where schemaname='public';
SELECT tablename FROM pg_tables WHERE schemaname = 'public';
Commented
Feb 14, 2016 at 23:22
information_schema
not listing items from the public
schema, but the pg_tables
method worked nicely. Many thanks!
Commented
Jan 8, 2018 at 22:58
For those coming across this in the future:
If you would like to see a list of relations for several schemas:
$psql mydatabase
mydatabase=# SET search_path TO public, usa; #schema examples
SET
mydatabase=# \dt
List of relations
Schema | Name | Type | Owner
--------+-----------------+-------+----------
public | counties | table | postgres
public | spatial_ref_sys | table | postgres
public | states | table | postgres
public | us_cities | table | postgres
usa | census2010 | table | postgres
If you are interested in listing all tables in a particular schema, I found this answer relevant :
SELECT table_schema||'.'||table_name AS full_rel_name
FROM information_schema.tables
WHERE table_schema = 'yourschemaname';
AND table_type = 'BASE TABLE'
to the where clause.
Commented
Jul 14, 2021 at 7:52
All of the previous answers has covered the public schemas. However, as Postgres supports multiple schemas, you can do query in other schemas too, just put your schema name in place of public.
For Example:
select * from pg_tables where schemaname='your_own_schema_name';